# [DirectX] Rotating a sprite relative to a fixed position

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

## Recommended Posts

I'm trying to rotate a textured quad like in the image below:

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.

##### Share on other sites
I solved the problem by subtracting the unit position from the cursor

moveTo = cursor - unit.pos

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634090
• Total Posts
3015431
×