better box2d

This commit is contained in:
OusmBlueNinja 2025-04-18 20:29:00 -05:00
parent 93083afd97
commit 5d1672ed8f
6 changed files with 285 additions and 2 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "Remake"]
path = Remake
url = https://dock-it.dev/GigabiteStudios/Remake.git
[submodule "src/vendor/box2d"]
path = src/vendor/box2d
url = https://dock-it.dev/GigabiteStudios/box2d.git

View File

@ -5,6 +5,8 @@ src_dirs:
- src/src
- src/vendor
- src/include/lua
- src/vendor/box2d
# Include directories (-I)
@ -13,6 +15,8 @@ include_dirs:
- src/include/lua
- src/vendor
- src/vendor/imgui
- src/vendor/box2d/
- C:/msys64/mingw64/include
# Library search paths (-L)

View File

@ -1,2 +1,141 @@
[LINK] g++ src\build\Engine.o src\build\main.o src\build\Renderer.o src\build\Components\CameraComponent.o src\build\Components\LightComponent.o src\build\Components\ScriptComponent.o src\build\Components\SpriteComponent.o src\build\Components\TextComonent.o src\build\Components\TilemapComponent.o src\build\Entitys\Object.o src\build\utils\EngineConfig.o src\build\utils\ExceptionHandler.o src\build\utils\FileDialog.o src\build\utils\GameObjectsList.o src\build\utils\Logging.o src\build\utils\Profiler.o src\build\utils\Shader.o src\build\utils\UID.o src\build\utils\utils.o src\build\imgui\imgui.o src\build\imgui\imgui_demo.o src\build\imgui\imgui_draw.o src\build\imgui\imgui_impl_glfw.o src\build\imgui\imgui_impl_opengl3.o src\build\imgui\imgui_tables.o src\build\imgui\imgui_widgets.o src\build\lapi.o src\build\lauxlib.o src\build\lbaselib.o src\build\lcode.o src\build\lcorolib.o src\build\lctype.o src\build\ldblib.o src\build\ldebug.o src\build\ldo.o src\build\ldump.o src\build\lfunc.o src\build\lgc.o src\build\linit.o src\build\liolib.o src\build\llex.o src\build\lmathlib.o src\build\lmem.o src\build\loadlib.o src\build\lobject.o src\build\lopcodes.o src\build\loslib.o src\build\lparser.o src\build\lstate.o src\build\lstring.o src\build\lstrlib.o src\build\ltable.o src\build\ltablib.o src\build\ltm.o src\build\lua.o src\build\luac.o src\build\lundump.o src\build\lutf8lib.o src\build\lvm.o src\build\lzio.o -o src\build\app.exe -LC:\msys64\mingw64\lib -lglfw3 -lglew32 -lopengl32 -lgdi32 -lyaml-cpp -lcomdlg32 -lssl -lcrypto
[RUN] Executed app.exe successfully.
[ERROR] g++ -std=c++20 -Wall -g -Isrc/include -Isrc/include/lua -Isrc/vendor -Isrc/vendor/imgui -Isrc/vendor/box2d/ -IC:/msys64/mingw64/include -IC:\msys64\mingw64\lib\libyaml-cpp.a -Isrc\vendor\imgui -MMD -MP -c src\src\Components\PhysicsComponent.cpp -o src\build\Components\PhysicsComponent.o
cc1plus.exe: warning: C:\msys64\mingw64\lib\libyaml-cpp.a: not a directory
In file included from C:/msys64/mingw64/include/yaml-cpp/parser.h:13,
from C:/msys64/mingw64/include/yaml-cpp/yaml.h:10,
from src\src\Components\Component.h:5,
from src\src\Components\PhysicsComponent.h:2,
from src\src\Components\PhysicsComponent.cpp:1:
C:/msys64/mingw64/include/yaml-cpp/dll.h:22:65: note: '#pragma message: Defining YAML_CPP_API for DLL import'
22 | # pragma message( "Defining YAML_CPP_API for DLL import" )
| ^
src\src\Components\PhysicsComponent.h:13:21: error: 'b2World' has not been declared
13 | void CreateBody(b2World* world, const glm::vec2& position);
| ^~~~~~~
src\src\Components\PhysicsComponent.h:13:43: error: 'glm' does not name a type
13 | void CreateBody(b2World* world, const glm::vec2& position);
| ^~~
src\src\Components\PhysicsComponent.h:13:52: error: expected unqualified-id before '&' token
13 | void CreateBody(b2World* world, const glm::vec2& position);
| ^
src\src\Components\PhysicsComponent.h:13:52: error: expected ')' before '&' token
13 | void CreateBody(b2World* world, const glm::vec2& position);
| ~ ^
| )
src\src\Components\PhysicsComponent.h:13:52: error: expected ';' at end of member declaration
13 | void CreateBody(b2World* world, const glm::vec2& position);
| ^
| ;
src\src\Components\PhysicsComponent.h:13:54: error: 'position' does not name a type
13 | void CreateBody(b2World* world, const glm::vec2& position);
| ^~~~~~~~
src\src\Components\PhysicsComponent.h:16:28: error: 'glm' does not name a type
16 | void SetVelocity(const glm::vec2& vel);
| ^~~
src\src\Components\PhysicsComponent.h:16:37: error: expected unqualified-id before '&' token
16 | void SetVelocity(const glm::vec2& vel);
| ^
src\src\Components\PhysicsComponent.h:16:37: error: expected ')' before '&' token
16 | void SetVelocity(const glm::vec2& vel);
| ~ ^
| )
src\src\Components\PhysicsComponent.h:16:37: error: expected ';' at end of member declaration
16 | void SetVelocity(const glm::vec2& vel);
| ^
| ;
src\src\Components\PhysicsComponent.h:16:39: error: 'vel' does not name a type
16 | void SetVelocity(const glm::vec2& vel);
| ^~~
src\src\Components\PhysicsComponent.h:17:5: error: 'glm' does not name a type
17 | glm::vec2 GetVelocity() const;
| ^~~
src\src\Components\PhysicsComponent.h:29:5: error: 'b2Body' does not name a type; did you mean 'b2BodyId'?
29 | b2Body* body = nullptr;
| ^~~~~~
| b2BodyId
src\src\Components\PhysicsComponent.h:30:5: error: 'b2World' does not name a type; did you mean 'b2WorldId'?
30 | b2World* world = nullptr;
| ^~~~~~~
| b2WorldId
src\src\Components\PhysicsComponent.h:26:10: error: 'void PhysicsComponent::OnImGuiInspector()' marked 'override', but does not override
26 | void OnImGuiInspector() override;
| ^~~~~~~~~~~~~~~~
src\src\Components\PhysicsComponent.cpp: In constructor 'PhysicsComponent::PhysicsComponent(BodyType, float, float)':
src\src\Components\PhysicsComponent.cpp:6:46: error: no matching function for call to 'Component::Component()'
6 | : type(type), width(width), height(height) {}
| ^
src\src\Components\Component.h:13:5: note: candidate: 'Component::Component(Object*)'
13 | Component(Object* owner) : owner(owner) {}
| ^~~~~~~~~
src\src\Components\Component.h:13:5: note: candidate expects 1 argument, 0 provided
src\src\Components\Component.h:11:7: note: candidate: 'constexpr Component::Component(const Component&)'
11 | class Component {
| ^~~~~~~~~
src\src\Components\Component.h:11:7: note: candidate expects 1 argument, 0 provided
src\src\Components\PhysicsComponent.cpp: At global scope:
src\src\Components\PhysicsComponent.cpp:12:6: error: variable or field 'CreateBody' declared void
12 | void PhysicsComponent::CreateBody(b2World* world, const glm::vec2& position) {
| ^~~~~~~~~~~~~~~~
src\src\Components\PhysicsComponent.cpp:12:35: error: 'b2World' was not declared in this scope; did you mean 'b2WorldId'?
12 | void PhysicsComponent::CreateBody(b2World* world, const glm::vec2& position) {
| ^~~~~~~
| b2WorldId
src\src\Components\PhysicsComponent.cpp:12:44: error: 'world' was not declared in this scope
12 | void PhysicsComponent::CreateBody(b2World* world, const glm::vec2& position) {
| ^~~~~
src\src\Components\PhysicsComponent.cpp:12:51: error: expected primary-expression before 'const'
12 | void PhysicsComponent::CreateBody(b2World* world, const glm::vec2& position) {
| ^~~~~
src\src\Components\PhysicsComponent.cpp: In member function 'void PhysicsComponent::DestroyBody()':
src\src\Components\PhysicsComponent.cpp:39:9: error: 'body' was not declared in this scope
39 | if (body && world) {
| ^~~~
src\src\Components\PhysicsComponent.cpp:39:17: error: 'world' was not declared in this scope
39 | if (body && world) {
| ^~~~~
src\src\Components\PhysicsComponent.cpp: At global scope:
src\src\Components\PhysicsComponent.cpp:45:6: error: no declaration matches 'void PhysicsComponent::SetVelocity(const glm::vec2&)'
45 | void PhysicsComponent::SetVelocity(const glm::vec2& vel) {
| ^~~~~~~~~~~~~~~~
src\src\Components\PhysicsComponent.h:16:10: note: candidate is: 'void PhysicsComponent::SetVelocity(...) &'
16 | void SetVelocity(const glm::vec2& vel);
| ^~~~~~~~~~~
src\src\Components\PhysicsComponent.h:6:7: note: 'class PhysicsComponent' defined here
6 | class PhysicsComponent : public Component {
| ^~~~~~~~~~~~~~~~
src\src\Components\PhysicsComponent.cpp:49:11: error: no declaration matches 'glm::vec2 PhysicsComponent::GetVelocity() const'
49 | glm::vec2 PhysicsComponent::GetVelocity() const {
| ^~~~~~~~~~~~~~~~
src\src\Components\PhysicsComponent.cpp:49:11: note: no functions named 'glm::vec2 PhysicsComponent::GetVelocity() const'
src\src\Components\PhysicsComponent.h:6:7: note: 'class PhysicsComponent' defined here
6 | class PhysicsComponent : public Component {
| ^~~~~~~~~~~~~~~~
src\src\Components\PhysicsComponent.cpp: In member function 'void PhysicsComponent::SetFriction(float)':
src\src\Components\PhysicsComponent.cpp:59:9: error: 'body' was not declared in this scope
59 | if (body && body->GetFixtureList())
| ^~~~
src\src\Components\PhysicsComponent.cpp: In member function 'void PhysicsComponent::SetFixedRotation(bool)':
src\src\Components\PhysicsComponent.cpp:70:9: error: 'body' was not declared in this scope
70 | if (body)
| ^~~~
src\src\Components\PhysicsComponent.cpp: In member function 'void PhysicsComponent::SyncFromPhysics()':
src\src\Components\PhysicsComponent.cpp:75:9: error: 'body' was not declared in this scope
75 | if (body) {
| ^~~~
src\src\Components\PhysicsComponent.cpp:77:9: error: 'object' was not declared in this scope; did you mean 'Object'?
77 | object->SetLocalPosition({ pos.x, pos.y });
| ^~~~~~
| Object
src\src\Components\PhysicsComponent.cpp: In member function 'void PhysicsComponent::SyncToPhysics()':
src\src\Components\PhysicsComponent.cpp:82:9: error: 'body' was not declared in this scope
82 | if (body) {
| ^~~~
src\src\Components\PhysicsComponent.cpp:83:25: error: 'object' was not declared in this scope; did you mean 'Object'?
83 | glm::vec2 pos = object->GetLocalPosition();
| ^~~~~~
| Object
src\src\Components\PhysicsComponent.cpp: In member function 'void PhysicsComponent::OnImGuiInspector()':
src\src\Components\PhysicsComponent.cpp:90:21: error: 'GetVelocity' was not declared in this scope; did you mean 'SetVelocity'?
90 | glm::vec2 vel = GetVelocity();
| ^~~~~~~~~~~
| SetVelocity

View File

@ -0,0 +1,99 @@
#include "PhysicsComponent.h"
#include "../Entitys/Object.h"
#include "imgui.h"
PhysicsComponent::PhysicsComponent(BodyType type, float width, float height)
: type(type), width(width), height(height) {}
PhysicsComponent::~PhysicsComponent() {
DestroyBody();
}
void PhysicsComponent::CreateBody(b2World* world, const glm::vec2& position) {
this->world = world;
b2BodyDef def;
switch (type) {
case BodyType::Static: def.type = b2_staticBody; break;
case BodyType::Dynamic: def.type = b2_dynamicBody; break;
case BodyType::Kinematic: def.type = b2_kinematicBody; break;
}
def.position.Set(position.x, position.y);
def.fixedRotation = fixedRotation;
body = world->CreateBody(&def);
b2PolygonShape box;
box.SetAsBox(width * 0.5f, height * 0.5f);
b2FixtureDef fixtureDef;
fixtureDef.shape = &box;
fixtureDef.density = density;
fixtureDef.friction = friction;
body->CreateFixture(&fixtureDef);
}
void PhysicsComponent::DestroyBody() {
if (body && world) {
world->DestroyBody(body);
body = nullptr;
}
}
void PhysicsComponent::SetVelocity(const glm::vec2& vel) {
if (body) body->SetLinearVelocity(b2Vec2(vel.x, vel.y));
}
glm::vec2 PhysicsComponent::GetVelocity() const {
if (body) {
b2Vec2 v = body->GetLinearVelocity();
return { v.x, v.y };
}
return {};
}
void PhysicsComponent::SetFriction(float f) {
friction = f;
if (body && body->GetFixtureList())
body->GetFixtureList()->SetFriction(f);
}
void PhysicsComponent::SetDensity(float d) {
density = d;
// requires refixture for update
}
void PhysicsComponent::SetFixedRotation(bool fixed) {
fixedRotation = fixed;
if (body)
body->SetFixedRotation(fixed);
}
void PhysicsComponent::SyncFromPhysics() {
if (body) {
b2Vec2 pos = body->GetPosition();
object->SetLocalPosition({ pos.x, pos.y });
}
}
void PhysicsComponent::SyncToPhysics() {
if (body) {
glm::vec2 pos = object->GetLocalPosition();
body->SetTransform({ pos.x, pos.y }, body->GetAngle());
}
}
void PhysicsComponent::OnImGuiInspector() {
ImGui::Text("Physics Body");
glm::vec2 vel = GetVelocity();
ImGui::DragFloat2("Velocity", &vel.x);
SetVelocity(vel);
if (ImGui::DragFloat("Friction", &friction, 0.01f, 0.0f, 1.0f))
SetFriction(friction);
if (ImGui::Checkbox("Fixed Rotation", &fixedRotation))
SetFixedRotation(fixedRotation);
}

View File

@ -0,0 +1,37 @@
#pragma once
#include "Component.h"
#include "box2d/box2d.h"
#include <memory>
class PhysicsComponent : public Component {
public:
enum class BodyType { Static, Dynamic, Kinematic };
PhysicsComponent(Object* owner, BodyType type = BodyType::Dynamic, float width = 1.0f, float height = 1.0f);
~PhysicsComponent();
void CreateBody(b2World* world, const glm::vec2& position);
void DestroyBody();
void SetVelocity(const glm::vec2& vel);
glm::vec2 GetVelocity() const;
void SetFriction(float friction);
void SetDensity(float density);
void SetFixedRotation(bool fixed);
void SyncFromPhysics();
void SyncToPhysics();
void OnImGuiInspector() override;
private:
b2Body* body = nullptr;
b2World* world = nullptr;
BodyType type;
float width, height;
float friction = 0.3f;
float density = 1.0f;
bool fixedRotation = false;
};

1
src/vendor/box2d vendored Submodule

@ -0,0 +1 @@
Subproject commit 3e968638a5d0b0ff7ff0dd5a1e8e88844927b2d2