I am currently working on n-body simulation using ReactPhysics3D(https://www.reactphysics3d.com/).
I made simple simulation of collision of two spheres. Then, I print the coordinates of the two spheres as the simulation runs. The problem is that they don't collide but pass through each other. Then, they collide in weird position.
Here is the setup Two spheres are created First sphere has position (-2, 0, 0) with initial velocity (1, 0, 0), radius = 0.5, and mass = 10 Second sphere has position (0, 0, 0) with initial velocity (-1, 0, 0), radius = 0.5, and mass = 10
I thought they will collide at positions 0: (-1.5, 0.0, 0.0)
and
1: (-0.5, 0.0, 0.0)
but they pass through it and collide at 0: (-0.5, 0.0, 0.0)
and 1: (-1.5, 0.0, 0.0)
Here is my code
int main (int argc, char **argv) {
Vector3 gravity = Vector3(0.0, 0.0, 0.0);
double mSimulationTime = 2.0;
DynamicsWorld dynamicsWorld(gravity);
// create a rigid body with the position
Vector3 initPosition1(-2.0, 0.0, 0.0);
Quaternion quaternion1 = Quaternion::identity();
Transform transform1(initPosition1, quaternion1);
RigidBody * body1 = dynamicsWorld.createRigidBody(transform1);
// set the initial velocity, material, and the shape
SphereShape sphere1(decimal(0.5));
body1->addCollisionShape(&sphere1, transform1, decimal(10.0));
Vector3 init_velocity1 = Vector3(1.0, 0.0, 0.0);
body1->setLinearVelocity(init_velocity1);
body1->getMaterial().setBounciness(decimal(1.0));
body1->getMaterial().setFrictionCoefficient(decimal(0.0));
body1->setType(BodyType::DYNAMIC);
// create a rigid body with the position
Vector3 initPosition2(0.0, 0.0, 0.0);
Quaternion quaternion2 = Quaternion::identity();
Transform transform2(initPosition2, quaternion2);
RigidBody * body2 = dynamicsWorld.createRigidBody(transform2);
// set the initial velocity, material, and the shape
SphereShape sphere2(decimal(0.5));
body2->addCollisionShape(&sphere2, transform2, decimal(10.0));
Vector3 init_velocity2 = Vector3(-1.0, 0.0, 0.0);
body2->setLinearVelocity(init_velocity2);
body2->getMaterial().setBounciness(decimal(1.0));
body2->getMaterial().setFrictionCoefficient(decimal(0.0));
body2->setType(BodyType::DYNAMIC);
const float timeStep = 1.0 / 60.0;
while (mSimulationTime >= timeStep) {
mSimulationTime -= timeStep;
dynamicsWorld.update(timeStep);
double x, y, z;
x = body1->getTransform().getPosition().x;
y = body1->getTransform().getPosition().y;
z = body1->getTransform().getPosition().z;
printf("0: (%f, %f, %f)\n", x, y, z);
x = body2->getTransform().getPosition().x;
y = body2->getTransform().getPosition().y;
z = body2->getTransform().getPosition().z;
printf("1: (%f, %f, %f)\n", x, y, z);
printf("collision = %s\n", dynamicsWorld.testOverlap(body1, body2) ? "=============================================================" : "false");
printf("num = %d\n", dynamicsWorld.getNbRigidBodies());
printf("----------------\n\n");
}
}
Here is the output
0: (-1.983333, 0.000000, 0.000000)
1: (-0.016667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.966667, 0.000000, 0.000000)
1: (-0.033333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.950000, 0.000000, 0.000000)
1: (-0.050000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.933333, 0.000000, 0.000000)
1: (-0.066667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.916667, 0.000000, 0.000000)
1: (-0.083333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.900000, 0.000000, 0.000000)
1: (-0.100000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.883333, 0.000000, 0.000000)
1: (-0.116667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.866667, 0.000000, 0.000000)
1: (-0.133333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.850000, 0.000000, 0.000000)
1: (-0.150000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.833333, 0.000000, 0.000000)
1: (-0.166667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.816667, 0.000000, 0.000000)
1: (-0.183333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.800000, 0.000000, 0.000000)
1: (-0.200000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.783334, 0.000000, 0.000000)
1: (-0.216667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.766667, 0.000000, 0.000000)
1: (-0.233333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.750000, 0.000000, 0.000000)
1: (-0.250000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.733334, 0.000000, 0.000000)
1: (-0.266667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.716667, 0.000000, 0.000000)
1: (-0.283333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.700000, 0.000000, 0.000000)
1: (-0.300000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.683334, 0.000000, 0.000000)
1: (-0.316667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.666667, 0.000000, 0.000000)
1: (-0.333333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.650000, 0.000000, 0.000000)
1: (-0.350000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.633334, 0.000000, 0.000000)
1: (-0.366667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.616667, 0.000000, 0.000000)
1: (-0.383333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.600000, 0.000000, 0.000000)
1: (-0.400000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.583334, 0.000000, 0.000000)
1: (-0.416667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.566667, 0.000000, 0.000000)
1: (-0.433333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.550000, 0.000000, 0.000000)
1: (-0.450000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.533334, 0.000000, 0.000000)
1: (-0.466667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.516667, 0.000000, 0.000000)
1: (-0.483334, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.500000, 0.000000, 0.000000)
1: (-0.500000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.483334, 0.000000, 0.000000)
1: (-0.516667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.466667, 0.000000, 0.000000)
1: (-0.533333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.450001, 0.000000, 0.000000)
1: (-0.550000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.433334, 0.000000, 0.000000)
1: (-0.566667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.416667, 0.000000, 0.000000)
1: (-0.583333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.400001, 0.000000, 0.000000)
1: (-0.600000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.383334, 0.000000, 0.000000)
1: (-0.616667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.366667, 0.000000, 0.000000)
1: (-0.633333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.350001, 0.000000, 0.000000)
1: (-0.650000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.333334, 0.000000, 0.000000)
1: (-0.666667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.316667, 0.000000, 0.000000)
1: (-0.683333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.300001, 0.000000, 0.000000)
1: (-0.700000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.283334, 0.000000, 0.000000)
1: (-0.716667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.266667, 0.000000, 0.000000)
1: (-0.733333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.250001, 0.000000, 0.000000)
1: (-0.750000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.233334, 0.000000, 0.000000)
1: (-0.766667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.216667, 0.000000, 0.000000)
1: (-0.783333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.200001, 0.000000, 0.000000)
1: (-0.800000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.183334, 0.000000, 0.000000)
1: (-0.816667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.166667, 0.000000, 0.000000)
1: (-0.833333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.150001, 0.000000, 0.000000)
1: (-0.850000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.133334, 0.000000, 0.000000)
1: (-0.866666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.116668, 0.000000, 0.000000)
1: (-0.883333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.100001, 0.000000, 0.000000)
1: (-0.900000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.083334, 0.000000, 0.000000)
1: (-0.916666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.066668, 0.000000, 0.000000)
1: (-0.933333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.050001, 0.000000, 0.000000)
1: (-0.950000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.033334, 0.000000, 0.000000)
1: (-0.966666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.016668, 0.000000, 0.000000)
1: (-0.983333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-1.000001, 0.000000, 0.000000)
1: (-1.000000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.983334, 0.000000, 0.000000)
1: (-1.016666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.966668, 0.000000, 0.000000)
1: (-1.033333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.950001, 0.000000, 0.000000)
1: (-1.050000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.933334, 0.000000, 0.000000)
1: (-1.066666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.916668, 0.000000, 0.000000)
1: (-1.083333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.900001, 0.000000, 0.000000)
1: (-1.100000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.883334, 0.000000, 0.000000)
1: (-1.116666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.866668, 0.000000, 0.000000)
1: (-1.133333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.850001, 0.000000, 0.000000)
1: (-1.150000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.833334, 0.000000, 0.000000)
1: (-1.166666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.816668, 0.000000, 0.000000)
1: (-1.183333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.800001, 0.000000, 0.000000)
1: (-1.200000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.783334, 0.000000, 0.000000)
1: (-1.216666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.766668, 0.000000, 0.000000)
1: (-1.233333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.750001, 0.000000, 0.000000)
1: (-1.250000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.733335, 0.000000, 0.000000)
1: (-1.266666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.716668, 0.000000, 0.000000)
1: (-1.283333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.700001, 0.000000, 0.000000)
1: (-1.299999, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.683335, 0.000000, 0.000000)
1: (-1.316666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.666668, 0.000000, 0.000000)
1: (-1.333333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.650001, 0.000000, 0.000000)
1: (-1.349999, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.633335, 0.000000, 0.000000)
1: (-1.366666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.616668, 0.000000, 0.000000)
1: (-1.383333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.600001, 0.000000, 0.000000)
1: (-1.399999, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.583335, 0.000000, 0.000000)
1: (-1.416666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.566668, 0.000000, 0.000000)
1: (-1.433333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.550001, 0.000000, 0.000000)
1: (-1.449999, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.533335, 0.000000, 0.000000)
1: (-1.466666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.516668, 0.000000, 0.000000)
1: (-1.483333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.500001, 0.000000, 0.000000)
1: (-1.499999, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.483335, 0.000000, 0.000000)
1: (-1.516666, 0.000000, 0.000000)
collision = =============================================================
num = 2
----------------
0: (-0.502335, 0.000000, 0.000000)
1: (-1.497666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.519001, 0.000000, 0.000000)
1: (-1.480999, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.535668, 0.000000, 0.000000)
1: (-1.464333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.552335, 0.000000, 0.000000)
1: (-1.447666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.569001, 0.000000, 0.000000)
1: (-1.431000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.585668, 0.000000, 0.000000)
1: (-1.414333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.602334, 0.000000, 0.000000)
1: (-1.397666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.619001, 0.000000, 0.000000)
1: (-1.381000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.635668, 0.000000, 0.000000)
1: (-1.364333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.652334, 0.000000, 0.000000)
1: (-1.347666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.669001, 0.000000, 0.000000)
1: (-1.331000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.685668, 0.000000, 0.000000)
1: (-1.314333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.702334, 0.000000, 0.000000)
1: (-1.297666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.719001, 0.000000, 0.000000)
1: (-1.281000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.735668, 0.000000, 0.000000)
1: (-1.264333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.752334, 0.000000, 0.000000)
1: (-1.247666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.769001, 0.000000, 0.000000)
1: (-1.231000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.785668, 0.000000, 0.000000)
1: (-1.214333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.802334, 0.000000, 0.000000)
1: (-1.197666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.819001, 0.000000, 0.000000)
1: (-1.181000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.835668, 0.000000, 0.000000)
1: (-1.164333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.852334, 0.000000, 0.000000)
1: (-1.147666, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.869001, 0.000000, 0.000000)
1: (-1.131000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.885668, 0.000000, 0.000000)
1: (-1.114333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.902334, 0.000000, 0.000000)
1: (-1.097667, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.919001, 0.000000, 0.000000)
1: (-1.081000, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.935668, 0.000000, 0.000000)
1: (-1.064333, 0.000000, 0.000000)
collision = false
num = 2
----------------
0: (-0.952334, 0.000000, 0.000000)
1: (-1.047667, 0.000000, 0.000000)
collision = false
num = 2
----------------
Could you give me some advice why I am getting this result? I guessing wrong initialization or wrong way to print coordinates but I couldn't figure it out. Thank you
Note: This is just a good guess because the numbers add up.
I think that the additional transform of -2 to its collision shape causes this unwanted effect.
This is your initialization logic for sphere1. I
Vector3 initPosition1(-2.0, 0.0, 0.0);
Transform transform1(initPosition1, quaternion1);
RigidBody * body1 = dynamicsWorld.createRigidBody(transform1);
// set the initial velocity, material, and the shape
SphereShape sphere1(decimal(0.5));
body1->addCollisionShape(&sphere1, **transform1**, decimal(10.0));
Sphere 1: direction: +1.0 expected Pos(-1.5) Actual (-0.5) => diff |1|
Sphere 2: direction: -1.0 expected Pos(-0.5) Actual (-1.5) => diff |1|
Your expected positions are failed by exactly the amount of 1 in each moving direction. Two spheres both moving 1 unit towards each other too far create a total distance of 2, which is exactly your amount of translation to the collision shape of sphere1.
Please test this, it might just be a coincidence...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With