saharan / OimoPhysics

A cross-platform 3D physics engine
MIT License
863 stars 68 forks source link

Moving Dynamic Rigid Body #30

Closed CDanSantana closed 2 years ago

CDanSantana commented 4 years ago

Hi! First of all, awesome project! Congratulations! I'm trying to use your library in a small fps like project... I'm using the available javascript binary... I'm trying to move a simple dynamic rigid body in a place with static rigid bodies... But I can't get the dynamic body to move applying impulses or forces... So I tried to move using the setPosition method of the rigid body. It works, but seems like the gravity stops acting on the body after this... What am I doing wrong?

The following code I use on main character class:

class Character {
    constructor(px, py, pz){
        this.body_cnf = new OIMO.RigidBodyConfig();
        this.body_cnf.type= OIMO.RigidBodyType.DYNAMIC;
        this.body_cnf.position = new OIMO.Vec3(px, py, pz);
        this.body = new OIMO.RigidBody(this.body_cnf);
        this.shape_cnf = new OIMO.ShapeConfig();
        this.shape_cnf.geometry = new OIMO.CylinderGeometry(0.5,1.0);
        this.shape_cnf.density=70.0;
        this.shape_cnf.position=new OIMO.Vec3(px,py,pz);
        this.body.addShape(new OIMO.Shape(this.shape_cnf ));
        world.addRigidBody(this.body);
    }
    moveFront(){
        this.body.applyImpulse(new OIMO.Vec3(0,0,1), this.body.getPosition());
        //Also tried
        //this.body.applyForce(new OIMO.Vec3(0,0,1), this.body.getPosition());
    }
    moveBack(){
        this.body.applyImpulse(new OIMO.Vec3(0,0,-1), this.body.getPosition());
        //Also tried
        //this.body.applyForce(new OIMO.Vec3(0,0,-1), this.body.getPosition());
    }
}

Thanks!

cjbailey commented 4 years ago

How often are you applying the impulse? i.e. just once or every step update? Maybe the impulse you're applying is too small to make a noticeable change?

CDanSantana commented 4 years ago

Hi @cjbailey! I tried applying one impulse every 0.5 second while a key is being held down... Also tried applying a Vec3 every step update, changing it from (0.0, 0.0, 0.0) to something like (0.0, 0.0, 1.0) when a key is being held down and back to (0.0, 0.0, 0.0) on key release... Tried using setLinearVelocity(), every 0.5 seconds and and in every step update... In this case, the body followed random directions, passing through the static bodies... I also tried with large values... Unfortunately, so far I have achieved nothing... Still trying to figure out what I'm doing wrong... Regarding gravity, do you know if it really should stop when I change the position manually?

cjbailey commented 4 years ago

Hmmm.. I'm not sure tbh... I created a simple setup in CodeSandbox to play around with and it seemed to work ok for me... https://codesandbox.io/s/threejs-oimophysics-integration-test-3-djc6g?file=/src/DemoScene.ts

p.s. I've just created a Spectrum.chat community for OimoPhysics as it seems a better fit for general help type questions.

https://spectrum.chat/oimo-physics

CDanSantana commented 4 years ago

Thanks @cjbailey! I will keep on trying!

saharan commented 3 years ago

I'm not sure what's actually happening in your code, but in Character's constructor, shape_cnf.position is set to the value of body_cnf.position. As in the API document, this means that the shape will be placed at (px, py, pz) relative to the parent body's position. Here, you set the body's position to (px, py, pz) through body_cnf.position, so the shape will initially be placed at (2*px, 2*py, 2*pz) in global coordinates, while the body's center of gravity is still at (px, py, pz). This may result in a strong momentum and unexpected behaviour, especially when the position vector is large. Please check if this is the cause of your problem.