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;
}