#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();
}
}