Jump to content

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


aMoistKite

Recommended Posts

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:

%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.

Link to comment
Share on other sites

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 @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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

// 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);
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

// 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.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...