Jump to content

View more

Image of the Day

Adding some finishing touches...
Follow us for more
#screenshotsaturday #indiedev... by #MakeGoodGames https://t.co/Otbwywbm3a
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

How to have a Sprite follow the player?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
3 replies to this topic

#1 XwingVmanX   Members   

108
Like
0Likes
Like

Posted 07 August 2012 - 11:53 AM

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.

#2 Andy474   Members   

694
Like
1Likes
Like

Posted 07 August 2012 - 12:08 PM

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]
Posted Image
excuse the math ... might not be right, been ages since i done somthing like this :P

#3 XwingVmanX   Members   

108
Like
0Likes
Like

Posted 07 August 2012 - 12:24 PM

OOOOH, Wow all that math is coming back to me. Thanks lol

#4 PunCrathod   Members   

596
Like
1Likes
Like

Posted 07 August 2012 - 02:32 PM

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.
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();
}

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, 07 August 2012 - 02:45 PM.





Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.