Jump to content
  • Advertisement
Sign in to follow this  
DarioMicera

[DirectX] Rotating a sprite relative to a fixed position

This topic is 2440 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'm trying to rotate a textured quad like in the image below:


screen1tn.jpg



This is the code i'm using to rotate the arrow:

//copies the current square position to a new vector
moveTo = cursor;


//these 2 floats store the magnitude of each vector
part1 = D3DXVec3Length(&unit.pos);
part2 = D3DXVec3Length(&moveTo);

//tried to use both these formulae to calculate the angle
//angle = acos((D3DXVec3Dot(&unit.pos,&moveTo)/(part1 * part2)));
angle = atan2(moveTo.y,moveTo.x)- atan2(unit.pos.y,unit.pos.x);

D3DXMATRIX center, rotate, reposition;
//move to 0,0 -> Rotate -> reposition
D3DXMatrixTranslation(&center,-unit.pos.x-16,-unit.pos.y,0.0f);
D3DXMatrixRotationZ(&rotate,angle);
D3DXMatrixTranslation(&reposition,unit.pos.x+16,unit.pos.y,0);
D3DXMATRIX world;
world = center *rotate * reposition;
dev->SetTransform(D3DTS_WORLD,&world);

//just draws the primitive with the selected Texture

primitive.DrawPrimitive(dev,Arrow1,12);



The problem is that the arrow rotates, ofc, relative to the origin instead of the position of the tank, so that if the arrow would be perpendicular to the tank, it is not. I'm probably missing something in my calculation, but i cannot figure out what's missin so that i can modify the behaviour of the rotation relative to the Tank.

The tanks are Dynamically created during the game.

Thanks in advance

Share this post


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