Rotating A Vector Around Another Vector


hessiess

Member
Joined
Apr 26, 2008
Messages
219
Say I have a 2D vector 'C' which is located at (0,0), and a second 2D vector 'O' which should 'orbit' around 'C' at angle 'A' and at a distance of 'D'.

How would this be implemented? Preferably without using matrices, as they go strait over my head :(.

Thanks.
 
In 2D, a parametric equation somewhere along the lines of:

y=a + m*sin(T)
x=b + n*cos(T)

(a, b ) should be the coordinates of C, m and n being the distances (allows for elliptical orbits).

There are similar formulae for 3D, but I can't remember them off the top of my head.

EDIT: stopped b ) becoming a smiley face
 
Phew... I saw the thread title and thought I was gonna have to explain quaternions, but thankfully you wanted 2D. 3D rotation around an arbitrary axis is much more of a pain :/
 
sinoth said:
Phew... I saw the thread title and thought I was gonna have to explain quaternions, but thankfully you wanted 2D. 3D rotation around an arbitrary axis is much more of a pain :/
That's why im sticking to 2D for now, 3D maths is just so much more complicated. Im only just starting to understand 2D vector maths...
 
Last edited by a moderator:
B-ZaR said:
For all your coordinate system transformation needs:
http://en.wikipedia.org/wiki/List_of_canon...transformations


awwww I remember having to look at that same wikipedia page... I just don't remember what subject I was looking at that for (linear algebra, physics, calculus... many areas use them) :(
anyway, don't feel scared of 3D just because of that. your game idea is 2D, so no problem of doing it like that :) good luck
 
Last edited by a moderator:
Im having some issues with my rotation code:

CODE
bool dirswitch = false;
for(int vert = 0; vert < 4; vert ++)
{
// calculate the distance between the vert and the
// center of the quad
vector2d_f vert_offs;
vert_offs.X = 0 - data .vertices[vert].X;
vert_offs.Y = 0 - data .vertices[vert].Y;

// calculate rotated vertex location
vertex new_vert;

// fix problem with the veracities at opposite corners rotating in opposite directions
if(dirswitch == false)
{
dirswitch = true;
new_vert.X = vert_offs.X * cos(rotation *
(3.142/180));
new_vert.Y = vert_offs.Y * sin(rotation *
(3.142/180));
}
else
{
dirswitch = false;
new_vert.X = vert_offs.X * sin(rotation *
(3.142/180));
new_vert.Y = vert_offs.Y * cos(rotation *
(3.142/180));
}
// apply
data .vertices[vert] = new_vert;
}



If this is applied to a square, it works correctilly, however if it is applied to a rectangle

rotate_problem_1.jpg


rotating by 45 degrees gives this, which is obviously wrong.
rotate_problem_2.jpg


Thanks.
 
I don't think that's right. For rotating a point around the origin, try something like:
CODE
vector2d_f rotate_point(vector2d_f old_point, float rotation)
{
vector2d_f new_point;
new_point.X = old_point.X * cos(rotation) - old_point.Y * sin(rotation);
new_point.Y = old_point.X * sin(rotation) + old_point.Y * cos(rotation);
return new_point;
}

(from http://en.wikipedia.org/wiki/Rotation_(mathematics) )
 
sinoth said:
Phew... I saw the thread title and thought I was gonna have to explain quaternions, but thankfully you wanted 2D. 3D rotation around an arbitrary axis is much more of a pain :/
Yeah, I did the same sort of double-take. "Evil" doesn't even begin to describe that sort of computational task... ;)
 
Last edited by a moderator:
Actually, I believe there is a formula I learnt last year that makes orbits in 3D a lot simpler (although I never tried using it when programming ). When I can get to them, I'll have a dig through my old notes and see if I can find it.
 
Multiplex said:
I don't think that's right. For rotating a point around the origin, try something like:
CODE
vector2d_f rotate_point(vector2d_f old_point, float rotation)
{
vector2d_f new_point;
new_point.X = old_point.X * cos(rotation) - old_point.Y * sin(rotation);
new_point.Y = old_point.X * sin(rotation) + old_point.Y * cos(rotation);
return new_point;
}

(from http://en.wikipedia.org/wiki/Rotation_(mathematics) )


Thanks allot, workers perfectly, and in vastly fewer lines of code. Though exactly how i managed to miss that wiki page while looking for a fix I honestly don't know.

QUOTE
Actually, I believe there is a formula I learnt last year that makes orbits in 3D a lot simpler (although I never tried using it when programming ). When I can get to them, I'll have a dig through my old notes and see if I can find it.


Sounds interesting, 3D that isn't dreadfully complicated :)

Is there any way to mark this topic as solved?
 
Last edited by a moderator:
Back
Top