lo-th / Oimo.js

Lightweight 3d physics engine for javascript
http://lo-th.github.io/Oimo.js
MIT License
3.05k stars 301 forks source link

How to create a body that "can't move but can rotate" ? #73

Open finscn opened 6 years ago

finscn commented 6 years ago

I set the move: false , it can't rotate, move:true , it can move & can rotate.

How to create a body that "can't move but can rotate" ?

How to create a body that "can't rotate but can move" ?

Thanks

l1553k commented 6 years ago

I guess (I just started using this lib) you have to set move: true and add a joint (probably ball and socket type). If necessary, add another body with move:false for this join to attach to.

w84miracle commented 6 years ago

There seems no simple option like rotate:false for now. But the concept is simple: RigidBody.linearVelocity controls movement and the RigidBody.angularVelocity controls rotaion. So, to make and object rotate only, reset object's position and it's linear velocity in each frame. You can try these code I modified from the rotation demo:

function demo() {
    cam ( 90, 20, 100 );
    world = new OIMO.World({ info:true,gravity: [0,-100,0]  });
    g1 = add({ size:[50, 5, 10], pos:[0,0,0], rot:[0,0,-5], density:1, move:true});
    // basic geometry body
    b1 = add({ type:'sphere', size:[3], pos:[20,50,0],density:1, move:true });

    // world internal loop
    world.postLoop = postLoop;
    world.play();

};
function postLoop () {
    bodys.forEach( function ( b, id ) {

        if( b.shapes.type === 1 ){
        m = b.mesh;
            if( m.position.y < -50 ) b.resetPosition( 20, 50,0 );
        }else if(b.type === 1){
            g1.position.set(0,0,0)
            g1.linearVelocity.set(0,0,0);
        }
    });
   // Uncomment this line to apply force manually
   // g1.applyImpulse({x:20,y:0,z:0},{x:0,y:-99999,z:0})
    editor.tell( world.getInfo() );
}

Also, you can modify angular velocity (g1.angularVelocity in this case) depending on which axis you want to limit the object to rotate along.

But if you want objects work like a hinge attached, use joint like @l1553k advised

icedcoke commented 5 years ago

I give the model a speed like this this.modelBody.linearVelocity.set(1000,0,0)