reactphysics3d
reactphysics3d copied to clipboard
Why is the collision not happening how I think it should?
Greetings!
I have created a very basic test with two rigid bodies. One of them is of BodyType::DYNAMIC
at position (0, 20, 0) and another one is BodyType::STATIC
at position (0, 0, 0).
Both of the bodies has box collider of halfextents (0.5, 0.5, 0.5) and (5.0, 1.0, 5.0) respectively.
Since the body at (0, 20, 0) is falling through gravity, it should ideally collide before it's position reaches (0, 0, 0) because it has a 1x1x1 collider box. But this is the output I am getting where the entity kept falling till (0, -18.68, 0), way below the expected position, after which the collision was detected.
Output
My code
#include "reactphysics3d/reactphysics3d.h"
#include <iostream>
using namespace reactphysics3d;
constexpr const char* COLOR_GREEN = "\x1B[32m";
constexpr const char* COLOR_YELLOW = "\x1B[33m";
constexpr const char* COLOR_RED = "\x1B[31m";
constexpr const char* COLOR_RESET = "\x1B[00m";
class PhysicsEventListner : public EventListener
{
public:
virtual void onContact(const CollisionCallback::CallbackData& callbackData) override
{
std::cout << COLOR_GREEN << "Collision Detected." << COLOR_RESET << std::endl;
}
};
int main()
{
PhysicsCommon physics;
PhysicsWorld* world = physics.createPhysicsWorld();
world->setEventListener(new PhysicsEventListner());
Transform transform1(Vector3(0, 20, 0), Quaternion::identity());
RigidBody* cube = world->createRigidBody(transform1);
{
cube->setType(BodyType::DYNAMIC);
BoxShape* box = physics.createBoxShape(Vector3(0.5f, 0.5f, 0.5f));
Collider* collider = cube->addCollider(box, transform1);
cube->updateMassPropertiesFromColliders();
}
Transform transform2(Vector3(0, 0, 0), Quaternion::identity());
RigidBody* table = world->createRigidBody(transform2);
{
table->enableGravity(false);
table->setType(BodyType::STATIC);
BoxShape* box = physics.createBoxShape(Vector3(5.0f, 1.0f, 5.0f));
Collider* collider = table->addCollider(box, transform2);
table->updateMassPropertiesFromColliders();
}
unsigned int i = 200;
while (i--)
{
world->update(1.0f / 60.0f);
const Transform& transform1 = cube->getTransform();
const Vector3& position1 = transform1.getPosition();
const Transform& transform2 = table->getTransform();
const Vector3& position2 = transform2.getPosition();
std::cout << "Body1 Position: (" << position1.x << ", " << position1.y << ", " << position1.z << ")" << std::endl;
std::cout << "Body2 Position: (" << position2.x << ", " << position2.y << ", " << position2.z << ")" << std::endl << std::endl;
}
world->destroyRigidBody(cube);
world->destroyRigidBody(table);
physics.destroyPhysicsWorld(world);
return 0;
}
Having exactly the same stuff on my machine
Hello. Sorry for my late answer.
The issue in your code is probably because when creating the box body and collider, you are using the transform1
twice. Once for the position of your body in the createRigidBody()
method (which seems fine) but also a second time when you set the position of your collider in the addCollider()
method (that is probably wrong).
As described here in the documentation, the transform
parameter of the addCollider()
method is the transform of the collider relative to the origin of the body.
Therefore, in your case, your box body is at world-position (0, 20, 0) and your box collider is at world-position (0, 40, 0). That's why it collides with the table when the box position is around (0, -18, 0).
I hope this helps.
I am closing this issue. Feel free to reopen it if something is not clear.