From 38c0fee65cacfe27ef2e5f5c9b94cd2baeb9f33d Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 27 Apr 2024 00:03:48 -0400 Subject: [PATCH] Now box 2d lets you move the box around with right and left mouse buttons. --- sfmldemo/main.cpp | 67 ++++++++++++++++++++++++++++++++++------ sfmldemo/meson.build | 1 + sfmldemo/reset_build.ps1 | 1 + sfmldemo/reset_build.sh | 1 + sfmldemo/setup.ps1 | 1 + sfmldemo/setup.sh | 18 +++++++++++ 6 files changed, 80 insertions(+), 9 deletions(-) create mode 100755 sfmldemo/setup.sh diff --git a/sfmldemo/main.cpp b/sfmldemo/main.cpp index 50ef581..c898027 100644 --- a/sfmldemo/main.cpp +++ b/sfmldemo/main.cpp @@ -1,7 +1,7 @@ #include "imgui.h" #include "imgui-SFML.h" #include - +#include #include #include #include @@ -35,22 +35,55 @@ void Window_update(sf::RenderWindow &window, sf::Shape &shape) { window.display(); } +struct BoxTest { + b2Body *groundBody; + b2Body *body; +}; + +struct BoxTest Box2d_setup(b2World &world) { + b2BodyDef groundBodyDef; + groundBodyDef.position.Set(0.0f, -10.0f); + b2Body *groundBody = world.CreateBody(&groundBodyDef); + + b2PolygonShape groundBox; + groundBox.SetAsBox(50.0f, 10.0f); + groundBody->CreateFixture(&groundBox, 0.0f); + + b2BodyDef bodyDef; + bodyDef.type = b2_dynamicBody; + bodyDef.position.Set(3.0f, 4.0f); + b2Body *body = world.CreateBody(&bodyDef); + + b2PolygonShape dynamicBox; + dynamicBox.SetAsBox(1.0f, 1.0f); + b2FixtureDef fixtureDef; + fixtureDef.shape = &dynamicBox; + + fixtureDef.density = 1.0f; + fixtureDef.friction = 0.3f; + + body->CreateFixture(&fixtureDef); + + BoxTest box {groundBody, body}; + return box; +} + int main() { fmt::print("Setting up a window for you...\n"); sf::ContextSettings settings; settings.antialiasingLevel = 8; - sf::RenderWindow window(sf::VideoMode(1920, 1080), "Simple Game Demo", sf::Style::Default, settings); + sf::RenderWindow window(sf::VideoMode(720, 480), "Simple Game Demo", sf::Style::Default, settings); // window.setFramerateLimit(60); window.setVerticalSyncEnabled(true); ImGui_setup(window); - sf::CircleShape shape(100.f, 4); - sf::Vector2u size = window.getSize(); - shape.setPosition(size.x / 2, size.y / 2); - shape.setOrigin(100.f, 100.f); + sf::CircleShape shape(50.f, 4); + sf::Vector2u winSize = window.getSize(); + shape.setPosition(winSize.x / 2, winSize.y / 2); + shape.setOrigin(50.f, 50.f); shape.setFillColor(sf::Color(150, 50, 250)); shape.setOutlineThickness(10.f); shape.setOutlineColor(sf::Color(250, 150, 100)); @@ -69,6 +102,14 @@ int main() { thread.launch(); + b2Vec2 gravity(0.0f, -10.0f); + b2World world(gravity); + BoxTest box = Box2d_setup(world); + + float timeStep = 1.0f / 60.0f; + int velocityIterations = 6; + int positionIterations = 2; + while (window.isOpen()) { sf::Event event; @@ -96,8 +137,11 @@ int main() { break; case sf::Event::MouseButtonPressed: if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) { - sf::Vector2i clickAt = sf::Mouse::getPosition(window); - shape.setPosition(clickAt.x, clickAt.y); + b2Vec2 force(-200, 1000); + box.body->ApplyForceToCenter(force, true); + } else if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) { + b2Vec2 force(200, 1000); + box.body->ApplyForceToCenter(force, true); } break; } @@ -110,8 +154,13 @@ int main() { tick = since; } + world.Step(timeStep, velocityIterations, positionIterations); + b2Vec2 position = box.body->GetPosition(); + float angle = box.body->GetAngle(); + + shape.setPosition(position.x * 100.0f, winSize.y - position.y * 100.0f); + shape.setRotation(angle * 180.0f / M_PI); - shape.rotate(1); ImGui_update(window, deltaClock, tick); Window_update(window, shape); diff --git a/sfmldemo/meson.build b/sfmldemo/meson.build index aa52b9b..0d61255 100644 --- a/sfmldemo/meson.build +++ b/sfmldemo/meson.build @@ -5,6 +5,7 @@ dependencies = [ dependency('sfml'), dependency('imgui-sfml'), dependency('fmt'), + dependency('box2d'), ] executable('sfmldemo', 'main.cpp', diff --git a/sfmldemo/reset_build.ps1 b/sfmldemo/reset_build.ps1 index 9ee73a0..d3a0ea3 100644 --- a/sfmldemo/reset_build.ps1 +++ b/sfmldemo/reset_build.ps1 @@ -14,4 +14,5 @@ meson wrap install sfml meson wrap install vorbis meson wrap install zlib meson wrap install fmt +meson wrap install box2d meson setup -Ddefault_library=static builddir diff --git a/sfmldemo/reset_build.sh b/sfmldemo/reset_build.sh index caf96c3..5f20ffc 100755 --- a/sfmldemo/reset_build.sh +++ b/sfmldemo/reset_build.sh @@ -17,4 +17,5 @@ meson wrap install sfml meson wrap install vorbis meson wrap install zlib meson wrap install fmt +meson wrap install box2d meson setup builddir diff --git a/sfmldemo/setup.ps1 b/sfmldemo/setup.ps1 index f0a1f18..d777501 100644 --- a/sfmldemo/setup.ps1 +++ b/sfmldemo/setup.ps1 @@ -10,4 +10,5 @@ meson wrap install openal-soft meson wrap install sfml meson wrap install vorbis meson wrap install zlib +meson wrap install box2d meson setup -Ddefault_library=static builddir diff --git a/sfmldemo/setup.sh b/sfmldemo/setup.sh new file mode 100755 index 0000000..36b1ea1 --- /dev/null +++ b/sfmldemo/setup.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -ex + +mkdir subprojects +mkdir builddir +meson wrap install flac +meson wrap install freetype2 +meson wrap install imgui-sfml +meson wrap install imgui +meson wrap install libpng +meson wrap install ogg +meson wrap install openal-soft +meson wrap install sfml +meson wrap install vorbis +meson wrap install zlib +meson wrap install fmt +meson wrap install box2d +meson setup builddir