Hello.
Today I've downloaded Box2D and wanted to test it out a little bit, just to see how it works with SFML.
I wrote a really, really simple code just to see if everything works properly, but I'm getting strange results.
My box, that should be dynamic, is dynamic indeeed, but it behaves strangely.
When I add a solid ground to my world, the dynamic box flies upwards (instead downwards) to something about y=-275, which I dont understand.
When I remove the solid ground, the simulation is quite all right, the box falls just as it should fall.
Anyway, here's the code:
#include <Box2D/Box2D.h>
#include "SFML/Graphics.hpp"
#include <iostream>
int main()
{
b2World world(b2Vec2(0.0f, 0.5f));
b2BodyDef boxDef;
boxDef.type = b2_dynamicBody;
boxDef.position.Set(300.0f/50,100.0f/50);
b2Body *boxB = world.CreateBody(&boxDef);
b2PolygonShape boxShape;
boxShape.SetAsBox(30.0f/2,30.0f/2);
b2FixtureDef boxFixtureDef;
boxFixtureDef.shape = &boxShape;
boxB->CreateFixture(&boxFixtureDef);
//////////////////////////--------------------------------------------//////////////////////////
b2BodyDef groundDef;
groundDef.type = b2_staticBody;
groundDef.position.Set(0.0f, 500.0f/50);
b2Body *groundB = world.CreateBody(&groundDef);
// those lines can be cut out and the
//simulation will work properly
b2PolygonShape groundShape;
groundShape.SetAsBox(500.0f/50, 25.0f/50);
groundB->CreateFixture(&groundShape, 1.0f);
//////////////////////////--------------------------------------------//////////////////////////
sf::RenderWindow window(sf::VideoMode(800,600), "SFML");
sf::RectangleShape ground;
ground.setSize(sf::Vector2f(1000,50));
sf::RectangleShape box;
box.setSize(sf::Vector2f(30, 30));
box.setOrigin(15,15);
ground.setFillColor(sf::Color::Red);
box.setFillColor(sf::Color::Blue);
ground.setPosition(0, 500);
float time = 1.0f;
float steps = 60.0f;
float timeSteps = time / steps;
int i=0;
int velocityIt = 8, positionIt = 3;
while(window.isOpen())
{
sf::Event e;
while(window.pollEvent(e))
{
if(e.type == sf::Event::Closed)
{
window.close();
}
}
world.Step(timeSteps, velocityIt, positionIt);
b2Vec2 posBox = boxB->GetPosition();
box.setPosition(sf::Vector2f(posBox.x*50, posBox.y*50));
std::cout << posBox.x*50 << "," << posBox.y*50 << std::endl;
window.clear();
window.draw(box);
window.draw(ground);
window.display();
//i++;
/* if(i==25)
{
break;
}
*/
}
}
Does anyone know a solution? Because I bet I did something wrong.
Thank you.
#EDIT
Still can't get it to work properly, tho.
I don't get it, when I set a 0.0f y gravity, my dynamic object still moves.
Another weird thing... If I remove the static body (ground) the object behaves normally.
It falls as it should, but for an infinite amout of time, which is understandable with no ground being there.
Changing the gravity y from let's say 1.0f to -1.0f doesn't change anything at all.
First 25 y positions of my moving object are:
http://screenshot.sh/mMdFPYd7mpWwp (gravity is -1.0f)
http://screenshot.sh/mGx7UA0DJj7Fv (gravity is 1.0f)
But I had put the ground on -10.0f (so I guess thats 10 meters = 640 pixes in my SFML world).
I included the inversion of y axis betweend box2D and SFML coordinate system,
but the falling never stops.
If someone could change the code to make it work, I'd be really happy.
Or even some pointers at what I'm doing wrong.
Code here:
#include <Box2D/Box2D.h>
#include "SFML/Graphics.hpp"
#include <iostream>
float MTP = 64.0f;
float PTM = 1/MTP;
int main()
{
sf::RenderWindow window(sf::VideoMode(1280,720), "SFML");
sf::RectangleShape playerRect;
playerRect.setSize(sf::Vector2f(64,64));
playerRect.setFillColor(sf::Color::Blue);
sf::RectangleShape groundRect;
groundRect.setSize(sf::Vector2f(640,64));
b2World world(b2Vec2(0.0f, 1.0f));
b2BodyDef playerDef;
playerDef.type = b2_dynamicBody;
playerDef.position.Set(2.0f, 0.0f);
b2Body *playerBody = world.CreateBody(&playerDef);
b2PolygonShape playerShape;
playerShape.SetAsBox(32, 32);
b2FixtureDef playerFixDef;
playerFixDef.shape = &playerShape;
playerFixDef.density = 1.0f;
playerFixDef.friction = 0.3f;
playerBody->CreateFixture(&playerFixDef);
playerRect.setPosition(sf::Vector2f((playerBody->GetPosition().x)*MTP, -(playerBody->GetPosition().y)*MTP));
std::cout << (playerBody->GetPosition().x)*MTP << ", " << (playerBody->GetPosition().y)*MTP << std::endl;
b2BodyDef groundDef;
groundDef.type = b2_staticBody;
groundDef.position.Set(0, -10.0f);
b2Body *groundBody = world.CreateBody(&groundDef);
b2PolygonShape groundShape;
groundShape.SetAsBox(320,32);
groundBody->CreateFixture(&groundShape, 1.0f);
groundRect.setPosition(sf::Vector2f((groundBody->GetPosition().x)*MTP, -((groundBody->GetPosition().y)*MTP)));
float time = 1.0f;
float steps = 60.f;
float timeSteps = time / steps;
int velIt = 8;
int posIt = 3;
while(window.isOpen())
{
sf::Event e;
while(window.pollEvent(e))
{
if(e.type == sf::Event::Closed)
{
window.close();
}
}
world.Step(timeSteps, velIt, posIt);
playerRect.setPosition(sf::Vector2f((playerBody->GetPosition().x)*MTP, -(playerBody->GetPosition().y)*MTP));
std::cout << (playerBody->GetPosition().x)*MTP<< ", " << -(playerBody->GetPosition().y)*MTP << std::endl;
window.clear();
window.draw(playerRect);
window.draw(groundRect);
window.display();
}
}