Interactive Forces
Create user-controlled physics interactions:
// Add force on key press
scene.onKeyboardObservable.add((kbInfo) => {
if (kbInfo.type === BABYLON.KeyboardEventTypes.KEYDOWN) {
switch (kbInfo.event.key) {
case "ArrowUp":
sphere.physicsImpostor.applyImpulse(
new BABYLON.Vector3(0, 0, -10),
sphere.getAbsolutePosition()
);
break;
case "ArrowDown":
sphere.physicsImpostor.applyImpulse(
new BABYLON.Vector3(0, 0, 10),
sphere.getAbsolutePosition()
);
break;
case "ArrowLeft":
sphere.physicsImpostor.applyImpulse(
new BABYLON.Vector3(-10, 0, 0),
sphere.getAbsolutePosition()
);
break;
case "ArrowRight":
sphere.physicsImpostor.applyImpulse(
new BABYLON.Vector3(10, 0, 0),
sphere.getAbsolutePosition()
);
break;
case " ": // Space key
sphere.physicsImpostor.applyImpulse(
new BABYLON.Vector3(0, 10, 0), // Jump
sphere.getAbsolutePosition()
);
break;
}
}
});