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