4 Replies Latest reply on Oct 6, 2008 8:40 PM by chinmayagoyal

# Character Movement + Rotation

Hello,
I am currently working on a car racing game but I am new to designing the engine for my game such as the physics for my car. So, I took help from a tutorial but could'nt quite understand the trigonometry part, can you explain it.
The part I did not understand was,
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.hit.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.6;
}
}
Hope you can help me out,
Thanking You,
Chinmaya

P.S. I know how to convert degrees to radians and know a little about unit circles. The main part I dont understand is that why do we multiply the sin of angle(in radians) to the speed!
• ###### 1. Re: Character Movement + Rotation
I'm not sure if I understand. Do you just need to know why you are multiplying the speed or is there something that isn't working?

The multiplication is easy. Basically you have some speed, let's say 5. Now if you are going perfectly to the right along the x axis then you have 5 in the x direction and you have 0 in the y direction.

If you are going down the screen you have 5 in the y direction and 0 in the x direction. Easy.

But what if you are going at some angle? That is when it gets a little more tricky. Well back to your lovely unit circle. If you speed was 1 we have the same issue for the x and y directions. But if you go off at an angle you've got your sin component in the x direction and your cos in the y direction. And there you go, cos(0) = 1 and sin(0) = 0 meaning at an angle of zero all your motion is in the x direction.

So for a given angle cos shows the amount you've got in the x direction and sin shows the y direction. That way you can tell your object how many x and y to move and it looks like movement in at an angle. You multiply them because you aren't always using a unit circle.

• ###### 2. Character Movement + Rotation
Thank you very much for your reply,
So I am correct in saying that we multiply the speed to change the radius of the unit circle(as you said it isn't always a unit).In other words, are we just increasing the hypotenuse of the right triangle inside the circle?

EDIT: Or are we just making the _x and _y co-ordinates increase more according to our speed

OR both??
Chinmaya
• ###### 3. Re: Character Movement + Rotation
Well I think both are just different ways of saying the same thing.

The unit circle, and its associated "unit triangle," will tell you the components of your velocity (velocity has a direction, speed doesn't) for a velocity of 1.

So you are increasing the hypotenuse of the right triangle, but it isn't inside a unit circle anymore! :)

And there is the principle of similar triangles. If we take a triangle and keep the angles the same the lengths of the sides will keep the same proportions.

So if the velocity is seven and we take the same unit triangle (all unit triangles have a hypotenuse of 1, right?) and then with the same angle we increase the hypotenuse to our new velocity, say 7, the x and y components (the other sides of the triangle) will both also be multiplied by 7.

Or alternately you are resolving a velocity vector into its orthonormal basis vectors. Just to muddy the waters. This is a fairly common mathematical problem that many people have resolved in a lot of different ways -- all related all with a little something different.
• ###### 4. Re: Character Movement + Rotation
I've been trying to understand this for quite a while now and.............
I've finally UNDERSTOOD IT!
Thank you,
Now to get to the collision detection part for my car racing track which is another problem as flash only recognizes the boundary of my movie clip track. So no hitTest will work.
Chinmaya