Wednesday 15 July 2015

ios - Control an object by physics -



ios - Control an object by physics -

i read illustration in apple documentation (scene kit vehicle) , utilize scnphysicsvehicle on vehicle. scnphysicsvehicle allows set speed, brake , everything. want able command scnnode (containing scnsphere). way physic?

use scnphysicsvehicle when want element in scene behave wheeled vehicle — command in terms of engine speed, braking, , steering, , display wheels rotate moves , pivot steers.

for simpler physics-based command of element in scene, create sncphysicsbody , attach node want physical behaviors. then, set in motion, apply forces or impulses it, straight set velocity, set collide other bodies, or allow fall due scene's gravity. there's far many things physics fit in 1 reply — read scnphysicsbody class reference see them all.

if you're looking joystick/tilt control, there multiple ways go, depending on kind of gameplay "feel" you're looking for. there mutual themes:

since want continuous control, want poll input in renderer:didupdateattime:or 1 of other render-loop methods. tilt , joysticks provide variable input, want variable control. whatever physics body should scale accelerometer input or joystick axis value.

one thing apply forcefulness on every frame based on joystick direction; e.g. in update method:

gccontrolleraxisinput *joystickx = controllers[0].extendedgamepad.xaxis; [spherenode.physicsbody applyforce:scnvector3(xaxis.value * scale_factor, 0, 0) impulse:no];

with option, holding joystick 1 direction or other firing thrusters: sphere move faster , faster longer (and stronger) hold joystick in particular direction. , you'll have hold joystick much in opposite direction apply plenty opposing forcefulness stop crazy thing.

another way set velocity directly:

spherenode.physicsbody.velocity = scnvector3(xaxis.value * scale_factor, 0, 0);

with option, sphere holds still when you're not pushing stick, , moves faster farther force stick, maximum speed of scale_factor.

(in both examples, scale_factor translates -1 1 range of joystick units meaningful game.)

there loads of other options — example, own math derive delta between joystick position , current direction/velocity — experiment find out best fits gameplay you're looking for.

ios scenekit scnnode

No comments:

Post a Comment