DanielChappuis / reactphysics3d

Open source C++ physics engine library in 3D
http://www.reactphysics3d.com
zlib License
1.52k stars 219 forks source link

Object jitter and random angular momentum #170

Closed JPitman97 closed 4 years ago

JPitman97 commented 4 years ago

Hi, Apologies if this is due to my own mistakes. I have implemented react physics into my engine and have set up a basic scene with 2 objects (Both with box colliders), one is a dynamic rigid body and one is the "map" collider so is static. The world has standard gravity and the dynamic cube has a mass of 20 kilograms and angular damping of 1 (Was testing whether this would remove the random angular momentum)

The dynamic cube is dropped from 20 metres and on collision with the map it bounces quite high (which I tried to remedy by changing the physics material, which apparently no longer exists) but after bouncing it often changes direction and refuses to stop moving, even though the map plane is flat (So should have no angular momentum transferred)

Code is visible on my ArchitectEngine repo under the MeshCollision branch, Core::Start is where debug code is where I was testing the physics engine and trying to resolve this issue.

Cheers, Jack

DanielChappuis commented 4 years ago

Hello,

The Material class still exists but is now part of the the Collider class (and not the RigidBody anymore) in version 0.8.0. As you can see here, the Collider contains a getMaterial() method.

I didn't find your project or branch. Is this your project ? Can you post here the code that you use to create the two boxes and the heightfield and also how to you update the PhysicsWorld ?

It would be great if you can post a small video of a GIF that shows the issue.

DanielChappuis commented 4 years ago

Ok now I have find your project ...

JPitman97 commented 4 years ago

Hi, Sorry this is the link to the repo and specific file with the physics code (https://github.com/JPitman97/ArchitectEngine/blob/MeshCollision/src/ArchitectEngine/Core.cpp), the relevant code is within the Core::Start method and please ignore how messy it is and the large comment (That was the previous code)

Attached is a video of what happens ScreenRec

DanielChappuis commented 4 years ago

You have changed the mass of the rigid body to 20kg. In this case, you need to manually set the inertia tensor of the body. The default inertia tensor is for the default mass of 1kg. Therefore, you need to use the RigidBody::setLocalInertiaTensor() method to manually set the inertia tensor of the body.

Note that you can also automatically compute the mass and inertia tensor using the RigidBody::updateMassPropertiesFromColliders() method but in this case you should not set the mass yourself with the RigidBody::setMass() method but you need to use the density value of the material of a collider. This will be use to automatically compute the mass and inertia tensor of your body after you have added the collider to the body.

You can find more information here in the documentation here.

Can you try this and let me know if this improves things. You can also try to see if you have this issue without setting the mass to 20kg and use the default value ?

JPitman97 commented 4 years ago

Using the RigidBody::updateMassPropertiesFromColliders() worked and means that now the cube just bounces vertically and comes to rest, appreciate the help there.

I tried accessing the material on the collider by doing auto mat = body->getCollider(0)->getMaterial(); however this results in an error stating that Material declared at line 66 is inaccessible.

Because of this I tried body->getCollider(0)->getMaterial().setBounciness(0); which didn't cause an error however had no effect (The cube still bounces)

I am using the user manual which still states the material is under the RigidBody, hence my confusion, I apologie.

JPitman97 commented 4 years ago

Sorry, just applied "getCollider(0)->getMaterial().setBounciness(0)" to both the cube and to the map RigidBody and now the cube no longer bounces

DanielChappuis commented 4 years ago

Using the RigidBody::updateMassPropertiesFromColliders() worked and means that now the cube just bounces vertically and comes to rest, appreciate the help there.

Ok great. Note that using this method will override the mass of 20kg that you set but it will automatically compute the mass and inertia tensor of the body using the density of the material of your collider.

I tried accessing the material on the collider by doing auto mat = body->getCollider(0)->getMaterial(); however this results in an error stating that Material declined at line 66 is inaccessible.

The getMaterial() method returns a reference to the material of the collider so that you can change it. Therefore, you might need to use "auto&" as follows:

auto& mat = body->getCollider(0)->getMaterial();

I am using the user manual which still states the material is under the RigidBody, hence my confusion, apologies.

You are right this is a mistake sorry. I will fix this in the user manual.

Sorry, just applied "getCollider(0)->getMaterial().setBounciness(0)" to both the cube and to the map RigidBody and now the cube no longer bounces

Yes when two colliders collide, the largest bounciness of the two colliders is used.

Can I close this issue ?

JPitman97 commented 4 years ago

I appreciate all your help, sorry it was something so simple that I missed, thank you for the great physics library!

DanielChappuis commented 4 years ago

Don't be sorry. I wish you all the best with your engine project !!