0

I read the example in the apple documentation (Scene Kit Vehicle) and they use SCNPhysicsVehicle on the vehicle. SCNPhysicsVehicle allows to set speed, brake and everything. I want to be able to control a SCNNode (containing a SCNSphere). What is the way to do that by physic?

Abizern
  • 146,289
  • 39
  • 203
  • 257
mathieug
  • 901
  • 1
  • 11
  • 24
  • That sample code creates an `SCNPhysicsVehicle` from the physics body attached to the car node. If you want to simulate a vehicle with a node containing a sphere, attach a physics body to that node and create an `SCNPhysicsVehicle` from that node (the same way the sample code does for its car node). Or is this not what you're asking? – rickster Oct 30 '14 at 18:03
  • @rickster I don't think my sphere is a vehicle, is it? – mathieug Oct 30 '14 at 18:07
  • A vehicle is whatever you make it. :) Do you want your sphere to act like a vehicle — that is, be controllable in terms of engine speed, steering, and braking? If so, use `SCNPhysicsVehicle`. Otherwise, just give it a physics body, and you can control it by applying forces, setting velocity directly, etc. – rickster Oct 30 '14 at 18:20
  • @rickster I don't think `SCNPhysicsVehicle` fits to my case :). How do you apply force, velocity, etc? I mean how do I link my joystick/accelerometer to the physic of my sphere? In the update? – mathieug Oct 30 '14 at 18:34

1 Answers1

1

Use SCNPhysicsVehicle only when you want an element in your scene to behave like a wheeled vehicle — to control it in terms of engine speed, braking, and steering, and to display it with wheels that rotate as it moves and pivot as it steers.

For simpler physics-based control of an element in your scene, create an SNCPhysicsBody and attach it to the node for which you want physical behaviors. Then, to set it in motion, apply forces or impulses to it, directly set its velocity, set it up to collide with other bodies, or just let it fall due to the scene's gravity. There's far too many things to do with physics to fit in one answer — read the SCNPhysicsBody Class Reference to see them all.

If you're looking specifically for joystick/tilt control, even then there are multiple ways to go, depending on what kind of gameplay "feel" you're looking for. But there are some common themes:

One thing you could do is apply a force on every frame based on the joystick direction; e.g. in your update method:

GCControllerAxisInput *joystickX = controllers[0].extendedGamepad.xAxis;
[sphereNode.physicsBody applyForce:SCNVector3(xAxis.value * SCALE_FACTOR, 0, 0) impulse:NO];

With this option, holding the joystick one direction or the other is like firing thrusters: your sphere will move faster and faster the longer (and stronger) you hold the joystick in a particular direction. And you'll have to hold the joystick just as much in the opposite direction to apply enough opposing force to stop that crazy thing.

Another way to do it would be to set velocity directly:

sphereNode.physicsBody.velocity = SCNVector3(xAxis.value * SCALE_FACTOR, 0, 0);

With this option, the sphere holds still when you're not pushing the stick, and it moves faster the farther you push the stick, up to the maximum speed of SCALE_FACTOR.

(In both examples, SCALE_FACTOR is something that translates the -1 to 1 range of the joystick into units meaningful to your game.)

There are loads of other options — for example, you could do your own math to derive a delta between the joystick position and the current direction/velocity — experiment with some to find out what best fits the gameplay you're looking for.

rickster
  • 124,678
  • 26
  • 272
  • 326
  • Your answer is still great. Do you know how to define a maximum? I guess I can compute it with the sphere's speed? – mathieug Oct 01 '15 at 22:53