Hazel icon indicating copy to clipboard operation
Hazel copied to clipboard

Fixed physics collider visualization offset

Open erikrl2 opened this issue 2 years ago • 2 comments

Describe the issue

The collider visualization rectangle does not match the actual position of the collider when using offset:

Hazel - Physics Collider Offset Issue - before

PR impact

List of related issues/PRs this will solve:

Impact Issue/PR
Issues this solves None
Other PRs this solves None

Proposed fix

Adding the offset and rotation to the Box2D box shape:

b2PolygonShape boxShape;
boxShape.SetAsBox(bc2d.Size.x * transform.Scale.x, bc2d.Size.y * transform.Scale.y,
	b2Vec2(bc2d.Offset.x, bc2d.Offset.y), bc2d.Rotation);

Calculating the relative transform of the visualization rectangle before the entity's transform:

glm::vec3 scale = tc.Scale * glm::vec3(bc2d.Size * 2.0f, 1.0f);

glm::mat4 transform = glm::translate(glm::mat4(1.0f), tc.Translation)
	* glm::rotate(glm::mat4(1.0f), tc.Rotation.z, glm::vec3(0.0f, 0.0f, 1.0f))
	* glm::translate(glm::mat4(1.0f), glm::vec3(bc2d.Offset, 0.001f))
	* glm::rotate(glm::mat4(1.0f), bc2d.Rotation, glm::vec3(0.0f, 0.0f, 1.0f))
	* glm::scale(glm::mat4(1.0f), scale);

Now it looks like:

Hazel - Physics Collider Offset Issue - after

Also fixed this for circle colliders.

Note: Needed to add Rotation attribute to BoxCollider2DComponent. You could expose this to the editor and serialize it.

erikrl2 avatar Jun 11 '22 21:06 erikrl2

Commit history is such a mess because I failed rebasing to master.

erikrl2 avatar Jun 24 '22 16:06 erikrl2

I applied your requested changes @TheCherno 👍

erikrl2 avatar Jul 04 '22 10:07 erikrl2