# 3d vector rotation

I'm implementing my own 3d vector class.
I have a rotate method like this:

[code]
//Rotates the vector around the origin and by the selected angles (in radians)
void CVector3D::Rotate(double rotx, double roty, double rotz)
{
if(rotx)
{
y = (double)(y*cos(rotx) - z*sin(rotx));
z = (double)(y*sin(rotx) + z*cos(rotx));
}
if(roty)
{
x = (double)(x*cos(roty) + z*sin(roty));
z = (double)(- x*sin(roty) + z*cos(roty));
}
if(rotz)
{
x = (double)(x*cos(rotz) - y*sin(rotz));
y = (double)(x*sin(rotz) + y*cos(rotz));
}
}

[/code]

but it doesn't work...
Anybody can find the error???

And does anybody know if there's a function to rotate a D3DXVECTOR3 ???

Thank you.
nICO

[hr]
[italic]How beautiful, if sorrow had not made Sorrow more beautiful than Beauty itself.[/italic]
JOHN KEATS

• I found the error myself (and it was a quite stupid one).

Doing this:
[code]
if(rotx)
{
y = (double)(y*cos(rotx) - z*sin(rotx));
z = (double)(y*sin(rotx) + z*cos(rotx));
}
[/code]

I change the value of y and then use the new value to update z... (the same for the other axis).

I just needed to store the result in another variable!

bye
nICO

[hr]
[italic]How beautiful, if sorrow had not made Sorrow more beautiful than Beauty itself.[/italic]
JOHN KEATS

• Fayetteville, NC, USA
I'm at this point in my world editor now, and wanted to ask you what exactly you changed. I'm having a bit of trouble with it myself (yeah I know the object has to be translated to 0,0,0 before rotation). I'd be happy to share the function I wrote that rotates the objects upon user request if you'd like.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

• : I'm at this point in my world editor now, and wanted to ask you what exactly you changed. I'm having a bit of trouble with it myself (yeah I know the object has to be translated to 0,0,0 before rotation). I'd be happy to share the function I wrote that rotates the objects upon user request if you'd like.
:
: -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
:
:
This is the function I wrote that works.

[code]
void CVector3D::Rotate(double rotx, double roty, double rotz)
{
CVector3D result(x, y, z);

if (rotx)
{
result.y += (double)(y*cos(rotx) - z*sin(rotx));
result.z += (double)(y*sin(rotx) + z*cos(rotx));
}
if (roty)
{
result.x += (double)(x*cos(roty) + z*sin(roty));
result.z += (double)(- x*sin(roty) + z*cos(roty));
}
if (rotz)
{
result.x += (double)(x*cos(rotz) - y*sin(rotz));
result.y += (double)(x*sin(rotz) + y*cos(rotz));
}

*this = result;
}
[/code]

btw, I've now abandoned this class, because I found that using matrices and D3DVECTOR saves a lot of time; you can try using D3DX functions for matrices, that are very useful and are optimized.

nICO

[hr]
[italic]How beautiful, if sorrow had not made Sorrow more beautiful than Beauty itself.[/italic]
JOHN KEATS

• Fayetteville, NC, USA
I can't use D3D structures or classes because I am using OpenGL as a renderer. Based on various commercial game tests, I find GL to be much faster, though not nearly as compatible as D3D. Thank you for the code though, and if you would like to see it implemented in an OpenGL application, I can email you the world editor once I get it to the point of solid object manipulation. It'll weigh in around 200kb.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

• Ok, I wait for your editor!

bye
nICO

[hr]
[italic]How beautiful, if sorrow had not made Sorrow more beautiful than Beauty itself.[/italic]
JOHN KEATS

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!