Skip to main content
GameDev.net gamedev.net
🔒 Locked

Vector rotation + trigonometry question

Started by dxdotnet1 Apr 13, 2005 at 8:55 PM 7 replies 33.1k views
Original Post
dxdotnet1
dxdotnet1
Can somebody explain in detail or perhaps provide a link on how rotation is done using the trigonotric functions? i knw the basic of this trigonometric functions like the soh-cah-toa thing but i dont knw and i dont understand how this functions are used in rotation hope i can get the answer from you guys :) thanks
Zakwayda
Zakwayda
One way to rotate a point or vector about the origin in 2d is:

x' = cos(a) * x - sin(a) * y
y' = sin(a) * x + cos(a) * y

To rotate about some point other than the origin, you can translate by the negative of that point, perform the rotation, and then translate back.

Rotation in general is a pretty big topic, but maybe the above will help get you started.
dxdotnet1
dxdotnet1
thanks for the reply

the only thing i dont understand is why are you using sine and cosine for rotation?

what is the explanation behind using these two in rotation

pls excue my ignorance.
esaint
esaint
Trigonometry is, by definition, the relationship between angles and distances. Consider the following three-part image:
sine and cosine
http://www.ekermo.se/tmp/cosine.gif

... Now, on the unit circle (the circle with a radius of 1, if it's not, you need to multiply the sine/cosine by the radius to get accurate values), you can easily see how the X and Y coordinates for any point of the circle can be achieved using an angle and sine/cosine. Simply enough, the X points projection on the axis, it's X coordinate (horizontal, forgot to label them, sorry), is the cosine of the angle, and Y the sine, respectively.

Consider the point at the end of the red line. Say we want to rotate that around origo 100 degrees. What we do is take the projections on the principal axes (x and y) and rotate each of them 100 degrees using sin and cos, as in picture 2.

Now, the final vector is the sum of these two, as picture 3 shows, and using this formula, we can rotate any point in 2D space.

3D space is kinda similar, only you need to rotate around an axis, and not a point. This is kind of an advanced topic though, and I won't go into it unless you really want me to ():-)

Rotations can be handled using matrices, you could check this out: http://www.makegames.com/3drotation/ ... Also, 2D rotation can be handled using 2D complex numbers, and similarly, 3D rotation can be handled using 4D complex numbers, quaternions. That is beyond the realm of simple trigonometry, though.
There are two secrets to great programming:1. Don''t tell everything you know
robert_p
robert_p
There are a couple ways to think about rotations
the vector way

the dot product of a vector is defined as
v.x*u.x + v.y*u.y
which is also equal to
||v||*||u|| * cos(angle between u and v)

if the length of u is 1
then this gives you ||v|| * cos(theta)
which is your new x component in this frame of refrence

x = dot product of the vector and a normalized vector
y = 2d cross product of the vector and a normalized vector

this will give you a new coordinate frame of refrence


and we can get the y component using the 2d cross product which is simply
dotting the normal of the vector.
the normal being (y,-x), which is a vector with no projection on the x axis, this gives us 2 mutually perpendicular unit length axes.

so the y component is
u.x * v.y - u.y * v.x

also the trig way to think about it

a vector is defined as
r * (cos(A),sin(A))

and we want to rotate our frame of refrence so we want
r * (cos(A-B),sin(A-B))
or
r * (cos(A-B),0) + r * (0,sin(A-B))

the cos(A-B) is defined as cos(A) * cos(B) + sin(A) * sin(B)
the sin(A-B) is defined as sin(A) * cos(B) - cos(A) * sin(B)

again if one of your vectors is unit length then the r distributes corrently

this gives you a couple ways to represent 2d rotations

as an angle
as a unit vector
as a matrix containing your new x and y axes
esaint
esaint
Without getting to heavily into general solutions and maths (as the above posters and lots of articles have covered this already), I could give you an example on how to solve the problem "rotating a point A around point B by C degrees".

Now. First of all, as I described in the previous post, a point that is on the X axis, L distance from origo, is rotated C degrees around origo by

x = L * cos(C)
y = L * sin(C)


Similarly, the formula for a perpendicular vector is x = -y | y = x, which means that a point that is on the Y axis (again, L from origo) would be rotated by C using the formula
 x = - L * sin(C) y = L * cos(C)

As shown in the above image, the final solution is the sum of the rotations of the projected vectors, so we can derive the formula
 x' = x * cos(C) - y * sin(C) y' = y * cos(C) + x * sin(C)

... but you knew that already, right? problem is, this formula only rotates around origo. So what we need to do is move the coordinate system we're rotating around to origo, rotate and then move back. This can be done quickly with complex numbers or in general solutions with matrices, but we're gonna stick to vector math on this one to keep it simple.

first step; move the origin point.
x' = A.x - B.xy' = A.y - B.y

second step, perform rotation
x''' = x' * cos(C) - y' * sin(C) = (A.x-B.x) * cos(C) - (A.y-B.y) * sin(C)y''' = y' * cos(C) + x' * sin(C) = (A.y-B.y) * cos(C) + (A.x-B.x) * sin(C)

third and final step, move back the coordinate frame
x''''' = x''' + B.x = (A.x-B.x) * cos(C) - (A.y-B.y) * sin(C) + B.xy''''' = y''' + B.y = (A.y-B.y) * cos(C) + (A.x-B.x) * sin(C) + B.y

And presto! we have our rotation formula. I'll give it to you without all those calculations:
Rotating a point A around point B by angle CA.x' = (A.x-B.x) * cos(C) - (A.y-B.y) * sin(C) + B.xA.y' = (A.y-B.y) * cos(C) + (A.x-B.x) * sin(C) + B.y

If you've been following me here (and I'm a pretty lousy teacher, so sorry if you haven't), you can se that the ordering in which you perform these operations is very important. Try to mix step 3 and 1 and see the difference in the formulae you get.

Good luck and all!
There are two secrets to great programming:1. Don''t tell everything you know
Daerax
Daerax
Based on the OP's post, I think the level of mathematics used in the explanations of this thread to be too advanced.

They're nonetheless quite good. Rating++ for esaint and robert_p.

----------------------------------------
Edit for more constructivenes

First go here [graph relating trig funcs to rotation, use the applet] and also here [Unit Circle Applet] to gain a more intuitive feel for the relationship between the trigonometric functions/sinusoids and circles or rotations. Then look at the proof below.

Recalling that sin(θ) = y /r and cos(θ) = x/r:


 sin(<font face="Symbol">f</font> + <font face="Symbol">q</font>) =   AD  , noting that AD = AB + BD and BD = CE, also AC ⊥ OC               OA                     = AB + CE       OA   OA          = AC AB + CE OC       AC OA   OA OC                   = AC AB + CE OC        OA AC   OC OA = sin(Φ)cos(θ) + sin(θ)cos(Φ)   (1)It is very similar for cosine.

Remebering that x =rcos(θ) and y = rsin(θ) if we rotate point (x,y) by angle Φ then we have x' = rcos(θ + Φ) and y' = rsin(θ + Φ). Recalling equation (1) we then have:

y' = rsin(Φ)cos(θ) + rsin(θ)cos(Φ)

where the underlined terms represnt term we arleady have definitions for. That is x = rcos(θ), y = rsin(θ). So this breaks down to:

y' = xsin(Φ) + ycos(Φ)

where y' is the new y after rotating by Φ. The case is similar for x'.

[Edited by - Daerax on May 6, 2005 11:54:13 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.