Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Box2D/Box2D.h>
- #include <SFML/Graphics.hpp>
- int main()
- {
- sf::RectangleShape rectRender(sf::Vector2f(100,50));
- b2World world(b2Vec2(0, 100));
- world.SetGravity(b2Vec2(0, 100));
- b2BodyDef boxDef;
- boxDef.type = b2_kinematicBody;
- boxDef.position.Set(50, 100);
- b2FixtureDef boxFixDef;
- boxFixDef.friction = 0;
- boxFixDef.restitution = 0;
- b2PolygonShape boxShape;
- boxShape.SetAsBox(50, 25);
- boxFixDef.shape = &boxShape;
- b2Body* body = world.CreateBody(&boxDef);
- body->CreateFixture(&boxFixDef);
- body->SetLinearDamping(0);
- sf::RectangleShape rectRender2(sf::Vector2f(100, 50)); rectRender2.setFillColor(sf::Color::Red);
- b2BodyDef boxDef2;
- boxDef2.type = b2_dynamicBody;
- boxDef2.position.Set(50, 10);
- b2FixtureDef boxFixDef2;
- boxFixDef2.restitution = 0;
- boxFixDef.friction = 0;
- boxFixDef2.density = 20;
- b2PolygonShape boxShape2;
- boxShape2.SetAsBox(50, 25);
- boxFixDef2.shape = &boxShape;
- b2Body* body2 = world.CreateBody(&boxDef2);
- body2->CreateFixture(&boxFixDef2);
- body2->SetLinearDamping(0);
- sf::RenderWindow window(sf::VideoMode(1280, 720), "Box2D Test");
- window.setFramerateLimit(60);
- while (window.isOpen())
- {
- world.Step(1 / 60.f, 8, 3);
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- if (event.type == sf::Event::KeyPressed)
- {
- body->SetTransform(b2Vec2(body->GetPosition().x + 10, body->GetPosition().y), 0);
- }
- }
- window.clear();
- rectRender.setPosition(body->GetPosition().x, body->GetPosition().y);
- rectRender2.setPosition(body2->GetPosition().x, body2->GetPosition().y);
- window.draw(rectRender);
- window.draw(rectRender2);
- window.display();
- std::cout << body2->GetLinearVelocity().y << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement