为什么我的(尽量现实)2D 汽车物理计算 (C#) 不准确?

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

我正在尝试用 C# 制作具有逼真的物理特性的 2D 汽车游戏。框架给出了,现在我们只关注运动的计算。

使用当前代码,汽车正在以某种方式超速转弯。

第一个我解决不了的问题好像是车子走的很颠簸,前行的时候来回弹跳。我想知道这是否可能是由浮点计算引起的,但我不确定,即使计算也可能是错误的。

float angle = (float)Math.Atan2(direction.Y, direction.X);

Vector2 motion = new Vector2(
    (float)(speedMS * Math.Cos(angle - Math.PI / 2.0)),
    (float)(speedMS * Math.Sin(angle - Math.PI / 2.0))) * (float)timeBetweenFrames;

第二个问题是,转向后,汽车有点滑动,就像在冰上行驶一样,而且它也没有朝着它应该面对的确切方向移动(偏离一些角度)。

angularVelocity = (float)(speedMS / direction.Length());
turningRadius = (float)(speedMS / angularVelocity);

Vector2 direction... //given direction vector
float angleChange = (float)(speedMS * timeBetweenFrames) / turningRadius;
newRotation = (float)(angle - angleChange) * 180.0f / (float)Math.PI;

Vector2 newPosition = new Vector2(X, Y) + motion;

我尝试了几个角度和运动矢量计算的变化,但没有任何帮助。

提前致谢。

c# game-physics game-development physics-engine
© www.soinside.com 2019 - 2024. All rights reserved.