As you can see below, the images of the little missiles are not over the lines. How can I create an offset that respects the angle and rotation. I am using linear interpolation to extract x y coordinates between the ends of each straight line.
float xDiff = end_xpos[i] - start_xpos[i];
float yDiff = end_ypos[i] - start_ypos[i];
double degrees = Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
double radians = (Math.PI / 180) * degrees;
args.DrawingSession.DrawLine(start_xpos[i], start_ypos[i], end_xpos[i], end_ypos[i], Color.FromArgb(Alpha_line, 255, 255, 255), 2);
pointX = Math.Round((start_xpos[i] + (end_xpos[i] - start_xpos[i]) * percent), 1);
pointY = Math.Round((start_ypos[i] + (end_ypos[i] - start_ypos[i]) * percent), 1);
Missle_ROT.TransformMatrix = Matrix3x2.CreateRotation((float)radians);
args.DrawingSession.DrawImage(Missle_ROT, Convert.ToSingle(pointX), Convert.ToSingle(pointY));
I think the problem is that you set the angle of your image like this
Missle_ROT.TransformMatrix = Matrix3x2.CreateRotation((float)radians);
.The coordinate system is like this:
So when you used Matrix3x2.CreateRotation Method (Single) to rotate the image, it rotated with the default center point which is also the left-top point of this image. As a solution you can use Matrix3x2.CreateRotation Method (Single, Vector2) to specify the rotation center, and for your scenario, this center should possible be the center of your image. You can have a test like this:
And for your last code to draw image
args.DrawingSession.DrawImage(Missle_ROT, Convert.ToSingle(pointX), Convert.ToSingle(pointY));
, it still draw the image from the the (0,0) point, you should also calculate the center point of the image to the "pointX" and "pointY" like this:You want to draw an image (of dimensions
w
,h
in pixels) on the screen, rotated with the center at specified pixel coordinatespx
andpy
. You need to find the location of the upper left corner of the image (with coordinatesx_A
andy_A
).This is done with a coordinate rotation of the relative location of the image center (
h/2
andw/2
).