Hi all.
Please help me to understand. I'm trying to understand the positioning, size, and in general familiarizing box2d and sfml.
I have the 2nd day of trying to understand but can not: (
I am writing a simple example and that's what I get.

Why falls boxing. It behaves quite strange at all ... I have the impression that the yellow square that I draw sfml not display the behavior of the body.
Please help me. I do not understand what is wrong. What to read, where to look .... tutorials and manuals are rather scanty (sorry for my opinion is not professionally).
I understand that the problem could be in my use of SFML but if you can tell me what I'm doing wrong.
Code:
Code:
//main.cpp file
#include <iostream>
#include <Box2D/Box2D.h>
#include <SFML/Graphics.hpp>
#include <sstream>
#include <ctime>
//#include "box.h"
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
#include <stdlib.h>
const int PPM = 30;
using namespace std;
class QueryCallback : public b2QueryCallback
{
public:
QueryCallback(const b2Vec2& point)
{
m_point = point;
m_fixture = NULL;
}
bool ReportFixture(b2Fixture* fixture)
{
b2Body* body = fixture->GetBody();
if (body->GetType() == b2_dynamicBody)
{
bool inside = fixture->TestPoint(m_point);
if (inside)
{
m_fixture = fixture;
// We are done, terminate the query.
return false;
}
}
// Continue the query.
return true;
}
b2Vec2 m_point;
b2Fixture* m_fixture;
};
struct body
{
b2BodyDef bodyDef;
b2PolygonShape shape;
b2FixtureDef fixtureDef;
b2Body * bod;
sf::RectangleShape rect;
};
int main()
{
sf::RenderWindow AppWnd(sf::VideoMode(800,600,32), "Application");
b2Vec2 gravity(0.0f, 9.8f);
b2World world(gravity);
b2Vec2 mousePos;
b2MouseJoint* mouseJoint = NULL;
body ground;
ground.bodyDef.position.Set(400.0f,600.0f);
ground.bod = world.CreateBody(&ground.bodyDef);
ground.shape.SetAsBox(400.0f,20.0f);
ground.bod->CreateFixture(&ground.shape, 0.0f);
ground.rect = sf::RectangleShape(sf::Vector2f(800,20));
ground.rect.setOrigin(0,20);
ground.rect.setFillColor(sf::Color::Green);
body left_wall;
left_wall.bodyDef.position.Set(10.0f,300.0f);
left_wall.bod = world.CreateBody(&left_wall.bodyDef);
left_wall.shape.SetAsBox(10.0f,300.0f);
left_wall.bod->CreateFixture(&left_wall.shape, 1.0f);
left_wall.rect = sf::RectangleShape(sf::Vector2f(20,600));
left_wall.rect.setOrigin(10,0);
left_wall.rect.setFillColor(sf::Color::Magenta);
body right_wall;
right_wall.bodyDef.position.Set(790.0f,300.0f);
right_wall.bod = world.CreateBody(&right_wall.bodyDef);
right_wall.shape.SetAsBox(10.0f,300.0f);
right_wall.bod->CreateFixture(&right_wall.shape, 1.0f);
right_wall.rect = sf::RectangleShape(sf::Vector2f(20,600));
right_wall.rect.setOrigin(10,0);
right_wall.rect.setFillColor(sf::Color::Magenta);
body top_wall;
top_wall.bodyDef.position.Set(400.0f,10.0f);
top_wall.bod = world.CreateBody(&top_wall.bodyDef);
top_wall.shape.SetAsBox(400.0f,10.0f);
top_wall.bod->CreateFixture(&top_wall.shape, 1.0f);
top_wall.rect = sf::RectangleShape(sf::Vector2f(800,20));
top_wall.rect.setOrigin(0,10);
top_wall.rect.setFillColor(sf::Color::Blue);
body dynamBody;
dynamBody.bodyDef.type = b2_dynamicBody;
dynamBody.bodyDef.position.Set(300.0f, 300.0f);
dynamBody.bod = world.CreateBody(&dynamBody.bodyDef);
dynamBody.shape.SetAsBox(20.0f,20.0f);
dynamBody.fixtureDef.shape = &dynamBody.shape;
dynamBody.fixtureDef.density = 100.0f;
dynamBody.fixtureDef.friction = 0.1f;
dynamBody.fixtureDef.restitution = 0.5f;
dynamBody.bod->CreateFixture(&dynamBody.fixtureDef);
dynamBody.rect = sf::RectangleShape(sf::Vector2f(40,40));
dynamBody.rect.setOrigin(20,20);
dynamBody.rect.setFillColor(sf::Color::Yellow);
float timeStep = 1.0f / 60.0f;
//parametri silmylacii fiziki
int32 velocity_iterations = 7;
int32 position_iterations = 3;
while(AppWnd.isOpen())
{
world.Step(timeStep, velocity_iterations, position_iterations);
sf::Event event;
while (AppWnd.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
{
AppWnd.close();
};break;
case sf::Event::MouseButtonPressed:
{
if(event.mouseButton.button == sf::Mouse::Left)
{
mousePos = b2Vec2(event.mouseButton.x, event.mouseButton.y);
if(mouseJoint == NULL)
{
// Make a small box.
b2AABB aabb;
b2Vec2 d;
d.Set(0.001f, 0.001f);
aabb.lowerBound = mousePos - d;
aabb.upperBound = mousePos + d;
// Query the world for overlapping shapes.
QueryCallback callback(mousePos);
world.QueryAABB(&callback, aabb);
if (callback.m_fixture)
{
b2Body* body = callback.m_fixture->GetBody();
b2MouseJointDef md;
md.bodyA = ground.bod;
md.bodyB = body;
md.target = mousePos;
md.maxForce = 50.0f * body->GetMass();
mouseJoint = (b2MouseJoint*)world.CreateJoint(&md);
body->SetAwake(true);
}
}
}
};break;
case sf::Event::MouseButtonReleased:
{
if(event.mouseButton.button == sf::Mouse::Left)
{
if ( mouseJoint != NULL )
{
world.DestroyJoint( mouseJoint );
mouseJoint = NULL;
}
}
};break;
case sf::Event::MouseMoved:
{
mousePos = b2Vec2(event.mouseButton.x, event.mouseButton.y);
if ( mouseJoint != NULL )
{
mouseJoint->SetTarget( b2Vec2( mousePos.x, mousePos.y ) );
}
};break;
}
}
ground.rect.setPosition(ground.bod->GetPosition().x - ground.bod->GetPosition().x, ground.bod->GetPosition().y);
left_wall.rect.setPosition(left_wall.bod->GetPosition().x, left_wall.bod->GetPosition().y - left_wall.bod->GetPosition().y);
right_wall.rect.setPosition(right_wall.bod->GetPosition().x, right_wall.bod->GetPosition().y - right_wall.bod->GetPosition().y);
top_wall.rect.setPosition(top_wall.bod->GetPosition().x - top_wall.bod->GetPosition().x, top_wall.bod->GetPosition().y);
dynamBody.rect.setPosition(dynamBody.bod->GetPosition().x, dynamBody.bod->GetPosition().y);
dynamBody.rect.setRotation(dynamBody.bod->GetAngle());
cout << "body pos.x:"<< dynamBody.bod->GetPosition().x << " y: " << dynamBody.bod->GetPosition().y <<endl;
cout << "rect pos.x:" << dynamBody.rect.getPosition().x << " y:" << dynamBody.rect.getPosition().y <<endl;
cout << "body->angle:" << dynamBody.bod->GetAngle() << " rect.rotation:" << dynamBody.rect.getRotation() << endl;
AppWnd.clear();
AppWnd.draw(ground.rect);
AppWnd.draw(left_wall.rect);
AppWnd.draw(right_wall.rect);
AppWnd.draw(top_wall.rect);
AppWnd.draw(dynamBody.rect);
AppWnd.display();
//Sleep(500);
}
return 0;
}