【问题标题】:Swift SpriteKit: Creating a realistic driving experience 2DSwift SpriteKit:创建逼真的 2D 驾驶体验
【发布时间】:2017-07-17 17:41:55
【问题描述】:

我将如何创造逼真的驾驶体验?

我正在使用带有 SpriteKit 的 iOS Swift 3,当我单击油门按钮时,我使用 applyForce 函数来加速,当我在制动时给物理体增加摩擦力,我似乎​​也无法右转,不知道该怎么做它。

现在转向我正在使用左右分开屏幕来转向,但我正在使用 applyForce 但它非常糟糕,因为它会在汽车停止时以非常不切实际的方式转向。

当我施加力时它只会上升,所以如果我确实创建了一个转向机制并且我做了一个 uturn,那么汽车仍然会上升。

另附注:多点触控不起作用?

有什么帮助吗?谢谢

override func didMove(to view: SKView) {

    // Multi Touch
    self.view?.isMultipleTouchEnabled = true

    car.carNode.position = CGPoint(x: 0, y: 0)
    car.carNode.physicsBody = SKPhysicsBody(rectangleOf: car.carNode.size)
    car.carNode.physicsBody?.affectedByGravity = false
    car.carNode.physicsBody?.angularDamping = 0.1
    car.carNode.physicsBody?.linearDamping = 0.1
    car.carNode.physicsBody?.friction = 0.1
    car.carNode.physicsBody?.mass = 1

    self.addChild(car.carNode)

    // HUD Display
    gas.gasButton.position = CGPoint(x: 300, y: -500)
    self.addChild(gas.gasButton)

    brake.brakeButton.position = CGPoint(x: 150, y: -500)
    self.addChild(brake.brakeButton)

}

override func update(_ currentTime: TimeInterval) {

if touching {

    car.carNode.physicsBody?.applyForce(CGVector(dx: 0, dy: 180))
  }
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

for touch in touches {
    let location = touch.location(in: self)

    if location.x < self.frame.size.width / 2 {

        // Left side of the screen
        car.carNode.physicsBody?.applyForce(CGVector(dx: -100, dy: 0))

    } else {

        // Right side of the screen
        car.carNode.physicsBody?.applyForce(CGVector(dx: 100, dy: 0))
    }

    // Gas Button
    if (gas.gasButton.contains(location)) {

        touching = true
    }
        // Brake Button
    else if (brake.brakeButton.contains(location)) {

        car.carNode.physicsBody?.friction = 1
    }

  }
}

【问题讨论】:

  • 你可能想把它带到physics.stackexchange.com 并找出它背后的原理,然后将它应用到你的代码中。
  • 好的,这张图片可以帮助你了解你的 2D 游戏。试试let velY = car.carNode.physicsBody?.velocity.dy car.carNode.physicsBody?.applyForce(CGVector(dx: 100 * velY, dy: 0))
  • 计算你的y速度,如果你的车Y速度为零,你的车不转弯,显然速度越大,转向力越大。您可以构建一个函数来将此力限制为最大值。完成此检查后,最好在汽车前部使用 applyAngularImpulse 而不是 applyForce 来转动您的汽车
  • 这是一个非常有趣的物理引擎利用设计问题。谁能想到一个论坛,我们可以就创建“解决方案”的不同方式及其各种妥协和好处交换意见?
  • @BroSimple 这是解决问题的一种方法...iforce2d.net/b2dtut/top-down-car

标签: ios swift sprite-kit


【解决方案1】:

评论整合

for touch in touches {
    let location = touch.location(in: self)

    let velY = car.carNode.physicsBody?.velocity.dy
    let factor:CGFloat = 100
    let maxFactor:CGFloat = 300
    //limit
    let dxCalc = factor * velY > maxFactor ? maxFactor : factor * velY

    if location.x < self.frame.size.width / 2 {

        // Left side of the screen
       car.carNode.physicsBody?.applyForce(CGVector(dx: -dxCalc, dy: 0))

     } else {

        // Right side of the screen
        car.carNode.physicsBody?.applyForce(CGVector(dx: dxCalc, dy: 0))
    }

    // Gas Button
    //etc

} }

【讨论】:

  • 谢谢!我得到一个错误说Binary operator '*' cannot be applied to operands of type 'Int' and 'CGFloat'?
  • 好的,在继续之前您绝对需要阅读 SWIFT 书籍!简单更改为 CGFloat,我会更新....
  • 是的,对不起,我刚开始通过随机项目而不是书来学习 Swift,但我会开始阅读,而且当你对 applyAngularImpulse 说所以汽车转向看起来很真实时,我只是做同样的事情你在这里用这段代码做的吗?因为AngularImpulse 发疯了
  • 在应用 angularImpulse 之前,你必须改变汽车的锚点,并尝试给阵风一点冲动,比如 factor = 1 或 10
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-23
  • 2017-12-02
  • 1970-01-01
  • 2013-06-19
相关资源
最近更新 更多