About Physics Engine!

Hi,
I have a question about Physics engine.
I want rotate the physicsRoot,but this line ( physicsRoot.rotation.y += 0.1;) does not effect.
I have used unity,if I use physics engine,it can do well.
if I remove the line (physicsRoot.physicsImpostor = new BABYLON.PhysicsImpostor(physicsRoot, BABYLON.PhysicsImpostor.NoImpostor, { mass: 3 }, scene);),it can effect,however it has no physics effect.
I don’t know why? Can you help me? Thanks a lot!

The relative code as follow:

var createScene = function () {
var scene = new BABYLON.Scene(engine);
var camera = new BABYLON.FreeCamera(“camera1”, new BABYLON.Vector3(0, 5, -10), scene);
camera.setTarget(BABYLON.Vector3.Zero());
camera.attachControl(canvas, true);
var light = new BABYLON.HemisphericLight(“light1”, new BABYLON.Vector3(0, 1, 0), scene);
scene.enablePhysics(new BABYLON.Vector3(0,-10,0), new BABYLON.AmmoJSPlugin());
var ground = BABYLON.Mesh.CreateGround(“ground1”, 6, 6, 2, scene);
ground.physicsImpostor = new BABYLON.PhysicsImpostor(ground, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, friction: 0.5, restitution: 0.7 }, scene);

// Import mesh and set colliders
BABYLON.SceneLoader.ImportMesh("", "scenes/", "skull.babylon", scene, function (newMeshes) {
    // Scale loaded mesh
    newMeshes[0].scaling.scaleInPlace(0.01);
    newMeshes[0].position.set(0,0,0)

    var boxCollider = BABYLON.Mesh.CreateBox("box1", 0.3, scene);
    // Create a physics root and add all children
    var physicsRoot = new BABYLON.Mesh("", scene);
    physicsRoot.addChild(newMeshes[0]);
    physicsRoot.addChild(boxCollider);
    physicsRoot.position.y+=3;
    physicsRoot.rotation = new BABYLON.Vector3(0,Math.PI/2,0);

    // Enable physics on colliders first then physics root of the mesh
    boxCollider.physicsImpostor = new BABYLON.PhysicsImpostor(boxCollider, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0 }, scene);
    physicsRoot.physicsImpostor = new BABYLON.PhysicsImpostor(physicsRoot, BABYLON.PhysicsImpostor.NoImpostor, { mass: 3 }, scene);

    setInterval(()=>{
        physicsRoot.rotation.y += 0.1;
    },100)        
});
return scene;

};

1 Like

Hey! I have an easy solution. Whenever you are using a physicsimpostor, you have to use rotation Quaternion. Works the same was as a plain rotation for non-physics meshes. I even made a little demo for you: https://www.babylonjs-playground.com/#BEFOO#397

rotation Quaternion does have a sort of limit, though, so you can’t have a loop that rotates it forever. I think.

(P.S. when using code in the forums, you can use ``` on the line above and below your code to make it all look nice.)

sphere.rotationQuaternion = 5;
1 Like

Yes,It does well.thank you very much.

There is something wrong.It can do well,if you modify these codes:
var yaw = 0;
var pitch = 0;
var roll = 0;

scene.registerBeforeRender(function () {
    if(inputMap[" "]){
        pitch += 0.1;
        sphere.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(yaw, pitch, roll);
    } 
});

Thank you again!
I think it maybe a bug of Babylon.js.

I made a playground with the issue. https://www.babylonjs-playground.com/#BEFOO#398 I’m not sure what is going on, but the console might have an error in it. If you are using chrome, just open the link and press ctrl+shift+i to open the console. I’ll try and figure out who to ping. @Deltakosh who knows the physics engines?

you should modify this line with var pitch = 0;

Oh, right. I’m used to initializing variables and setting them later.
https://www.babylonjs-playground.com/#BEFOO#399
This works, so what’s the issue?