Put*_*aKg 5 c# xna monogame windows-phone-8
当汽车撞到星星时,我想将星星(我的代码中的硬币)移动到屏幕的正确右上角.在每次更新期间,恒星和道路都以恒定速度向下移动.由于道路向下移动,汽车不会移动但似乎向上移动.虽然它可以根据用户的命令移动到左右车道.
所以我使用以下方法计算了屏幕的星形和右上角之间的角度
public double AngleBetween(Vector2 a, Vector2 b)
{
return Math.Atan2(b.Y - a.Y, b.X - a.X);
}
Run Code Online (Sandbox Code Playgroud)
在我的Update方法中,以下计算移动速度并将其发送到屏幕的右上角
double angleBetween = coin.AngleBetween(coin.Position, new
Vector2(currentGame.GraphicsDevice.Viewport.Bounds.Right, 0));
collidedCoinVelocity = new Vector2((float)Math.Sin(angleBetween),
-(float)Math.Cos(angleBetween));
Run Code Online (Sandbox Code Playgroud)
在我的Draw方法中,我更新了coin.Position使用
coin.Position += collidedCoinVelocity * 10 ;
Run Code Online (Sandbox Code Playgroud)
问题是星(硬币)没有像我想要的那样发送到右上角,但是它位于右侧屏幕中间的某个位置.
当星球在右侧车道上被击中时,它与右上角之间的角度始终是
1.2196048576751 radians = 69.878211 degree
Run Code Online (Sandbox Code Playgroud)
当恒星在左侧车道上时,角度为
0.952588487628243 radians = 54.5793 degree
Run Code Online (Sandbox Code Playgroud)
我正确地计算了角度,我错过了什么?也许我忘了考虑明星的向下运动?


编辑
我已更新图像以显示我正在尝试计算的角度并编辑我的问题以使其更清晰.
编辑2
添加了第二张图片,以显示被击中后星星的去向.
呃……仅供参考;从笛卡尔坐标转换为使用角度,然后再转换回笛卡尔坐标在这里没有任何意义。
只需像这样导出最终速度:
Vector2 direction = new Vector2(currentGame.GraphicsDevice.Viewport.Bounds.Right, 0) - coin.Position;
direction.Normalize();
Velocity = direction * 10;
Position += Velocity;
Run Code Online (Sandbox Code Playgroud)
还; 切勿更新绘图中的位置。Draw是为了绘图,不是为了更新。更新留在更新!
还有2;您应该概括您的代码。所有移动对象都应该继承相同的基础,其中包括速度、位置和加速度等内容,以及处理这些内容的代码。这样,您只需更改逻辑来操纵速度和/或加速度即可使物体移动。
移动对象.更新:
Velocity += Acceleration * deltaTime;
Positioin += Velocity * deltaTime;
Run Code Online (Sandbox Code Playgroud)
(deltaTime = 自上一帧以来的时间(以秒为单位),或 (float)gameTime.ElapsedGameTime.TotalSeconds)
然后,您只需在子类更新结束时使用 base.Update() ,只要您设置正确的值,位置和速度就会起作用:)。