Home Game Development opengl – Character overlaps with static objects

opengl – Character overlaps with static objects

0
opengl – Character overlaps with static objects

[ad_1]

I’m making a easy OpenGL C++ sport. I’ve a most important character (inexperienced field) and a few static objects (pink containers) positioned as stairs.

The issue is as inexperienced field is descending down the steps, it overlaps with the sides of pink containers. You possibly can see within the gif that backside proper of inexperienced field intersects with top left of pink containers.

enter image description here

It isn’t the identical with Unity. If the character collides, it rotates giving a way more pure really feel.

enter image description here

Character.cpp (Dynamic Object)

m_Position = glm::vec2(800.0f, 800.0f);
m_Rotation = 45.0f;
m_Scale = 50.0f;

b2BodyDef def;
def.sort = b2_dynamicBody;
def.place.Set(Util::PixelToMeter(m_Position.x), Util::PixelToMeter(m_Position.y));

m_Body = m_PhysicsWorld.CreateBody(&def);

b2PolygonShape form;
form.SetAsBox(Util::PixelToMeter(m_Scale / 2.0f), Util::PixelToMeter(m_Scale / 2.0f));

b2Fixture* fixture = m_Body->CreateFixture(&form, 1.0f);
fixture->SetFriction(0.3f);

After which 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(place.x, place.y);
m_Rotation = 0.0f;
m_Scale = 50.0f;

b2BodyDef def;
def.sort = b2_staticBody;
def.place.Set(Util::PixelToMeter(m_Position.x), Util::PixelToMeter(m_Position.y));

m_Body = m_PhysicsWorld.CreateBody(&def);

b2PolygonShape form;
form.SetAsBox(Util::PixelToMeter(m_Scale / 2.0f), Util::PixelToMeter(m_Scale / 2.0f));

m_Body->CreateFixture(&form, 0.0f);

That is my repository: https://github.com/axelthat/OpenGLBox2D

Physics world is created right here: https://github.com/axelthat/OpenGLBox2D/blob/most important/SuperMarioBrosClone/src/Sport.cpp

Purple field: https://github.com/axelthat/OpenGLBox2D/blob/most important/SuperMarioBrosClone/src/Wall.cpp

Inexperienced field:
https://github.com/axelthat/OpenGLBox2D/blob/most important/SuperMarioBrosClone/src/Character.cpp

What am I doing unsuitable?

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here