Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why is the collision not happening how I think it should? #384

Closed
JayNakum opened this issue Apr 19, 2024 · 3 comments
Closed

Why is the collision not happening how I think it should? #384

JayNakum opened this issue Apr 19, 2024 · 3 comments
Assignees

Comments

@JayNakum
Copy link

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

image

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;
}
@JayNakum JayNakum changed the title Why is the collision not happening how I think it should happen? Why is the collision not happening how I think it should? Apr 19, 2024
@vvector2
Copy link

Having exactly the same stuff on my machine

@DanielChappuis DanielChappuis self-assigned this May 14, 2024
@DanielChappuis
Copy link
Owner

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.

@DanielChappuis
Copy link
Owner

I am closing this issue. Feel free to reopen it if something is not clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants