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

Author Topic: Unwanted extra drawing at box2d bodydef locations  (Read 1444 times)

0 Members and 1 Guest are viewing this topic.

lieukhuong

  • Newbie
  • *
  • Posts: 2
    • View Profile
Unwanted extra drawing at box2d bodydef locations
« on: November 15, 2017, 08:44:35 pm »
Hi guys,
So with my code:
Quote
#ifndef GAME_H_
#define GAME_H_
#define SCALE 30.f
#define DEG 57.29577f

#include <SFML/Graphics.hpp>
#include <Box2D/Box2D.h>
using namespace sf;
class game{
public:
b2World World;
void game::setWall(int x, int y, int w, int h)
{
b2PolygonShape gr;
gr.SetAsBox(w / SCALE, h / SCALE);

b2BodyDef wbdef;
wbdef.position.Set(x / SCALE, y / SCALE); // wall position but with extra ball drawing????

b2Body *b_ground = World.CreateBody(&wbdef);
b_ground->CreateFixture(&gr, 1);
}
game::game() :World(b2Vec2(0.0f, 0.98f))
{
RenderWindow window(VideoMode(800, 600), "Volleyball Game!");
window.setFramerateLimit(60);
window.setSize(Vector2u(800.0 * 0.8, 600.0 * 0.8) );

Texture t1, t2;
t1.loadFromFile("images/background.png");
t2.loadFromFile("images/ball.png");

t1.setSmooth(true);
t2.setSmooth(true);
Sprite sBackground(t1), sBall(t2);
sBall.setOrigin(32, 32);

/////////box2d///////////
setWall(400, 620, 2000, 10); ///Draw with ball
setWall(0, 0, 10, 2000);        ///Draw with ball
setWall(800, 0, 10, 2000);    ///Draw with ball

b2PolygonShape shape;
shape.SetAsBox(30 / SCALE, 30 / SCALE);
b2BodyDef bdef;
bdef.type = b2_dynamicBody;
/// ball /////////////
bdef.position.Set(1, 1);
b2CircleShape circle;
circle.m_radius = 32 / SCALE;
b2Body *b = World.CreateBody(&bdef);
b2FixtureDef fdef;
fdef.shape = &circle;
fdef.restitution = 0.95f;
fdef.density = 0.2f;
b->CreateFixture(&fdef);
/////////////////////////
bool onGround = 0;
while (window.isOpen())
{
Event e;
while (window.pollEvent(e))
{
if (e.type == Event::Closed)
window.close();
}
for (int n = 0; n<2; n++) // 2 - speed
World.Step(1 / 60.f, 8, 3);
//////////Draw///////////////
window.draw(sBackground);
for (b2Body* it = World.GetBodyList(); it != 0; it = it->GetNext())
{
b2Vec2 pos = it->GetPosition();
float angle = it->GetAngle();
sBall.setPosition(pos.x * SCALE, pos.y*SCALE);
sBall.setRotation(angle * DEG);
window.draw(sBall);
}
window.display();
}
}
};
#endif;

So I am trying my luck as I'm not sure this is a right place to ask regarding sfml & box2d combining
Does anyone know why I keep getting extra balls draw at locations of the walls?(highlight and pic)

« Last Edit: November 15, 2017, 08:51:39 pm by eXpl0it3r »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Unwanted extra drawing at box2d bodydef locations
« Reply #1 on: November 15, 2017, 09:26:25 pm »
I don't know anything about Box2D, but looking at the code
for (b2Body* it = World.GetBodyList(); it != 0; it = it->GetNext())
{
   b2Vec2 pos = it->GetPosition();
   float angle = it->GetAngle();
   sBall.setPosition(pos.x * SCALE, pos.y*SCALE);
   sBall.setRotation(angle * DEG);
   window.draw(sBall);
}
 

Won't iterating over World.GetBodyList() give you 4 objects (3 walls and 1 ball)? Which means for each object, including the walls, you are moving the ball sprite to its location and drawing it. Seems pretty straightforward, but I may be misunderstanding something.

lieukhuong

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Unwanted extra drawing at box2d bodydef locations
« Reply #2 on: November 16, 2017, 12:23:57 am »
Thanks a lot man!
I copied these codes from other and try to modify it.
Apparently I deleted the code that identify my ball object in the loop
I got it now. Thanks again!

 

anything