Professional Documents
Culture Documents
hpp>
#include <Box2D/Box2D.h>
class Platform {
public:
Platform(b2World &world, float x, float y, int dynamic = 0,float density =
1.0f) {
b2BodyDef bodydef;
bodydef.position.Set(x / SCALE, y / SCALE);
switch (dynamic) {
case 0:
bodydef.type = b2_staticBody;
break;
case 1:
bodydef.type = b2_dynamicBody;
break;
case 2:
bodydef.type = b2_kinematicBody;
break;
}
body = world.CreateBody(&bodydef);
b2PolygonShape shape;
shape.SetAsBox((80.f / 2) / SCALE, (10.f / 2) / SCALE);
b2FixtureDef fixturedef;
fixturedef.shape = &shape;
fixturedef.density = density;
body->CreateFixture(&fixturedef);
box.setSize(sf::Vector2f(80.f, 10.f));
box.setFillColor(sf::Color::Red);
box.setOrigin(5.f, 5.f);
}
void update() {
box.setPosition(SCALE * body->GetPosition().x, SCALE * body-
>GetPosition().y);
box.setRotation(body->GetAngle() * 180 / b2_pi);
}
void resetPosition() {
body->SetTransform(b2Vec2(0, 0), body->GetAngle());
}
void stop() {
body->SetLinearVelocity(b2Vec2(0, 0));
}
b2Body *getBody() {
return body;
}
private:
sf::RectangleShape box;
b2Body *body;
};
int main() {
sf::RenderWindow window(sf::VideoMode(1000, 1000), "Space Invaders v0.0.1!");
/* b2PrismaticJointDef jointDef;
jointDef.Initialize(cube.getBody(), cube3.getBody(), cube3.getBody()-
>GetWorldCenter(), b2Vec2(1.0f, 0.0f));
jointDef.lowerTranslation = -30.0f;
jointDef.upperTranslation = 30.0f;
jointDef.enableLimit = true;
b2PrismaticJointDef jointDef2;
jointDef2.Initialize(cube2.getBody(), cube3.getBody(), cube3.getBody()-
>GetWorldCenter(), b2Vec2(0.0f, 1.0f));
jointDef2.lowerTranslation = -3dddd0.0f;
jointDef2.upperTranslation = 30.0f;
jointDef2.enableLimit = true;
*/ b2RevoluteJointDef jointDef3;
gearJointDef.bodyA = cube.getBody();
gearJointDef.bodyB = cube2.getBody();
gearJointDef.joint1 = joint;
gearJointDef.joint2 = joint2;
gearJointDef.ratio = 0.2f;
world.CreateJoint(&gearJointDef);
*/
while (window.isOpen()) {
sf::Event event{};
while (window.pollEvent(event)) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
cube.getBody()->ApplyForceToCenter(b2Vec2(0.1f, 0.0f), true);
cube2.getBody()->ApplyForceToCenter(b2Vec2(0.1f, 0.0f), true);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
cube.getBody()->ApplyForceToCenter(b2Vec2(-0.1f, 0.0f), true);
cube2.getBody()->ApplyForceToCenter(b2Vec2(-0.1f, 0.0f), true);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
cube.getBody()->ApplyForceToCenter(b2Vec2(0.0f, -0.1f), true);
cube2.getBody()->ApplyForceToCenter(b2Vec2(0.0f, -0.1f), true);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
cube.getBody()->ApplyForceToCenter(b2Vec2(0.0f, 0.1f), true);
cube2.getBody()->ApplyForceToCenter(b2Vec2(0.0f, 0.1f), true);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
cube.resetPosition();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::R)) {
cube.stop();
cube2.stop();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) {
window.close();
}
}
world.Step(1 / 60.f, 8, 3);
cube.update();
cube2.update();
cube3.update();
cube4.update();
window.clear();
cube.draw(window);
cube2.draw(window);
cube3.draw(window);
cube4.draw(window);
window.display();
}
return 0;
}