I am creating a simple OpenGL C++ game. I have a main character (green box) and some static objects (red boxes) positioned as stairs.
The problem is as green box is descending down the stairs, it overlaps with the edges of red boxes. You can see in the gif that bottom right of green box intersects with top left of red boxes.
It's not the same with Unity. If the character collides, it rotates giving a much more natural feel.
Character.cpp
(Dynamic Object)
m_Position = glm::vec2(800.0f, 800.0f);
m_Rotation = 45.0f;
m_Scale = 50.0f;
b2BodyDef def;
def.type = b2_dynamicBody;
def.position.Set(Util::PixelToMeter(m_Position.x), Util::PixelToMeter(m_Position.y));
m_Body = m_PhysicsWorld.CreateBody(&def);
b2PolygonShape shape;
shape.SetAsBox(Util::PixelToMeter(m_Scale / 2.0f), Util::PixelToMeter(m_Scale / 2.0f));
b2Fixture* fixture = m_Body->CreateFixture(&shape, 1.0f);
fixture->SetFriction(0.3f);
And then on onRender
:
const auto& newPosition = m_Body->GetPosition();
m_Position.x = Util::MeterToPixel(newPosition.x);
m_Position.y = Util::MeterToPixel(newPosition.y);
m_Rotation = m_Body->GetAngle();
Wall.cpp
(Static Object)
m_Position = glm::vec2(position.x, position.y);
m_Rotation = 0.0f;
m_Scale = 50.0f;
b2BodyDef def;
def.type = b2_staticBody;
def.position.Set(Util::PixelToMeter(m_Position.x), Util::PixelToMeter(m_Position.y));
m_Body = m_PhysicsWorld.CreateBody(&def);
b2PolygonShape shape;
shape.SetAsBox(Util::PixelToMeter(m_Scale / 2.0f), Util::PixelToMeter(m_Scale / 2.0f));
m_Body->CreateFixture(&shape, 0.0f);
This is my repository: https://github.com/axelthat/OpenGLBox2D
Physics world is created here: https://github.com/axelthat/OpenGLBox2D/blob/main/SuperMarioBrosClone/src/Game.cpp
Red box: https://github.com/axelthat/OpenGLBox2D/blob/main/SuperMarioBrosClone/src/Wall.cpp
Green box: https://github.com/axelthat/OpenGLBox2D/blob/main/SuperMarioBrosClone/src/Character.cpp
What am I doing wrong?