Now box 2d lets you move the box around with right and left mouse buttons.
This commit is contained in:
parent
285cd4971f
commit
38c0fee65c
6 changed files with 80 additions and 9 deletions
|
@ -1,7 +1,7 @@
|
||||||
#include "imgui.h"
|
#include "imgui.h"
|
||||||
#include "imgui-SFML.h"
|
#include "imgui-SFML.h"
|
||||||
#include <fmt/core.h>
|
#include <fmt/core.h>
|
||||||
|
#include <box2d/box2d.h>
|
||||||
#include <SFML/Graphics/CircleShape.hpp>
|
#include <SFML/Graphics/CircleShape.hpp>
|
||||||
#include <SFML/Graphics/RenderWindow.hpp>
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
#include <SFML/System.hpp>
|
#include <SFML/System.hpp>
|
||||||
|
@ -35,22 +35,55 @@ void Window_update(sf::RenderWindow &window, sf::Shape &shape) {
|
||||||
window.display();
|
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() {
|
int main() {
|
||||||
fmt::print("Setting up a window for you...\n");
|
fmt::print("Setting up a window for you...\n");
|
||||||
sf::ContextSettings settings;
|
sf::ContextSettings settings;
|
||||||
settings.antialiasingLevel = 8;
|
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.setFramerateLimit(60);
|
||||||
window.setVerticalSyncEnabled(true);
|
window.setVerticalSyncEnabled(true);
|
||||||
|
|
||||||
ImGui_setup(window);
|
ImGui_setup(window);
|
||||||
|
|
||||||
sf::CircleShape shape(100.f, 4);
|
sf::CircleShape shape(50.f, 4);
|
||||||
sf::Vector2u size = window.getSize();
|
sf::Vector2u winSize = window.getSize();
|
||||||
shape.setPosition(size.x / 2, size.y / 2);
|
shape.setPosition(winSize.x / 2, winSize.y / 2);
|
||||||
shape.setOrigin(100.f, 100.f);
|
shape.setOrigin(50.f, 50.f);
|
||||||
shape.setFillColor(sf::Color(150, 50, 250));
|
shape.setFillColor(sf::Color(150, 50, 250));
|
||||||
shape.setOutlineThickness(10.f);
|
shape.setOutlineThickness(10.f);
|
||||||
shape.setOutlineColor(sf::Color(250, 150, 100));
|
shape.setOutlineColor(sf::Color(250, 150, 100));
|
||||||
|
@ -69,6 +102,14 @@ int main() {
|
||||||
|
|
||||||
thread.launch();
|
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()) {
|
while (window.isOpen()) {
|
||||||
sf::Event event;
|
sf::Event event;
|
||||||
|
|
||||||
|
@ -96,8 +137,11 @@ int main() {
|
||||||
break;
|
break;
|
||||||
case sf::Event::MouseButtonPressed:
|
case sf::Event::MouseButtonPressed:
|
||||||
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
|
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
|
||||||
sf::Vector2i clickAt = sf::Mouse::getPosition(window);
|
b2Vec2 force(-200, 1000);
|
||||||
shape.setPosition(clickAt.x, clickAt.y);
|
box.body->ApplyForceToCenter(force, true);
|
||||||
|
} else if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
|
||||||
|
b2Vec2 force(200, 1000);
|
||||||
|
box.body->ApplyForceToCenter(force, true);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -110,8 +154,13 @@ int main() {
|
||||||
tick = since;
|
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);
|
ImGui_update(window, deltaClock, tick);
|
||||||
Window_update(window, shape);
|
Window_update(window, shape);
|
||||||
|
|
|
@ -5,6 +5,7 @@ dependencies = [
|
||||||
dependency('sfml'),
|
dependency('sfml'),
|
||||||
dependency('imgui-sfml'),
|
dependency('imgui-sfml'),
|
||||||
dependency('fmt'),
|
dependency('fmt'),
|
||||||
|
dependency('box2d'),
|
||||||
]
|
]
|
||||||
|
|
||||||
executable('sfmldemo', 'main.cpp',
|
executable('sfmldemo', 'main.cpp',
|
||||||
|
|
|
@ -14,4 +14,5 @@ meson wrap install sfml
|
||||||
meson wrap install vorbis
|
meson wrap install vorbis
|
||||||
meson wrap install zlib
|
meson wrap install zlib
|
||||||
meson wrap install fmt
|
meson wrap install fmt
|
||||||
|
meson wrap install box2d
|
||||||
meson setup -Ddefault_library=static builddir
|
meson setup -Ddefault_library=static builddir
|
||||||
|
|
|
@ -17,4 +17,5 @@ meson wrap install sfml
|
||||||
meson wrap install vorbis
|
meson wrap install vorbis
|
||||||
meson wrap install zlib
|
meson wrap install zlib
|
||||||
meson wrap install fmt
|
meson wrap install fmt
|
||||||
|
meson wrap install box2d
|
||||||
meson setup builddir
|
meson setup builddir
|
||||||
|
|
|
@ -10,4 +10,5 @@ meson wrap install openal-soft
|
||||||
meson wrap install sfml
|
meson wrap install sfml
|
||||||
meson wrap install vorbis
|
meson wrap install vorbis
|
||||||
meson wrap install zlib
|
meson wrap install zlib
|
||||||
|
meson wrap install box2d
|
||||||
meson setup -Ddefault_library=static builddir
|
meson setup -Ddefault_library=static builddir
|
||||||
|
|
18
sfmldemo/setup.sh
Executable file
18
sfmldemo/setup.sh
Executable file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue