Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem SFML with Box2d  (Read 2185 times)

0 Members and 1 Guest are viewing this topic.

shotoreaper

  • Newbie
  • *
  • Posts: 11
    • View Profile
Problem SFML with Box2d
« on: May 30, 2013, 01:21:12 pm »
Hi!
In this code I try generate a Box that must fall and collide with a ground. Simple.
But when the box falls, it don´t collide with the ground. I think that the positions and sizes match, so I don´t now why it fails.

Code:

#include "SFML/Graphics.hpp"
#include "Box2D/Box2D.h"
#include <stdio.h>
#include <iostream>

#define PPM 30 // Pixel Per Meters

using namespace sf;
using namespace std;

int main()
{
    bool running = true;

    RenderWindow app(VideoMode(800,600,32),"First Test");

    sf::RectangleShape box(sf::Vector2f(100,50));
    box.setPosition(sf::Vector2f(150.f,4.f));

    sf::RectangleShape ground(sf::Vector2f(200,50));
    ground.setPosition(sf::Vector2f(0,500));

    // set up the world
    b2Vec2 gravity(0.0f,9.8f);
    b2World *myWorld = new b2World(gravity);

    // creamos y configuramos el suelo
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0.0f, 500.0f);
    b2Body* groundBody = myWorld->CreateBody(&groundBodyDef);

    b2PolygonShape groundBox;
    groundBox.SetAsBox(100.0f, 25.0f);

    groundBody->CreateFixture(&groundBox, 0.0f);

    // creamos y configuramos un cuerpo dinamico
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(150.f, 4.0f);

    b2Body* body = myWorld->CreateBody(&bodyDef);

    b2PolygonShape dynamicBox;
    dynamicBox.SetAsBox(50.0f, 25.0f);

    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;

    body->CreateFixture(&fixtureDef);

    float timeStep = 1.0f / 300.0f; // 30fps

    int velIter = 8;
    int posIter = 3;

    //app.setFramerateLimit(30);

    while(running)
    {
        Event event;
        while(app.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
            running = false;
        }

        myWorld->Step(timeStep, velIter, posIter);

        b2Vec2 pos = body->GetPosition();

        box.setPosition(sf::Vector2f(pos.x,pos.y));

        app.clear();

        app.draw(box);

        app.draw(ground);

        app.display();
    }

    app.close();
    delete myWorld;

    return EXIT_SUCCESS;
}
 

fallahn

  • Sr. Member
  • ****
  • Posts: 498
  • Buns.
    • View Profile
    • Trederia
Re: Problem SFML with Box2d
« Reply #1 on: May 30, 2013, 01:37:11 pm »
 #define PPM 30 // Pixel Per Meters

should really be

const int PPM = 30;

although looking at your code you don't seem to be multiplying your sizes by your scale anyway (the box2D manual / FAQ explains why trying to use 1 pixel per metre is a bad thing). Also box2D uses an inverted Y axis to SFML so you're setting your box rectangle 4 pixels from the top of the screen, but the box2D body 4 metres from the bottom. Oh and isn't

float timeStep = 1.0f / 300.0f; // 30fps

actually 300fps?

shotoreaper

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Problem SFML with Box2d
« Reply #2 on: May 30, 2013, 02:49:51 pm »
I set 300, because 30 draw very fast. With 300 draw slow.
And the box dont colllide with the ground when his positon X is >100. I dont understand why happen this if the ground have 200 width and his position X is 0

shotoreaper

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Problem SFML with Box2d
« Reply #3 on: May 30, 2013, 05:43:00 pm »
Ok, I found the problem. Box2d have the origin in the center, but SFML have it in the top-left. To resolve this I set the origin in the middle:
box.setOrigin(width/2, height/2);