Jump to content
  • Advertisement
Sign in to follow this  
Zaerdna

[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.

If you intended to correct an error in the post then please contact us.

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.


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.

Share this post


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

Share this post


Link to post
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 this post


Link to post
Share on other sites

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



Thank you mate, that worked 100% laugh.gif

Share this post


Link to post
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 this post


Link to post
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 laugh.gif

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!