旋转精灵上的Vector2
问题描述:
我正在做一个小行星克隆,激光需要射出船的前方,但是当我尝试使用旋转矩阵旋转矢量时,它会失灵,在屏幕上飞行,我需要激光从船的前方射击,并将原点留在船上360度。目前它只能以90度的角度直射,当时该船面向东方。旋转精灵上的Vector2
这里是我的时刻:
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));
包括一些图片,以更好地解释我的问题
答
您正在使用Vector2.Transform()错误。 第一个参数是你想用Matrix转换的“参考”向量。如果你想让函数返回激光起始位置的位置,你需要给Vector2(shipWidth/2f,0)作为参数。
所以:
Vector2 laserStart = vPlayerPosition + Vector2.Transform(new Vector2(shipWidth/2f, 0), Matrix.CreateRotationZ(angle));
然后你就可以开始从这个位置绘制你的激光。