【发布时间】:2014-06-25 07:15:10
【问题描述】:
我正在编写一个自上而下的汽车游戏,通过在 didSimulatePhysics 方法中设置速度来移动汽车。要设置速度,我使用使用此公式创建的参数:
CGVectorMake(JoyPadRelativePosition.x * maxSpeed,-JoyPadRelativePosition.y * maxSpeed);
JoypadRelativePosition 保存用户决定的加速量,maxSpeed 保存最大速度的恒定值。通过这种方式,我可以以正确的速度和正确的方向移动我的汽车。但是有时汽车或背景“抖动”,这是我的 didSimulatePhysics:
- (void)didSimulatePhysics
{
//moving the car
float dx=JoyPadRelativePosition.x*_maxSpeed;
float dy=JoyPadRelativePosition.y*_maxSpeed;
[self.car.physicsBody setVelocity: CGVectorMake(dx,-dy)];
//center the background to car position
CGPoint target = [self pointToCenterViewOn:self.car.position];
CGPoint newPosition = _worldNode.position;
newPosition.x += (target.x - _worldNode.position.x) * 0.1f;
newPosition.y += (target.y - _worldNode.position.y) * 0.1f;
_worldNode.position = newPosition;
}
我的想法是,考虑到每一帧的不同渲染时间,我必须移动汽车和背景,所以在我的更新方法中我实现了这个:
if (_lastUpdateTime) {
_dt = currentTime - _lastUpdateTime;
} else {
_dt = 0;
}
_lastUpdateTime = currentTime;
通过这种方式,我知道从最后一个渲染帧经过了多少毫秒,但是现在我可以如何考虑变量操纵杆值、最大速度和从最后一帧经过的毫秒数来设置我的汽车的速度?我认为这种计算可以消除抖动问题,但是如果有其他一些解决方案,如果消除抖动,我无论如何都会接受它......我想通过 SetVelocity 移动汽车,无论如何我试图通过 setPosition 移动它并且问题仍然存在。
编辑 我发现了一些 fps 下降...我正在寻找原因。我试图将帧速率阻止到一个较低的值,现在它更好了,但无论如何都会显示抖动......即使是 20 fps!所以我必须先找到这个的原因
【问题讨论】:
-
乘以增量时间不会消除抖动,事实上,如果您确实有一个波动的帧率,它可能会使事情变得更糟。首先检查抖动是否是由 fps 下降引起的,例如让非物理精灵使用移动动作以恒定和慢速向左/向右移动,并观察(在设备上!)这种移动是否平滑。如果不是,请尝试查找帧率下降的来源。如果它是平滑的,则抖动是由其他原因引起的,例如尝试在更新中更改速度:之前物理模拟运行
-
我根据您的建议编辑了我的问题
标签: ios iphone cocoa sprite-kit game-physics