How to translate a physic object

Hi everybody,

I want to move a boxMesh with applied cannon.js imposters on key down.
The result i want to achieve is that the box is moving into the direction it is facing. (like a simple car/character movement).
Playground example: https://www.babylonjs-playground.com/#2CRZWS#3

Without the applied imposters i would go with position and rotation but this attributes seem to be overwritten by the physic engine.
Enclosed a basic playground setup where i try to achieve that effect: Babylon.js Playground

Can anyone point me into the right direction?
I am more the three guy… so first time Babylon and i struggle with the physic API a lil… nevertheless it looks like a really nice framework. Very cool… Thx a lot.

Hello there,

With the physics engine enabled, you need to think in terms op applying impulses/forces to the player Mesh (typically a box or cylinder impostor). Also the rotation property is skipped and instead the rotationQuaternion property of the Mesh is used.

There’s some details about this in the Learning Babylon.js book (Chapter 10):


Concerning controlling the player character with the Physics Engine.

And luckily also in the official docs:
https://doc.babylonjs.com/how_to/using_the_physics_engine
https://doc.babylonjs.com/how_to/forces

I hope this information helped you a bit further. Good luck! :slight_smile:

Q

2 Likes

https://playground.babylonjs.com/#9Q08R4#3

Here is a little demo!

I used the key events from your first link to turn on and off variables for smoother movement.
mf = move forward,
mb = move backward,
rl = rotate left,
rr = rotate right.

The 3 main functions are transformForce at line 34, translate at line 49, and rotate at line 40. All three are needed.

In the register before render loop I check to see if the previous mf, mb, rl, or rr booleans are true or false, if true, they move or rotate the box.

How to use transform:
transform(mesh, new BABYLON.Vector3(x, y, z), power);
The vector3 determines the direction of push, with (0, 1, 0) being up, (0, 0, 1) being forward, and so on.

How to use rotate:
rotate(mesh, new BABYLON.Vector3(x, y, z), power);
The vector 3 determins the spin direction.

All things can be swapped out for variables.

The functions are courtesy of @Deltakosh and made portable by @Wingnut

Hope these help!

Givo

2 Likes

Hey thank you guys a lot that helps me to get going. :+1:

1 Like

no problem.

Good thing those function can be helpful for other people too! :slightly_smiling_face:

To close that question
I reached the desired effect by checking out the inearDamping and angularDamping to get more control over the mesh movement.

Again thx guys for that fast support.:ok_hand:

1 Like

Yeah, when I was using the functions for movement I had really high numbers for both.
moju stands for more jump
44%20AM

I can (try to) help at any time. Glad it worked out.:+1:

I did it this way, however there are problems with collisions.
the property to move a physical body is "_deltaPosition"

var suelo = scene.getNodeByName("suelo");
var sueloFisico = new BABYLON.PhysicsImpostor(suelo , BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, friction: 0, restitution: 0 }, scene);
var posicionFIsicaCOlumna = -0.1;

//UPDATE
scene.onBeforeRenderObservable.add(() => {
    posicionFIsicaCOlumna -=0.01;
    sueloFisico._deltaPosition = new BABYLON.Vector3(0,-0.2122,-5 + posicionFIsicaCOlumna);
})

not exactly. Set the mesh’s position, and the physics body’s position will update as well. So in your case:

var suelo = scene.getNodeByName("suelo");
var sueloFisico = new BABYLON.PhysicsImpostor(suelo , BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, friction: 0, restitution: 0 }, scene);
var posicionFIsicaCOlumna = -0.1;

//UPDATE
scene.onBeforeRenderObservable.add(() => {
    posicionFIsicaCOlumna -=0.01;
    suelo.position.set(0,-0.2122,-5 + posicionFIsicaCOlumna);
})

In the way I wrote the bodies don’t collide, thanks you saved me several hours from breaking my head.

suelo.position.set(0,-0.2122,-5 + posicionFIsicaCOlumna);