ios - SpriteKit Jitters 并设置常量和 "per second"PhysicBody 速度

标签 ios iphone cocoa sprite-kit game-physics

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

最佳答案

速度是

速度是移动背景的速度 汽车速度 汽车速度 速度=5 车速=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);

//现在汽车以相同的方向和相同的增量时间移动

关于ios - SpriteKit Jitters 并设置常量和 "per second"PhysicBody 速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24402418/

相关文章:

objective-c - 在自动布局中,我设置了不应水平增长的标签和应水平增长的控件。为什么我会反其道而行之?

iphone - startMonitoringSignificantLocationChanges 在 iOS5 中不起作用

ios - 如何在 Google Analytics 仪表板上看到 'User timing'?

ios - 如果显示 View Controller ,状态栏不会隐藏

iphone - 在后台线程上加载 View

objective-c - NSWindow 的动画 setContent

ios - 如何在 ios 的 Xamarin 谷歌地图组件中处理点击事件

ios - 当我通过按钮操作从 TableView 中删除单元格时,应用程序崩溃了 - iOS Swift

iOS 共享扩展无法从 Chrome 获取共享 URL

android - 这么多移动设备