Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#pragma once #include <Box2D/Box2D.h> #include <SFML/Window.hpp> #include <SFML/Graphics.hpp> #include <cmath> namespace ts { class Box2DDebugDraw : public b2Draw { private: const float scale; const float lineThickness = -1.f; sf::RenderWindow *window; sf::Color GLColorToSFML(const b2Color &color, sf::Uint8 alpha = 255); sf::Vector2f B2VectorToSFVector(const b2Vec2 &vector, bool scaleToPixels = true); public: Box2DDebugDraw(sf::RenderWindow *window, const float scale); void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color); void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color); void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color); void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color); void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color); void DrawTransform(const b2Transform& xf); void DrawPoint(const b2Vec2& p, float32 size, const b2Color& color); }; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
#include "Box2DDebugDraw.hpp" sf::Color ts::Box2DDebugDraw::GLColorToSFML(const b2Color &color, sf::Uint8 alpha) { return sf::Color(static_cast<sf::Uint8>(color.r * 255), static_cast<sf::Uint8>(color.g * 255), static_cast<sf::Uint8>(color.b * 255), alpha); } sf::Vector2f ts::Box2DDebugDraw::B2VectorToSFVector(const b2Vec2 &vector, bool scaleToPixels) { return sf::Vector2f(vector.x * (scaleToPixels ? scale : 1.f), vector.y * (scaleToPixels ? scale : 1.f)); } ts::Box2DDebugDraw::Box2DDebugDraw(sf::RenderWindow *window, const float scale) : window(window), scale(scale) { } void ts::Box2DDebugDraw::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) { sf::ConvexShape polygon(vertexCount); sf::Vector2f center; for(int i = 0; i < vertexCount; i++) { sf::Vector2f transformedVec = B2VectorToSFVector(vertices[i]); polygon.setPoint(i, sf::Vector2f(std::floor(transformedVec.x), std::floor(transformedVec.y))); } polygon.setOutlineThickness(lineThickness); polygon.setFillColor(sf::Color::Transparent); polygon.setOutlineColor(GLColorToSFML(color)); window->draw(polygon); } void ts::Box2DDebugDraw::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) { sf::ConvexShape polygon(vertexCount); for(int i = 0; i < vertexCount; i++) { sf::Vector2f transformedVec = B2VectorToSFVector(vertices[i]); polygon.setPoint(i, sf::Vector2f(std::floor(transformedVec.x), std::floor(transformedVec.y))); } polygon.setOutlineThickness(lineThickness); polygon.setFillColor(GLColorToSFML(color, 60)); polygon.setOutlineColor(GLColorToSFML(color)); window->draw(polygon); } void ts::Box2DDebugDraw::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color) { sf::CircleShape circle(radius * scale); circle.setOrigin(radius * scale, radius * scale); circle.setPosition(B2VectorToSFVector(center)); circle.setFillColor(sf::Color::Transparent); circle.setOutlineThickness(lineThickness); circle.setOutlineColor(GLColorToSFML(color)); window->draw(circle); } void ts::Box2DDebugDraw::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color) { sf::CircleShape circle(radius * scale); circle.setOrigin(radius * scale, radius * scale); circle.setPosition(B2VectorToSFVector(center)); circle.setFillColor(GLColorToSFML(color, 60)); circle.setOutlineThickness(lineThickness); circle.setOutlineColor(GLColorToSFML(color)); b2Vec2 endPoint = center + radius * axis; sf::Vertex line[2] = { sf::Vertex(B2VectorToSFVector(center), GLColorToSFML(color)), sf::Vertex(B2VectorToSFVector(endPoint), GLColorToSFML(color)) }; window->draw(circle); window->draw(line, 2, sf::Lines); } void ts::Box2DDebugDraw::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color) { sf::Vertex line[] = { sf::Vertex(B2VectorToSFVector(p1), GLColorToSFML(color)), sf::Vertex(B2VectorToSFVector(p2), GLColorToSFML(color)) }; window->draw(line, 2, sf::Lines); } void ts::Box2DDebugDraw::DrawTransform(const b2Transform& xf) { float lineLength = 0.4f; b2Vec2 xAxis = xf.p + lineLength * xf.q.GetXAxis(); sf::Vertex redLine[] = { sf::Vertex(B2VectorToSFVector(xf.p), sf::Color::Red), sf::Vertex(B2VectorToSFVector(xAxis), sf::Color::Red) }; b2Vec2 yAxis = xf.p + lineLength * xf.q.GetYAxis(); sf::Vertex greenLine[] = { sf::Vertex(B2VectorToSFVector(xf.p), sf::Color::Green), sf::Vertex(B2VectorToSFVector(yAxis), sf::Color::Green) }; window->draw(redLine, 2, sf::Lines); window->draw(greenLine, 2, sf::Lines); } void ts::Box2DDebugDraw::DrawPoint(const b2Vec2& p, float32 size, const b2Color& color) { this->DrawCircle(p, size, color); } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
#pragma once #include "IGameState.hpp" #include "EGameState.hpp" #include <Box2D/Box2D.h> #include "../misc/Box2DDebugDraw.hpp" #include "../input/Controller.hpp" #include <iostream> namespace ts { namespace gamestates { class Box2DTestGameState : public GameState { private: b2Vec2 gravity; b2World world; b2Body *ground; b2Body *player; Box2DDebugDraw debugDraw; Controller *controller; int remainingJumpSteps = 0; public: Box2DTestGameState(sf::RenderWindow &window, Controller &controller) : gravity(0.f, 200.f), world(gravity), debugDraw(&window, 8.f), controller(&controller) { world.SetDebugDraw(&debugDraw); debugDraw.SetFlags(b2Draw::e_shapeBit); { b2BodyDef groundBodyDef; groundBodyDef.type = b2_staticBody; groundBodyDef.position.Set(86., 94.); ground = world.CreateBody(&groundBodyDef); b2PolygonShape groundShape; groundShape.SetAsBox(87, 8); b2FixtureDef groundFixtureDef; groundFixtureDef.shape = &groundShape; groundFixtureDef.density = 1; ground->CreateFixture(&groundFixtureDef); } { b2BodyDef playerBodyDef; playerBodyDef.type = b2_dynamicBody; playerBodyDef.position.Set(50, 50); playerBodyDef.fixedRotation = true; player = world.CreateBody(&playerBodyDef); b2PolygonShape playerShape; playerShape.SetAsBox(5, 5); b2FixtureDef playerFixture; playerFixture.shape = &playerShape; playerFixture.density = 1; player->CreateFixture(&playerFixture); } } ~Box2DTestGameState() { world.DestroyBody(ground); world.DestroyBody(player); } EGameState update(sf::Time deltaTime) { b2Vec2 vel = player->GetLinearVelocity(); float desiredVel = 0; float acc = 1.f; float damping = 0.4f; float maxSpeed = 16.f; if(controller->pressedLeft()) { desiredVel = b2Max(vel.x - acc, -maxSpeed); } else if(controller->pressedRight()) { desiredVel = b2Min(vel.x + acc, maxSpeed); } else { desiredVel = vel.x * damping; } float velChange = desiredVel - vel.x; float impulse = player->GetMass() * velChange; player->ApplyLinearImpulse(b2Vec2(impulse, 0), player->GetWorldCenter(), true); if(controller->hitX()) { remainingJumpSteps = 6; } if(remainingJumpSteps > 0) { float force = player->GetMass() * 150 / (1.f/60.f); force /= 6.f; player->ApplyForce(b2Vec2(0, -force), player->GetWorldCenter(), true); remainingJumpSteps--; } world.Step(1.f/60.f, 16, 16); return EGameState::NOTHING; } void render() { world.DrawDebugData(); } }; } } |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von »BlueCobold« (24.03.2017, 16:50)
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von »BlueCobold« (24.03.2017, 17:11)
Werbeanzeige