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

Author Topic: Help with Box2D (Duplicate sprites)  (Read 1193 times)

0 Members and 1 Guest are viewing this topic.

matiasng37

  • Newbie
  • *
  • Posts: 2
    • View Profile
Help with Box2D (Duplicate sprites)
« on: May 03, 2017, 11:27:28 pm »
Hello! I'm new to the forums, so excuse me if I'm breaking any rules  :-X

I'd like to request help with a little issue I have with the implementation of Box2D in my game.
The problem is that whenever I draw my sprites, I get a box and a player on top of each other for each object on screen. I can see where my code fails, but I have no idea what I should do to make it work properly.
This is my current code:

Quote
for (b2Body* BodyIterator = World.GetBodyList(); BodyIterator != 0; BodyIterator = BodyIterator->GetNext()){
            if (BodyIterator->GetType() == b2_dynamicBody)
                        {
                            box.DrawBox2DBody(window, BodyIterator); // Class details below
                            player.DrawBox2DBody(window, BodyIterator);
                        }
}

Quote
void Object::DrawBox2DBody(sf::RenderWindow &window, b2Body *BodyIterator)
{
    _sprite.setOrigin(sf::Vector2f(_sprite.getTextureRect().width / 2, _sprite.getTextureRect().height / 2));
    _sprite.setPosition(SCALE * BodyIterator->GetPosition().x, SCALE * BodyIterator->GetPosition().y);
    _sprite.setRotation(BodyIterator->GetAngle() * 180/b2_pi);

    window.draw(_sprite);
}

I hope this is enough information. Thanks in advance!

EDIT
i think one solution could be to access each object's body separately so that I can draw them one by one. I tried to do that without sucess. That should work. Right?
« Last Edit: May 04, 2017, 12:01:14 am by matiasng37 »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help with Box2D (Duplicate sprites)
« Reply #1 on: May 03, 2017, 11:34:44 pm »
I can see where my code fails
Where?
And why do you think it's there?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

matiasng37

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Help with Box2D (Duplicate sprites)
« Reply #2 on: May 04, 2017, 01:34:36 am »
The problem is that for each body (2) I'm drawing both the box and the player. So I get four of them.

EDIT
I found a not very nice solution. I will improve it and then share it. In case someone else needs it.
« Last Edit: May 04, 2017, 03:20:04 am by matiasng37 »