Nimbin[12]?SDL & Graphics / sdl_runtime_compiler / src/systems/physics_system.hpp

sdl_runtime_compiler git · main

SDL3 game for running and compiling code at runtime

sdl3 c++ compiler dlopen cmake · first commit 2026-04-19 · last commit 2026-07-03 (3 months ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/sdl-runtime-compiler

C++ 72.3% C 26.2%
git clone https://git.christianimmanuel.de/sdl-graphics/sdl_runtime_compiler.gitwget https://git.christianimmanuel.de/sdl-graphics/sdl_runtime_compiler/archive/sdl_runtime_compiler.tar.gz
src/systems/physics_system.hpp 2.7 KB · 75 lines raw
#pragma once

#include "entities/game_object.hpp"
#include "entities/player.hpp"
#include "systems/carry_system.hpp"
#include "systems/collision.hpp"
#include "text/handler.hpp"
#include "utils/globals.hpp"

namespace
Nimbin
{

struct Projectile;


struct ProjectileContactEvent
{
    Vec3        point{};
    Vec3        normal{};
    GameObject* other      = nullptr;
    Projectile* projectile = nullptr;
};


class PhysicsSystem
{
    struct SpatialGrid {
        DynArray<DynArray<GameObject*>> cells;
        double cell  = 2.0;
        double inv   = 0.5;
        double min_x = 0, min_z = 0;
        int    nx    = 0, nz = 0;

        void build(const DynArray<GameObject*>& objs);
        int  index(int cx, int cz) const { return cz * nx + cx; }
    };

    SpatialGrid _grid;

    static void
         resolve_WorldBounds(GameObject& obj);
    bool resolve_SingleBoxCollision(Projectile& proj,
                                    const Vec3& box_min, const Vec3& box_max,
                                    const Vec3& word_he, double restitution,
                                    Vec3& cur, Vec3& prev, Vec3& dir,
                                    Collision::SweptResult& out_sw);
    void resolve_ProjectileStatics (Projectile& proj,
                                    const DynArray<GameObject*>& statics,
                                    Vec3& cur, Vec3& prev, Vec3& dir,
                                    const Vec3& word_he,
                                    DynArray<ProjectileContactEvent>* contacts);
    void resolve_ProjectileLabels  (Projectile& proj,
                                    const DynArray<Text::Handler*>& labels,
                                    Vec3& cur, Vec3& prev, Vec3& dir,
                                    const Vec3& word_he);
    void try_Pair(GameObject* a, GameObject* b);
    void collide_Grid(const DynArray<GameObject*>& active);

public:
    void update_Player (Player& player, double acc_x, double acc_z, double dt);
    void update_Objects(const DynArray<GameObject*>& objects,
                        double dt, double gravity);
    void update_Projectiles (DynArray<Projectile>& projectiles, double dt,
                             const DynArray<Text::TokenPtr>& words);
    void resolve_Projectiles(DynArray<Projectile>& projectiles,
                             const DynArray<GameObject*>* statics,
                             const DynArray<Text::Handler*>* labels,
                             DynArray<ProjectileContactEvent>* contacts);
    void resolve_PlayerVsObjects(Player& player,
                                 CarriedLine& carried,
                                 const DynArray<GameObject*>& colliders);
};

} // namespace Nimbin