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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - lieukhuong

Pages: [1]
1
Graphics / Re: Unwanted extra drawing at box2d bodydef locations
« 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!

2
Graphics / 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)


Pages: [1]