[Solved!]Rotating sprite towards mouse.

Started by
5 comments, last by Zaerdna 13 years, 3 months ago
I need help rotating my arm sprite towards the mouse coordinates.
My mouse coordinates are set to be POINT MousePos

Here is my transformation code:

static float rotation;
D3DXVECTOR2 spriteCentre=D3DXVECTOR2(2.0f,5.0f);
D3DXMatrixTransformation2D(&mat,NULL,0.0,&scaling,&spriteCentre,rotation,&trans);

float Rotation is how much the sprite will rotate, in radian.


Please help me out on this one.

PS:If you don't know this code, you can write your own.


--------------------------------
C++ DirectX9.
MSVC++ 2010 Express.
Windows 7.
-Zaerdna
Advertisement
I'm not expert in this but I think you have to explain better what you are trying to accomplish.

You wan't a sprite to point towards the mouse pointer?

If that is the case you would probably need a algorithm to calculate it. It would depend on the location of the sprite as much as of the mouse pointer.

Let´s say the rotation 0 is straight up then that should be the case when it has the same X as mouse and Y is bigger.

The math here would be, assuming:

pos is position of sprite
mouse is position of mouse

DeltaY = mouse.Y - pos.Y
DeltaX = mouse.X - pos.X

rotation = tan^-1(DeltaY / DeltaX)

This is basic and I might be off.

I'm not expert in this but I think you have to explain better what you are trying to accomplish.

You wan't a sprite to point towards the mouse pointer?

If that is the case you would probably need a algorithm to calculate it. It would depend on the location of the sprite as much as of the mouse pointer.

Let´s say the rotation 0 is straight up then that should be the case when it has the same X as mouse and Y is bigger.

The math here would be, assuming:

pos is position of sprite
mouse is position of mouse

DeltaY = mouse.Y - pos.Y
DeltaX = mouse.X - pos.X

rotation = tan^-1(DeltaY / DeltaX)

This is basic and I might be off.



I'll explain better:

my character's arm is an individual sprite, I want it to rotate towards the mouse, so when you move the mouse the arm follows so that you are able to aim.
I tried to do

DeltaY = MousePos.y - pos_y;
DeltaX = MousePos.x - pos_x;
rotation = tan^-1(DeltaY / DeltaX);


But got the error "Expression must have (pointer-to-) function type" and "term does not evaluate to a function taking 1 arguments"
This was for the "-1" in this line "rotation = tan^-1(DeltaY / DeltaX);"

-Zaerdna
If your trying to use an inverse tangent function, try atan2( DeltaY, DeltaX).

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


If your trying to use an inverse tangent function, try atan2( DeltaY, DeltaX).



Thank you mate, that worked 100% laugh.gif
-Zaerdna


But got the error "Expression must have (pointer-to-) function type" and "term does not evaluate to a function taking 1 arguments"
This was for the "-1" in this line "rotation = tan^-1(DeltaY / DeltaX);"



My example was just pure math not code syntax
Buckeye has already explained the syntax you would need. Did it otherwise work as I explained? I wasn't sure just a rough guess.



'Zaerdna' said:

But got the error "Expression must have (pointer-to-) function type" and "term does not evaluate to a function taking 1 arguments"
This was for the "-1" in this line "rotation = tan^-1(DeltaY / DeltaX);"



My example was just pure math not code syntax
Buckeye has already explained the syntax you would need. Did it otherwise work as I explained? I wasn't sure just a rough guess.




Yea, it was basically like you said laugh.gif
-Zaerdna

This topic is closed to new replies.

Advertisement