敌舰向玩家旋转但没有锁定到位

问题描述 投票:0回答:1

当我的敌舰向玩家移动时,我发现它们之间的距离

private float FindDistance(Vector2 heroCenter, Vector2 spritePos)
{
    var deltaX = Math.Pow((spritePos.X - heroCenter.X), 2); // the 2 is the power value (into the power of 2)
    var deltaY = Math.Pow((spritePos.Y - heroCenter.Y), 2);
    float distance = (float)Math.Sqrt(deltaX + deltaY);
    return distance; // returns the distance between the two objects
}

然后用这段代码计算敌舰的旋转角度

distanceFromHero = FindDistance(Constants.heroCenter, spritePos);
if (distanceFromHero < Constants.HeroWithinRange)
{
    heroClose = true;
    VelocityX *= .985f; // enemy reduces vel X
    VelocityY *= .985f; // enemy reduces vel Y
    //atan2 for angle
    var radians = Math.Atan2((spritePos.Y - Constants.heroCenter.Y), (spritePos.X - Constants.heroCenter.X));
    //radians into degrees
    rotationAngle = (float)(radians * (180 / Math.PI));
}
else
{
    heroClose = false;
}

但奇怪的是,敌人虽然向着玩家移动并没有锁定并且保持稳定,但是像运动一样摆动,当他们在同一点时敌人的轮子无休止地旋转。一些代码帮助会有所帮助。

c# monogame
1个回答
0
投票

我在XBox论坛中找到的问题的完美答案无法加载到Visual Studio 2017中,所以我提出了一个新的解决方案。希望其他人可以从这个由微软制作的优秀教程中受益。解决方案是here.

© www.soinside.com 2019 - 2024. All rights reserved.