Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Box2D/Box2D.h>
- #include <SFML/Graphics.hpp>
- #include "CPlayer.h"
- #include <string>
- #define DEGTORAD 0.0174532925199432957f
- #define RADTODEG 57.295779513082320876f
- #define PIXPERMET 0.02
- #define METPERPIX 50
- #include <iostream>
- using namespace std;
- // KeyPressed bools
- bool m_Space = false;
- bool m_A = true; bool m_ACanMove = true;
- bool m_D = true; bool m_DCanMove = true;
- // body height
- float m_dynamicBodyDef_height_METER = 32.0f * PIXPERMET;
- // Prototype
- void isKeyPressed(b2World &world, b2Body *body1, b2Body *edgeBody);
- bool atFinistLine(b2Body &body1);
- // ContactListener
- class MyContactListener : public b2ContactListener
- {
- void BeginContact(b2Contact* contact)
- {
- // Bodys
- b2Body *bodyA = contact->GetFixtureA()->GetBody();
- b2Body *bodyB = contact->GetFixtureB()->GetBody();
- // Einer der Bodies Spieler?
- if ((int)bodyA->GetUserData() == 1)
- {
- if ((int)bodyB->GetUserData() == 0)
- {
- m_Space = true;
- }
- }
- else if ((int)bodyB->GetUserData() == 1)
- {
- if ((int)bodyA->GetUserData() == 0)
- m_Space = true;
- }
- }
- void EndContact(b2Contact* contact) {}
- };
- int main(int argc, char** argv)
- {
- // Timer
- sf::Clock m_Clock;
- float m_dt, m_fullTime, m_VelocityFullTime;
- // Werte für die Physik
- float32 timeStep = 1.0f / 60.0f;
- int32 velocityIterations = 10;
- int32 positionIterations = 8;
- // Fenster öffnen
- sf::RenderWindow m_MainWindow;
- m_MainWindow.create(sf::VideoMode(1920.0f, 1080.0f), "Test Anwendung Box2D", sf::Style::Fullscreen);
- // Welt erstellen
- b2Vec2 m_Gravity(0.0f, 9.81f);
- b2World *m_World = new b2World(m_Gravity);
- // ContactListener
- b2ContactListener *ContactListener = new MyContactListener;
- m_World->SetContactListener(ContactListener);
- // Edge erstellen
- // EdgeBody Def
- b2BodyDef m_edgeBodyDef;
- m_edgeBodyDef.type = b2_staticBody;
- m_edgeBodyDef.position.Set(0 * PIXPERMET, 0 * PIXPERMET);
- b2Body *m_edgeBody = m_World->CreateBody(&m_edgeBodyDef);
- int UserDataEdgeBody = 0;
- m_edgeBody->SetUserData((void*)UserDataEdgeBody);
- b2Body *m_edgeBodyL = m_World->CreateBody(&m_edgeBodyDef);
- b2Body *m_edgeBodyR = m_World->CreateBody(&m_edgeBodyDef);
- // Edge Shape
- b2EdgeShape m_EdgeShape;
- m_EdgeShape.Set(b2Vec2(0.0f * PIXPERMET, (1080.0f) * PIXPERMET),
- b2Vec2(1920.0f * PIXPERMET, (1080.0f) * PIXPERMET));
- // Fixture Template static erstellen
- b2FixtureDef m_edgeFixtureDef;
- m_edgeFixtureDef.shape = &m_EdgeShape;
- m_edgeBody->CreateFixture(&m_edgeFixtureDef);
- m_edgeFixtureDef.density = 0;
- // EdgeShapeL
- m_EdgeShape.Set(b2Vec2((0.0f) * PIXPERMET, 0.0f * PIXPERMET),
- b2Vec2((0.0f) * PIXPERMET, 1080.0f * PIXPERMET));
- m_edgeFixtureDef.shape = &m_EdgeShape;
- m_edgeBodyL->CreateFixture(&m_edgeFixtureDef);
- // EdgeShapeR
- m_EdgeShape.Set(b2Vec2((1920.0f) * PIXPERMET, 0.0f * PIXPERMET),
- b2Vec2((1920.0f) * PIXPERMET, 1080.0f * PIXPERMET));
- m_edgeFixtureDef.shape = &m_EdgeShape;
- m_edgeBodyL->CreateFixture(&m_edgeFixtureDef);
- // Body Template erstellen
- b2BodyDef m_dynamicBodyDef;
- m_dynamicBodyDef.type = b2_dynamicBody;
- m_dynamicBodyDef.position.Set(64.0f * PIXPERMET, 64.0f * PIXPERMET);
- // Body (Template1) erstellen
- b2Body *m_dynamicBody_1 = m_World->CreateBody(&m_dynamicBodyDef);
- int UserDataPlayerBody = 1;
- m_dynamicBody_1->SetUserData((void*)UserDataPlayerBody);
- m_dynamicBody_1->SetFixedRotation(true);
- // Shape Template erstellen
- b2PolygonShape m_polygoneShape;
- m_polygoneShape.SetAsBox(64.0f * PIXPERMET, 64.0f * PIXPERMET);
- // Fixture Template erstellen
- b2FixtureDef m_fixtureDef;
- m_fixtureDef.shape = &m_polygoneShape;
- m_fixtureDef.density = 70.0f;
- m_dynamicBody_1->CreateFixture(&m_fixtureDef);
- // Gegner BodyTemplate
- b2BodyDef m_enemyBodyDef;
- m_enemyBodyDef.type = b2_staticBody;
- m_enemyBodyDef.position.Set(1000.0f * PIXPERMET, (800.0f) * PIXPERMET);
- // Body GegnerTemplate 1
- b2Body *m_enemyBody = m_World->CreateBody(&m_enemyBodyDef);
- int UserDataEnemy = 2;
- m_enemyBody->SetUserData((void*)UserDataEnemy);;
- // Shape
- b2PolygonShape m_enemyShape;
- m_enemyShape.SetAsBox(64.0f * PIXPERMET, 64.0f * PIXPERMET);
- // Gegner Fixture Template
- b2FixtureDef m_enemyFixtureDef;
- m_enemyFixtureDef.shape = &m_enemyShape;
- m_enemyBody->CreateFixture(&m_enemyFixtureDef);
- // Sprites laden
- sf::Sprite m_Block, m_Enemy;
- sf::Texture m_Block_Texture, m_EnemyTexture;
- sf::IntRect m_Block_Rect, m_EnemyRect;
- m_Block_Texture.loadFromFile("Data/Graphics/block.png");
- m_Block_Rect.left = 0.0f;
- m_Block_Rect.top = 0.0f;
- m_Block_Rect.width = 64.0f;
- m_Block_Rect.height = 64.0f;
- m_Block.setTexture(m_Block_Texture);
- m_Block.setTextureRect(m_Block_Rect);
- m_Block.setOrigin(32.0f, 32.0f);
- m_EnemyTexture.loadFromFile("Data/Graphics/block.png");
- m_EnemyRect.left = 0.0f;
- m_EnemyRect.top = 0.0f;
- m_EnemyRect.width = 64.0f;
- m_EnemyRect.height = 64.0f;
- m_Enemy.setTexture(m_EnemyTexture);
- m_Enemy.setTextureRect(m_EnemyRect);
- m_Enemy.setOrigin(32.0f, 32.0f);
- // Hintergrund
- sf::Sprite m_Background_S;
- sf::Texture m_Background_T;
- sf::IntRect m_Background_R;
- m_Background_T.loadFromFile("D:/Projects/Box2DProjects/Box2DTestProgramm/Data/Graphics/Background-Image.png");
- m_Background_R.left = 0;
- m_Background_R.top = 0;
- m_Background_R.width = 512.0f;
- m_Background_R.height = 320.0f;
- m_Background_S.setTexture(m_Background_T);
- m_Background_S.setTextureRect(m_Background_R);
- m_Background_S.setScale(sf::Vector2f(3.75, 3.375));
- m_Background_S.setPosition(0.0f, 0.0f);
- // Für dt und Zeit
- m_Clock.restart();
- m_dt = 0;
- m_fullTime = 0;
- m_VelocityFullTime = 0;
- // Main Loop
- while (m_MainWindow.isOpen())
- {
- // Alle SFML Bilder löschen
- m_MainWindow.clear();
- // Spiel beenden?
- if (atFinistLine(*m_dynamicBody_1))
- m_MainWindow.close();
- // Background malen
- m_MainWindow.draw(m_Background_S);
- // Events abfragen
- sf::Event m_Event;
- if (m_MainWindow.pollEvent(m_Event))
- {
- if (m_Event.type == sf::Event::Closed)
- return 0;
- }
- isKeyPressed(*m_World, m_dynamicBody_1, m_edgeBody);
- // Physik berechnen
- if (m_VelocityFullTime >= timeStep)
- {
- // Zeit resetten
- m_VelocityFullTime = 0.0f;
- // World Step machen
- m_World->Step(timeStep, velocityIterations, positionIterations);
- // Spieler Sprite Position setzten
- m_Block.setPosition(m_dynamicBody_1->GetPosition().x * METPERPIX, m_dynamicBody_1->GetPosition().y * METPERPIX);
- m_Block.setRotation(m_dynamicBody_1->GetAngle() * RADTODEG);
- }
- // Gegner malen
- m_Enemy.setPosition(m_enemyBody->GetPosition().x * METPERPIX, m_enemyBody->GetPosition().y * METPERPIX);
- m_Enemy.setRotation(m_enemyBody->GetAngle() * RADTODEG);
- m_MainWindow.draw(m_Enemy);
- // Spieler malen
- m_MainWindow.draw(m_Block);
- //cout << m_dynamicBody_1->GetPosition().x * METPERPIX << endl << m_dynamicBody_1->GetPosition().y * METPERPIX << endl;
- // Alles anzeigen
- m_MainWindow.display();
- // dt setzten
- m_dt = m_Clock.restart().asSeconds();
- m_fullTime += m_dt;
- m_VelocityFullTime += m_dt;
- }
- m_MainWindow.close();
- }
- // isKeyPressed()
- void isKeyPressed(b2World &world, b2Body *body1, b2Body *edgeBody)
- {
- // Jump
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && m_Space == true)
- {
- m_Space = false;
- //float impulse = body1->GetMass() * (-1080 * PIXPERMET);
- //body1->ApplyLinearImpulse(b2Vec2(0, impulse), body1->GetWorldCenter(), true);
- b2Vec2 velocity = body1->GetLinearVelocity();
- velocity.y = -12;
- body1->SetLinearVelocity(velocity);
- }
- // Teleport
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::T))
- {
- body1->SetTransform(b2Vec2(200 * PIXPERMET, 200 * PIXPERMET), 0);
- }
- // Move right
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) && m_ACanMove)
- {
- // Force
- b2Vec2 velocity = body1->GetLinearVelocity();
- float force = 0;
- if (velocity.x < 5) force = 100;
- // Impulse
- body1->ApplyLinearImpulse(b2Vec2(force, 0), body1->GetWorldCenter(), true);
- }
- // Move left
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) && m_ACanMove)
- {
- // Force
- b2Vec2 velocity = body1->GetLinearVelocity();
- float force = 0;
- if (velocity.x > -5) force = -100;
- // Impulse
- body1->ApplyLinearImpulse(b2Vec2(force, 0), body1->GetWorldCenter(), true);
- }
- }
- // atFinishLine()
- bool atFinistLine(b2Body &body1)
- {
- // Ziellinie
- float finishLineX1 = 1800.0f;
- float finishLineX2 = 1920.0f;
- float finishLineY1 = 0.0f;
- float finishLineY2 = 1080.0f;
- // Position
- float x, y;
- x = body1.GetPosition().x * METPERPIX;
- y = body1.GetPosition().y * METPERPIX;
- // Auf/Über Ziellinie?
- if (x > finishLineX1 &&
- x < finishLineX2 &&
- y > finishLineY1 &&
- y < finishLineY2)
- {
- cout << "Ziel erreicht!" << endl;
- return true;
- }
- else
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment