Sign in to follow this  

How to have a Sprite follow the player?

This topic is 1957 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was trying to figure out how to make a sprite always look at the players current position. My problem is I am not sure how to check which way the sprite is facing in relation to the players position and adjust him accordingly. I would be grateful if I could get any suggestions as to how to start approaching this.

Share this post


Link to post
Share on other sites
Trig :) sohcahtoa or whatever you where tought, and you can find the angle between the sprite and your player, then just rotate it.

E.g. [Badly done picture]
[img]http://i.imgur.com/vx6Au.png[/img]
excuse the math ... might not be right, been ages since i done somthing like this :P

Share this post


Link to post
Share on other sites
I'll assume you are using c# and XNA. You should use Math.atan2(x2-x1,y2-y1) instead of atan(dx/dy) so you don't have to worry about special case dy=0 and don't have to find wich side the target is. Also what I like to use in cases like these I like to use the following code.
[CODE]
targetangle=Math.Atan2(target.x-this.x,target.y-this.y)//get the angle to the target

turnangle=targetangle-currentangle;//get the difference between the current angle and the target angle

if(turnangle>2*Math.PI)//make sure to wrap the angle between 0 and 2PI
turnangle-=2*Math.PI
if(turnangle<0)
turnagnel+=2*Math.PI

if(turnangle<Math.PI&&turnangle>Deadzone){//Check wich side you should turn to get to your target.
turnright();
}
else if(turnangle>Math.PI&&turnangle<(2*Math.PI)-Deadzone)
{
turnleft();
}
[/CODE]

This should always turn to the direction that is closer to the target. Deadzone is the amount of radians(you get radians from degrees with degrees/360*(2*PI)) the seeker can be off course before it starts to correct its course and should always be between 0 and PI radians(0 and 180 degrees) for this to work correctly.
If there are any errors in the code or elsewhere I blame the late hours. Edited by PunCrathod

Share this post


Link to post
Share on other sites

This topic is 1957 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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