Jump to content
• What is your GameDev Story?

• Advertisement

Character Movement

This topic is 4907 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

Given a 2d point representing an objects current position and a 2d point representing where that object needs to go, how would I go about rotating the object so that it faces the point it is moving toward? -Chris

Share this post

Share on other sites
Advertisement
You would have to give the object a facing vector, which you would keep track of. Then you would find the angle between the direction-to-face and the facing. Then interpolate evenly using time, updating the facing each frame.

Thats a brief overview.

ace

Share this post

Share on other sites
I'm only a beginner game developer and my math isn't that good, but that's the solution I got from the top of my head:
Let us assume you're object is currently located at point A(x1,y1) and looking at point B(x2,y2). You want it to look at point C(x3,y3) (while x3!=x2!=x1 and y3!=y2!=y1, also notice that points A and C are immovable, whereas B isn't).
What you'll do is calculate the distance between your objects' current position point and these two points (the point it's currently looking at and the point you want it to look at), and the line equation going through your current objects' position point and the point you want it to look at (equation of line AC).
After finding angle <BAC (using trigonometry and lengths of AB and AC), rotate your object (that is, point A) until it holds the equation of line AC.
that's about it (from the top of my head...)

[Edited by - Arie Segal on August 12, 2005 11:50:18 AM]

Share this post

Share on other sites
you could also use some kind of rotation "standard", for example make rotation angle of 0° face right, or along the x-axis, with increasing angles going counter-clockwise. 90° would be facing up, 180° facing left, 270° down and so on.
Now, if you want to look onto a point T from a point P you could do the following:

PT = T - P, where PT is the vector from P to T, P and T are the vectors representing the two points.
sin(a) = |PT| / PT.y, where |PT| is the magnitude of PT, and PT.y is the y component of the vector. Considering this equation you can calculate the needed angle to face point T with:
a = arcsin( |PT| / PT.y ) = arcsin ( |T-P| / (T.y - P.y) )

If you keep the current angle of your objects, you can now rotate them to fit the resulting angle a. Should work, I guess.

Share this post

Share on other sites
To find the angle that 'points' from A to B (relative to the +x axis):

angle = atan2(B.y-A.y, B.x-A.x);

This is stable as long as A and B are not (nearly) coincident, and requires no normalization. You can use this to find what angle you need to turn to to point at a target. If you want to know what angle you need to turn by to face a target, you can find the signed angle between two vectors like this:

angle = atan2(a.PerpDot(b), a.Dot(b));

Again, this is stable as long as neither vector is (nearly) zero-length, and a and b need not be normalized. In your case, 'a' would be your object's forward vector, and 'b' would be the vector from the object position to the target position.

Share this post

Share on other sites

• Advertisement
• Advertisement
• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• Popular Now

• 13
• 14
• 10
• 11
• 13
• Advertisement
• Forum Statistics

• Total Topics
634096
• Total Posts
3015487
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!