[Solved!]Rotating sprite towards mouse.

This topic is 2804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

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

--------------------------------
C++ DirectX9.
MSVC++ 2010 Express.
Windows 7.

Share on other sites
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.

Share on other sites

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);"

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

Share on other sites

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

Thank you mate, that worked 100%

Share on other sites

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.

Share on other sites

[quote name='Zaerdna' timestamp='1294972803' post='4758630']
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.

[/quote]
Yea, it was basically like you said

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 11
• 9
• Forum Statistics

• Total Topics
632563
• Total Posts
3007103

×