Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have problem with simple box2d physics: I have two bodies : 1 - static box , 2 - dynamic box. I only want to this dynamic box fall on this static, and lay. But now it looks like that at end alt text, starting from alt text . This is code, which bases on box2d HelloWorld:

#include <SFML/Graphics.hpp>
#include <Box2D/Box2D.h>
#include <cstdio>


int main(int argc, char* argv[])
{
    B2_NOT_USED(argc);
 B2_NOT_USED(argv);

 b2Vec2 gravity(0.0f, -10.0f);
 bool doSleep = true;
 b2World world(gravity, doSleep);

 b2BodyDef groundBodyDef;
 groundBodyDef.position.Set(0.0f, -10.0f);
 b2Body* groundBody = world.CreateBody(&groundBodyDef);
 b2PolygonShape groundBox;
 groundBox.SetAsBox(50.0f, 10.0f);
 groundBody->CreateFixture(&groundBox, 0.0f);


 b2BodyDef bodyDef;
 bodyDef.type = b2_dynamicBody;
 bodyDef.position.Set(0.0f, 300.0f);
 bodyDef.active = true;
 b2Body* body = world.CreateBody(&bodyDef);
 b2PolygonShape dynamicBox;
 dynamicBox.SetAsBox(3.5f, 3.5f);


 body->SetTransform(b2Vec2(0,100),30);

 b2FixtureDef fixtureDef;
 fixtureDef.shape = &dynamicBox;
 fixtureDef.density = 1.0f;
 fixtureDef.friction = 0.3;

 body->CreateFixture(&fixtureDef);

 float32 timeStep = 1.0f / 60.0f;
 int32 velocityIterations = 6;
 int32 positionIterations = 2;


    sf::RenderWindow app(sf::VideoMode(800, 600), "BOX/SFML");
 app.UseVerticalSync(true);
 sf::Shape shape = sf::Shape::Rectangle(-3.5f,-3.5f,3.5f,3.5f,sf::Color::Green);
 sf::Shape groundShape = sf::Shape::Rectangle(-50,-10,50,10,sf::Color(100,40,120));

    while (app.IsOpened())
    {

        sf::Event event_;
        while (app.GetEvent(event_))
        {

            if (event_.Type == sf::Event::Closed)
                app.Close();
        }

  world.Step(timeStep, velocityIterations, positionIterations);
        world.ClearForces();

  b2Vec2 position = body->GetPosition();
  float32 angle = body->GetAngle();
  b2MassData m;

  body->GetMassData(&m);
  printf("m %f\n",m);
  printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);

  app.Clear();
  shape.SetPosition(position.x+400,-position.y+300);
  shape.SetRotation(angle);
  app.Draw(shape);
  groundShape.SetPosition(0+400,10+300);
  app.Draw(groundShape);

        app.Display();
    }
 world.DestroyBody(groundBody);
 world.DestroyBody(body);
    return EXIT_SUCCESS;
}

I looking for any advice for solve this problem. And another problem, why mass of dynamic shape is 0 ?

share|improve this question
    
somebody please clean this post up. Change the image links to embedded images and use consistent indentation in the code. –  deft_code Aug 26 '10 at 16:36

1 Answer 1

up vote 1 down vote accepted

I've found I hate SFML when dealing with Box2D. It's concept of inverted-y and pixel based coordinates is great if you want to draw a quick box. It's horrible if you need to tie its coordinate system into another library (which you will need to do in any game that's not an SFML demo).

My recommendation: use Box2D's debug rendering. That way when you see an object in the middle of the screen you know it's actually there. The sf::Shapes could be way off of the underlying b2Body.

share|improve this answer
    
Thx, I find that I use another angle unit for SFML's rotate. But I still don't know why mass == 0 . –  youngProgrammer Aug 26 '10 at 17:32
    
You can use an sf::View to solve the issues with pixel-based coordinates. But yes, inverted-y and its insistence on degrees for angles drives me nuts. –  user744 Aug 27 '10 at 16:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.