Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Another problem with SFML and Box2d! :)  (Read 2177 times)

0 Members and 2 Guests are viewing this topic.

W4ffleKing

  • Newbie
  • *
  • Posts: 3
    • View Profile
Another problem with SFML and Box2d! :)
« on: August 28, 2012, 04:34:15 am »
I know there are already many posts for similar issues, but I've read all of them and am somehow still struggling to get SFML shapes to align to Box2d bodies. The SFML shapes seem to be larger than the Box2d bodies and are offset. It'll probably be something simple, but I've checked over and over again and can't see the problem...

To create an 'object', I give SetAsBox() half of width/height, convert angles between radians and degrees, setOrigin() to half of width/height and divide by my scale (30) where necessary. In my main loop I setPosition() to the body's position * 30, and setRotation() to the body's angle * (180 / b2_pi).

My code is pretty disorganised at the moment, so here are the important bits...

Creating body and shape:

//phys body
        b2BodyDef tbodyDef;
        tbodyDef.type = b2_dynamicBody;
        tbodyDef.position.Set(500 / 30, 500 / 30);
        tbodyDef.angle = 0;
        tbody = world->CreateBody(&tbodyDef);
        b2PolygonShape tdynamicBox;
        tdynamicBox.SetAsBox(75 / 2 / 30, 75 / 2 / 30);
        b2FixtureDef tfixtureDef;
        tfixtureDef.shape = &tdynamicBox;
        tfixtureDef.density = 1.0f;
        tfixtureDef.friction = 0.3f;
        tbody->CreateFixture(&tfixtureDef);

        //sfml
        tbox = new sf::RectangleShape;
        tbox->setSize(sf::Vector2f(75, 75));
        tbox->setFillColor(sf::Color(163, 36, 18, 100));
        tbox->setOrigin(75 / 2, 75 / 2);
        tbox->setRotation(0);

In main loop:

b2Vec2 position2 = tbody->GetPosition();
        float32 angle2 = tbody->GetAngle();
        tbox->setPosition((position2.x * 30), (position2.y * 30));
        tbox->setRotation(angle2 * (180 / b2_pi));
        mainWindow->draw(*tbox);

After many hours of confusion and frustration, I give up and hope someone can help me out. Apologies if I missed anything important and for my poor explanation, thanks in advance! :D

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Another problem with SFML and Box2d! :)
« Reply #1 on: August 28, 2012, 05:22:53 am »
May or may not be it : add .f to every int constant that you use as float.
Every time you do something like 500/30 or 75/2/30 it performs on integers, in order from left to right and truncs evrything.
500/30 is 16, 75/2 is 37, these are small enough errors.
75/2/30 is 1, not 1.25, that's 25% size difference.
Back to C++ gamedev with SFML in May 2023

W4ffleKing

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Another problem with SFML and Box2d! :)
« Reply #2 on: August 28, 2012, 07:52:14 pm »
Thanks for the suggestion, I tried it but the problem still exists. I attached an image of the problem, thanks again. :D

[attachment deleted by admin]

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Another problem with SFML and Box2d! :)
« Reply #3 on: August 28, 2012, 08:00:33 pm »
How am I supposed to know what is happening from a still image, show the code.
Back to C++ gamedev with SFML in May 2023

W4ffleKing

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Another problem with SFML and Box2d! :)
« Reply #4 on: August 28, 2012, 09:38:33 pm »
Oh my... While stripping out the irrelevant stuff to post my code, I noticed I had missed a few uncasted integers. Apologies for the confusion, all is fine now :) Thank you FRex for your help and patience :D