Sign in to follow this  
TheBenOne

Making a NPC move/rotate towards the player

Recommended Posts

TheBenOne    122
Hello! I want my NPC face the player character. I suppose the new directions are somehow based on the player char's X- and Z-Positions. What rotation values do I have to pass to D3DXMatrixRotationYawPitchRoll()? The same question goes for movement towards the player and the D3DXMatrixTranslation()-function. Any suggestions? I would be very grateful. Regards!

Share this post


Link to post
Share on other sites
TheBenOne    122
OK, at least I solved the second problem (the coordinates to move to). The NPC now walks in the direction he is facing by this call:

NBPC.MoveRel(-sin(NPC.GetYRotation()) * ElapsedTime, 0.0f, -cos(NPC.GetYRotation()) * ElapsedTime);

But the question how to rotate the NPC in the first place, so that he faces the player char, still remains...

Share this post


Link to post
Share on other sites
BoReDoM_Inc    112
To face the character towards the NPC you just use basic trigonometry. I'm not sure how to apply this to DirectX as I don't use it but to find the angle between the NPC and the player is just:

atan2((NPC.Z - Player.Z), (NPC.X - Player.X))

NOTE: atan2 is inverse tan and returns a value between –π and π.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this