1
\$\begingroup\$

How would I go by creating a realistic driving experience?

I'm using iOS Swift 3 with SpriteKit using the functions applyForce to accelerate when I click the gas button and when braking I add friction to the physicsBody, also I can't seem to turn right, no idea on how to do it.

Right now for turning I'm splitting the screen using the left and right to turn but I'm using applyForce but its very bad because it turns when the car is stopped and in a very unrealistic way.

When I apply force its only to go up so if I do create a turning mechanism and I do a uturn the car will still go up.

Also side note: Multitouch doesn't work?

Any help? Thanks

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
    }

  }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.