旋转固定在精灵上的 Vector2

Rotate a Vector2 fixated on a sprite

我正在制作一个小行星克隆体,激光需要射出飞船的前部,但是当我尝试使用旋转矩阵旋转矢量时,它变得混乱,飞过整个屏幕,我需要激光从船头拍摄,并让原点与船保持 360 度全方位。目前它只能以 90 度角直射,当船面向正东时。

这是我目前拥有的:

lLasers.Add(new Laser(Vector2.Transform(new Vector2((vPlayerPosition.X + 35), (vPlayerPosition.Y)), Matrix.CreateRotationZ(angle))));

角度为

Vector2 direction = mouseLoc - vPlayerPosition;
angle = (float)(Math.Atan2(direction.Y, direction.X));

包含一些图片以更好地解释我的问题

Origin in Bottom Left Corner

Shooting Straight at 90 degrees

您使用的 Vector2.Transform() 有误。 第一个参数是要用矩阵转换的 "reference" 向量。

在你的例子中,如果你想让函数return激光起始位置的位置,你需要给Vector2(shipWidth / 2f, 0)作为参数。

所以 :

Vector2 laserStart = vPlayerPosition + Vector2.Transform(new Vector2(shipWidth / 2f, 0), Matrix.CreateRotationZ(angle));

然后你就可以从这个位置开始画你的激光了。