rotating a 2d vector in directx

Jun 10, 2010 at 4:08pm
I'm making a 2d game in directx and need a function to rotate a 2d vector(D3DXVECTOR2). There are directx functions for rotating 3d vectors but I couldnt find any for 2d vector rotation. I dont know much about the mathematical side of programming.

Thanks.
Jun 10, 2010 at 9:00pm
closed account (D80DSL3A)
I am learning direct3D 9 now for the same purpose (writing 2D games)!! It looks to me (so far) that the whole thing is set up for 3D. I am planning to use 3D vectors but just work in the x-y plane only. That is, all z-components will = 0.0. That said though, if there are no transformation functions supplied for rotating 2D vectors then write your own! I just did this as an exercise. The result compiles (no build errors). Here is my function to transform a 2D vectors components under rotation.
Declaration
void rotate2Dvector(D3DXVECTOR2* pV2, float angle);
Definition
1
2
3
4
5
6
7
8
9
10
11
void rotate2Dvector(D3DXVECTOR2* pV2, float angle)
{
	// use local variables to find transformed components
	float Vx1 = cosf(angle)*pV2->x - sinf(angle)*pV2->y;
	float Vy1 = sinf(angle)*pV2->x + cosf(angle)*pV2->y;
	// store results thru the pointer
	pV2->x = Vx1;
	pV2->y = Vy1;

	return;
}

Implementation (inside some function)
1
2
3
D3DXVECTOR2 V2(1.0,2.0);
	float angle = D3DX_PI/4;// 45 degrees in radian measure
	rotate2Dvector(&V2,angle);


I tested this by using it to give the x-y components of a translation for a spinning triangle being drawn in one of the SDK examples. It works nicely there (the triangle now spins and goes around in a circle) so I think the transformation in the function is good.
Last edited on Jun 10, 2010 at 9:34pm
Topic archived. No new replies allowed.