How would you get a point that is a distance from another? (2D Vectors)

Scripting questions, discussions, etc
7 posts Page 1 of 1
aMoistKite
Posts: 18
Joined: Wed Mar 06, 2019 10:07 am
by aMoistKite » Tue Jun 04, 2019 12:11 pm
So I am making a game with a diablo style of movement and when the player clicks on an npc to say talk to them i need to get the point that is between the player and the npc that is a set distance from the npc to move the player to. I am honestly bad at math so if anyone could help me with this? I had been searching around on google and got something like this:

Code: Select all

%interactionDistance = 4; %interactX = %objectX + (%playerX - %objectX) / %interactionDistance; %interactY = %objectY + (%playerY - %objectY) / %interactionDistance;
but it only moves the player closer with the father away the player is.
Duion
Posts: 1335
Joined: Sun Feb 08, 2015 1:51 am
 
by Duion » Tue Jun 04, 2019 12:41 pm
I'm very bad at math as well, but I always try to find an out of the box solution or a workaround.

In may game players just run into each other, which is not a problem, since they usually kill each other before and if they hit each other they get pushed away through the physics engine.

I think the engine can already do what you need with the navMesh functionality, you only need to re-build the navmesh where the NPC is standing and the area the NPC is on will be cut out from the accessible area and therefore anyone trying to move onto the NPC position will stop before him, depending on the buffer radius you set to use in the navMesh.

I have no idea how to re generate the navMesh at runtime, but I saw @
User avatar
Azaezel
doing it in his game, so AI players do not run into each other.

So maybe that solution will work, you can try it out manually first before implementing it fully, if the NPCs are static and do not move it is even simpler, then you can regularly exclude the area they are standing on out of the navMesh, so the player will always stop at an exact distance before it.
Jason Campbell
Posts: 321
Joined: Fri Feb 13, 2015 2:51 am
 
by Jason Campbell » Tue Jun 04, 2019 2:36 pm
Perhaps VectorDist

%distance = VectorDist(%player.getPosition(), %npc.getPosition());

or
if (VectorDist(%player.getPosition(), %npc.getPosition()) <= 4)
{
Stop your player here
}

Or maybe if it is >=4 move the player closer.

I may be able to help more.
XIXWYRMEXIX
Posts: 44
Joined: Sat Mar 07, 2015 3:13 pm
by XIXWYRMEXIX » Tue Jun 04, 2019 2:51 pm
If I understand what you are trying to do, you want to click the NPC and have the player move to the NPC and stop a set distance away? You could set the NPC where he will be and then just use map coordinates to do this and you would not have to deal with any math. For example- NPC sits at -45, 34 on the map, when a player clicks on the NPC the player auto navigates to coordinates -45, 32, when the player reaches those coordinates have the interaction window open. Obviously the coordinates are just for example, the actual number differences will be different depending on your map etc. etc. The nav mesh that just comes with torque can auto navigate your player around objects. This is just off the top of my head as an idea. It is probably how I would do it.
OTHGMars
Posts: 30
Joined: Fri Jul 14, 2017 2:19 am
by OTHGMars » Tue Jun 04, 2019 11:23 pm
// Initial data
%npcPosition = %talkingNPC.getPosition();
%playerPosition = %clickingPlayer.getPosition();
%interactionDistance = 4;

// Get the vector between the NPC and the player
%separationVector = VectorSub(%playerPosition, %npcPosition);

// Eliminate the z component of the vector (only because OP says '2D vectors')
%separationVector.z = 0;

// Normalize to a length of 1
%unitVector = VectorNormalize(%separationVector);

// Scale the unit vector by %interactionDistance
%interactVector = VectorScale(%unitVector, %interactionDistance);

// Add %interactVector to the NPC position to get the target player position
%targetPosition = VectorAdd(%npcPosition, %interactVector);
Duion
Posts: 1335
Joined: Sun Feb 08, 2015 1:51 am
 
by Duion » Wed Jun 05, 2019 12:38 am
You all think pretty complicated, I think in most of those Diablo style games, characters just run into each other. They are probably grid based so the player will just occupy the square right next to the NPC.

If it is FPS movement, you only need a raycast to check if the NPC is in interaction distance, which already exists as a function. So player moves towards the NPC and when he is in range he can press a button to interact, there is no need to calculate anything else then.
aMoistKite
Posts: 18
Joined: Wed Mar 06, 2019 10:07 am
by aMoistKite » Thu Jun 06, 2019 6:36 am
// Initial data
%npcPosition = %talkingNPC.getPosition();
%playerPosition = %clickingPlayer.getPosition();
%interactionDistance = 4;

// Get the vector between the NPC and the player
%separationVector = VectorSub(%playerPosition, %npcPosition);

// Eliminate the z component of the vector (only because OP says '2D vectors')
%separationVector.z = 0;

// Normalize to a length of 1
%unitVector = VectorNormalize(%separationVector);

// Scale the unit vector by %interactionDistance
%interactVector = VectorScale(%unitVector, %interactionDistance);

// Add %interactVector to the NPC position to get the target player position
%targetPosition = VectorAdd(%npcPosition, %interactVector);
Works like a charm. Thank you good sir.
7 posts Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest