2
\$\begingroup\$

I've been implementing Box2D into my engine but have run into a snag. I decided to rebuild the helloworld project inside of my own engine (it is almost identical), of which I will snip here in a moment. Firstly, the problem:

The dynamic body collides too early on the ground body. The ground body is much larger than is being rendered.

Code:

GameHandler.cpp

#include "GameHandler.h"

gameHandler::gameHandler()
{
    world_ = new b2World(gravity); //Gravity = 0, -10

    gfx = graphics(ResourceManager::getShader("main"));

    enter();
}

void gameHandler::update()
{
    float32 timestep = 1.0f / 60.0f;

    int32 velocityIts = 6;
    int32 positionIts = 2;

    world_->Step(timestep, velocityIts, positionIts);

    std::cout << "bodyx: " << 
    body_->GetPosition().x << " \t\tbodyy: " << 
    body_->GetPosition().y << " \tgroundx: " << 
    ground_->GetPosition().x << " \tgroundy: " << 
    ground_->GetPosition().y << "\n";


    input.update();

    if (input.isKeyDown(SDL_SCANCODE_RIGHT))
    {
        body_->ApplyForce(b2Vec2(20, 0), b2Vec2(0, 0), true);
    }
    if (input.isKeyDown(SDL_SCANCODE_LEFT))
    {
        body_->ApplyForce(b2Vec2(-20, 0), b2Vec2(0, 0), true);
    }

}

void gameHandler::render()
{
    gfx.drawSprite(Texture(), ground_->GetPosition(), groundSize, 0.0f, glm::vec3(1.0f, 1.0f, 0.0f)); 
    gfx.drawSprite(Texture(), body_->GetPosition(), bodySize, body_->GetAngle(), glm::vec3(0.0f, 1.0f, 0.0f));
}

void gameHandler::enter()
{

    groundSize = b2Vec2(10.0f, 10.0f);
    bodySize = b2Vec2(2.0f, 2.0f);

    // Define the ground body.
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0.0f, -20.0f);

    // Call the body factory which allocates memory for the ground body
    // from a pool and creates the ground box shape (also from a pool).
    // The body is also added to the world.
    ground_ = world_->CreateBody(&groundBodyDef);

    // Define the ground box shape.
    b2PolygonShape groundBox;

    // The extents are the half-widths of the box.
    groundBox.SetAsBox(groundSize.x, groundSize.y);

    // Add the ground fixture to the ground body.
    ground_->CreateFixture(&groundBox, 0.0f);

    // Define the dynamic body. We set its position and call the body factory.
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(0.0f, 4.0f);
    body_ = world_->CreateBody(&bodyDef);

    // Define another box shape for our dynamic body.
    b2PolygonShape dynamicBox;
    dynamicBox.SetAsBox(bodySize.x, bodySize.y);

    // Define the dynamic body fixture.
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox;

    // Set the box density to be non-zero, so it will be dynamic.
    fixtureDef.density = 1.0f;

    // Override the default friction.
    fixtureDef.friction = 0.0f;

    // Add the shape to the body.
    body_->CreateFixture(&fixtureDef);
}

Projection:

float lowerX = -25.0f, upperX = 25.0f, lowerY = -15.0f, upperY = 25.0f;
glm::mat4 projection = glm::ortho(lowerX, upperX, lowerY, upperY, -1.0f, 1.0f);

Gif:

Dynamic box falling on static box inaccuracy

The box falls off after reaching the end of what seems to be the ground box scaled to about double it's size to the left. The same thing happens going to the right but the invisible ledge is much shorter.

The same thing does happen inside of the helloworld.cpp project provided in the examples:

helloworld.cpp problem

Strangest thing, any help?

Thanks in advance for any help!

\$\endgroup\$
1
\$\begingroup\$

I was playing around a little and rereading the manual again. It noted that the size of the actual box is 2x the given size. Half-widths.

So I continued to play around inside my graphics system and continued to see where the box was colliding and not and ended up with:

Negate the (untouched) size from the position. Double the size of the boxes.

glm::vec2 position(pos_.x - size_.x, pos_.y - size_.y);
glm::vec2 size(size_.x * 2, size_.y * 2);

It seems to be working

I could also have done all of this in the matrix operations, but this might use less ops.

\$\endgroup\$
1
  • \$\begingroup\$ I feel a little silly posting after figuring this out :) thanks for letting me know i'm not alone in this sailboat! \$\endgroup\$ – creativeflow Aug 8 '15 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.