Moving 2D through angles!
Chaps,
I have a 2D object(Over the top view), I wish to move it by the angle its facing, for instance 40 degrees.
How can i calculate its new coordinate on my x,y grid by its angle and its X and Y velocity? I am so rusty on all the this trigonometry maths!
Thanks for any help or guidence.
pete.
[edited by - BlackEye on August 8, 2003 5:26:40 AM]
For all points
xnew=x*cos(teta)+y*sin(teta);
ynew=-x*sin(teta)+y*cos(teta);
if you want to rotate around an arbitrary point(say t).
First subtract t from all points. Then rotate using the equation above.
Add t to all rotated points.
It is time consuming operation you should optimize it very much.
xnew=x*cos(teta)+y*sin(teta);
ynew=-x*sin(teta)+y*cos(teta);
if you want to rotate around an arbitrary point(say t).
First subtract t from all points. Then rotate using the equation above.
Add t to all rotated points.
It is time consuming operation you should optimize it very much.
newx = x + speed * cos(angle)
newy = y + speed * sin(angle)
Explanation:
x,y = object''s coordinates.
you can think them like a vector.
also you can think the object''s speed as vector:
speed is the distance(magnitude of the vector) the object will travel and angle is the direction of the vector.
so you want to convert the polar coordinates to cartesian and add them to the position vector of the object.
polar to cartesian : x = r * cos(theta),y = r *sin(theta)
so in speed * cos(angle) and speed * sin(angle) we convert the coordinates of the speed vector from polar to cartesian and we add them to the position vector of the object(x,y)
newy = y + speed * sin(angle)
Explanation:
x,y = object''s coordinates.
you can think them like a vector.
also you can think the object''s speed as vector:
speed is the distance(magnitude of the vector) the object will travel and angle is the direction of the vector.
so you want to convert the polar coordinates to cartesian and add them to the position vector of the object.
polar to cartesian : x = r * cos(theta),y = r *sin(theta)
so in speed * cos(angle) and speed * sin(angle) we convert the coordinates of the speed vector from polar to cartesian and we add them to the position vector of the object(x,y)
Thanks i think!
So a function would just look like this i guess?
CPitchObj::CalculatePosition()
{
m_X += m_Velocity * cos(angle)
m_Y += m_Velocity * sin(angle)
}
Do i need to work about different velocitys for my X and Y ?
I mean should i be side steping the object and / XOR moving forward? If so what are the changes to the above with 2 different Velocitys? Man i wish i got this math bit. Where is that GCSE Maths book!
So a function would just look like this i guess?
CPitchObj::CalculatePosition()
{
m_X += m_Velocity * cos(angle)
m_Y += m_Velocity * sin(angle)
}
Do i need to work about different velocitys for my X and Y ?
I mean should i be side steping the object and / XOR moving forward? If so what are the changes to the above with 2 different Velocitys? Man i wish i got this math bit. Where is that GCSE Maths book!
You need only one velocity that is the total distance the object will travel, the code you posted is correct.
quote:Original post by BlackEye
CPitchObj::CalculatePosition()
{
m_X += m_Velocity * cos(angle)
m_Y += m_Velocity * sin(angle)
}
Just one suggestion. If you want frame rate independence then pass in the time since the last frame and like this
CPitchObj::CalculatePosition( float deltaTime )
{
const float velThisFrame = deltaTime * mVelocity;
mY += velThisFrame * cos(angle);
mX += velThisFrame * sin(angle);
}
( Aside: Underscores are evil! )
If you want to move an object based on a certain angle in 2D you must find out how far to move the object on each axis, ie x and y.
"O" = Object to be moved.
"@" = Angle the object will be moving
"H" = Hypotenuse of a right triangle (or ship velocity)
"vy" = y component of velocity
"vx" = x component of velocity
| .
| /^
| H / |
| / | vy
| /@ |
| O---->
| vx
"EDITsorry about the picture I can't get it to look right)"
We know the velocity we want the ship fly at (or the hypotenuse of the triangle above) and we know the angle at which it will fly(@). We now need to find the vx and vy components so we can add them to the ship coordinates thereby moving the ship in the desired path.
From the diagram we end up will a right triangle. From the properties of a right triangle we know that the "vx side" is equal to cos(@) * H and the "vy side" is equal to sin(@) * H.
Once we have the "vx" and "vy" we simply add our position to the velocities (vx and vy) and our ship will move at the desired speed along the desired path.
vx = cos(@) * H;
vy = sin(@) * H;
x += vx;
y += vy;
I believe I got the math right there. If I have made mistakes I would appreciate the input.
[edited by - OneBitWonder on August 14, 2003 12:35:26 PM]
[edited by - OneBitWonder on August 14, 2003 12:37:19 PM]
ARGGH! I can't get the picture to look right!
[edited by - OneBitWonder on August 14, 2003 12:40:17 PM]
"O" = Object to be moved.
"@" = Angle the object will be moving
"H" = Hypotenuse of a right triangle (or ship velocity)
"vy" = y component of velocity
"vx" = x component of velocity
| .
| /^
| H / |
| / | vy
| /@ |
| O---->
| vx
"EDITsorry about the picture I can't get it to look right)"
We know the velocity we want the ship fly at (or the hypotenuse of the triangle above) and we know the angle at which it will fly(@). We now need to find the vx and vy components so we can add them to the ship coordinates thereby moving the ship in the desired path.
From the diagram we end up will a right triangle. From the properties of a right triangle we know that the "vx side" is equal to cos(@) * H and the "vy side" is equal to sin(@) * H.
Once we have the "vx" and "vy" we simply add our position to the velocities (vx and vy) and our ship will move at the desired speed along the desired path.
vx = cos(@) * H;
vy = sin(@) * H;
x += vx;
y += vy;
I believe I got the math right there. If I have made mistakes I would appreciate the input.
[edited by - OneBitWonder on August 14, 2003 12:35:26 PM]
[edited by - OneBitWonder on August 14, 2003 12:37:19 PM]
ARGGH! I can't get the picture to look right!
[edited by - OneBitWonder on August 14, 2003 12:40:17 PM]
Refer to picure here:
http://www.jjgifford.com/expressions/geometry/trigonometry.html
where vy = opposite, vx = adjacent, H = hypotenuse, @ = A
Tadd
- WarbleWare
http://www.jjgifford.com/expressions/geometry/trigonometry.html
where vy = opposite, vx = adjacent, H = hypotenuse, @ = A
Tadd
- WarbleWare
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement