aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine/physics.cpp
diff options
context:
space:
mode:
authorVaino Kauppila <vaino@vke.fi>2026-07-02 22:58:25 +0300
committerVaino Kauppila <vaino@vke.fi>2026-07-02 23:47:07 +0300
commitcd0c08dc7ce754473178ad3f32f7740a1dddc1eb (patch)
tree589c676b41d6f3d322ea5ce339ddb0811f29ec31 /src/engine/physics.cpp
downloadforts_clone-cd0c08dc7ce754473178ad3f32f7740a1dddc1eb.tar.gz
forts_clone-cd0c08dc7ce754473178ad3f32f7740a1dddc1eb.zip
Initial import: LibreForts (M0-M5 in progress)
Open-source Forts clone: custom C++20 engine + data-driven (Lua) 2D physics artillery RTS. - Building: node/strut graph on a stiff mass-spring solver (canon Forts model), triangulation rigidity, axial + 30-degree angle-stress breaking, cascading collapse, fire (DoT + spread), ground destroys debris. - Weapons (M4): cannon (ballistic) + laser (beam/ignite) with select-and-aim-in- arc UX; splash / beam damage; bg-brace passthrough. - Devices + economy (M5): reactors + win/loss + restart, mines/turbines/battery, metal deposits, storage caps, per-shot energy cost. - Data-driven via Lua/sol2 with a layered mod loader; enemy-fort scenario mod. - Renderer: SDL3 + textures + ImGui dev UI. stb_image + ImGui vendored. - Assets: CC0 placeholders only; real game art loaded at runtime from the user's own install via forts: paths (bring-your-own; nothing copyrighted committed). See README.md / CLAUDE.md and research/ + requirements/ for detail.
Diffstat (limited to 'src/engine/physics.cpp')
-rw-r--r--src/engine/physics.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/engine/physics.cpp b/src/engine/physics.cpp
new file mode 100644
index 0000000..af5d8ee
--- /dev/null
+++ b/src/engine/physics.cpp
@@ -0,0 +1,70 @@
+#include "engine/physics.hpp"
+#include <cstdio>
+
+PhysicsWorld::PhysicsWorld() {
+ b2WorldDef def = b2DefaultWorldDef();
+ def.gravity = {0.0f, -10.0f};
+ world_id_ = b2CreateWorld(&def);
+ printf("Physics: Box2D world created\n");
+}
+
+PhysicsWorld::~PhysicsWorld() {
+ b2DestroyWorld(world_id_);
+}
+
+void PhysicsWorld::step(float dt) {
+ // High sub-step count for stable distance joint networks
+ b2World_Step(world_id_, dt, 8);
+}
+
+PhysicsBody* PhysicsWorld::add_static_box(b2Vec2 center,
+ float half_w, float half_h,
+ float r, float g, float b) {
+ b2BodyDef body_def = b2DefaultBodyDef();
+ body_def.type = b2_staticBody;
+ body_def.position = center;
+
+ b2BodyId body_id = b2CreateBody(world_id_, &body_def);
+
+ b2ShapeDef shape_def = b2DefaultShapeDef();
+ b2Polygon box = b2MakeBox(half_w, half_h);
+ b2CreatePolygonShape(body_id, &shape_def, &box);
+
+ PhysicsBody pb;
+ pb.id = body_id;
+ pb.size = {half_w, half_h};
+ pb.red = r;
+ pb.green = g;
+ pb.blue = b;
+ pb.alpha = 1.0f;
+ pb.is_static = true;
+ bodies_.push_back(pb);
+ return &bodies_.back();
+}
+
+PhysicsBody* PhysicsWorld::add_dynamic_box(b2Vec2 center,
+ float half_w, float half_h,
+ float r, float g, float b) {
+ b2BodyDef body_def = b2DefaultBodyDef();
+ body_def.type = b2_dynamicBody;
+ body_def.position = center;
+
+ b2BodyId body_id = b2CreateBody(world_id_, &body_def);
+
+ b2ShapeDef shape_def = b2DefaultShapeDef();
+ shape_def.density = 1.0f;
+ shape_def.material.friction = 0.3f;
+ b2Polygon box = b2MakeBox(half_w, half_h);
+ b2CreatePolygonShape(body_id, &shape_def, &box);
+
+ PhysicsBody pb;
+ pb.id = body_id;
+ pb.size = {half_w, half_h};
+ pb.red = r;
+ pb.green = g;
+ pb.blue = b;
+ pb.alpha = 1.0f;
+ pb.is_static = false;
+ bodies_.push_back(pb);
+ return &bodies_.back();
+}