0
votes

I'm writing a little test project. I have an object (with a position and bounding box) at an origin, and when something happens (say a mouse click/touch on phone), I want a line to be drawn from the origin object to the point.

Using a texture, I realise I'm going to have to use the rotation here, but have no idea how to work out how much to rotate the texture by. Any help would be appreciated.

So far, I have:

Vector2 Origin Vector2 TouchPoint

and that's about it.

Thanks all!

1
You can get more answers in gamedev.stackexchange.comRicardo Souza

1 Answers

0
votes

Theres a simple formula for calculating an angle based on the X and Y coordinates:

float angle = Math.Atan2(TouchPoint.Y - Origin.Y, TouchPoint.X - Origin.X);

You can use this angle in an overload of the SpriteBatch.Draw() that accept an angle for the rotation.

See this for reference: http://msdn.microsoft.com/en-us/library/ff433992.aspx

You may want to convert between degrees and radians:

float rad = deg * Math.PI/180;

float deg = rad * 180/Math.PI;