Box2D Forums

It is currently Sat May 18, 2013 3:09 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Jul 13, 2012 4:02 am 
Offline

Joined: Fri Jul 13, 2012 3:40 am
Posts: 2
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.

Image

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





Attachments:
main.cpp [6.12 KiB]
Downloaded 36 times
Top
 Profile  
 
PostPosted: Fri Jul 13, 2012 7:57 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
Box2D uses radians. Try something like this...
Code:
#define DEGTORAD 0.0174532925199432957f
#define RADTODEG 57.295779513082320876f
dynamBody.rect.setRotation( dynamBody.bod->GetAngle() * RADTODEG );
Some other things I noticed:
- SFML has a nice portable sleep function, you could use sf::sleep( sf::seconds( timeStep ) );
- the yellow block is about the size of a four storey building and weighs 160 tons. To move it around with the mouse joint you will need a much larger maxForce, more like 5000. These kind of huge sizes and densities may not work well, but if you really want to keep them like that you can increase the gravity to make things appear smaller.
- by default two bodies connected by a joint will not collide with each other, you can do md.collideConnected = true; to change this or use another body as body A of the mouse joint.
- I think this line needs changing - for mouse move events only the mouseMove member of the event is valid:
Code:
case sf::Event::MouseMoved:
{
   //mousePos = b2Vec2(event.mouseButton.x, event.mouseButton.y);
   mousePos = b2Vec2(event.mouseMove.x, event.mouseMove.y);


Top
 Profile  
 
PostPosted: Sun Jul 15, 2012 11:16 pm 
Offline

Joined: Fri Jul 13, 2012 3:40 am
Posts: 2
Thank you, I hope soon'll ...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group