精灵套件抖动并设置恒定且"per second"的物理体速度



我正在编写一个自上而下的汽车游戏,其中汽车通过在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帧!所以我必须找到第一个

的原因

其中速度

speed为背景移动速度汽车速度汽车速度速度= 5carspeed = 10;

if (_lastUpdateTime) {
        _deltaTime = interval - _lastUpdateTime;
    } else {
        _deltaTime = 0;
    }
    _lastUpdateTime = interval;
    CGPoint bgVelocity = CGPointMake(speed*(direction),speed*(direction));
    CGPoint bgVelocity1 = CGPointMake(carspeed*(direction), carspeed*(direction));
    CGPoint amtToMove = CGPointMake((int)bgVelocity.x * _deltaTime, bgVelocity.y * _deltaTime);
    CGPoint amtToMove1 = CGPointMake((int) bgVelocity1.x * _deltaTime, bgVelocity1.y * _deltaTime)
    self.position = CGPointMake((int)(self.position.x+amtToMove.x), self.position.y+amtToMove.y);
    car.position = CGPointMake((int)(self.position.x+ amtToMove1), self.position.y+ amtToMove1.y);

最新更新