Nimbin[12]?SDL & Graphics / sdl3_3d_oop_basic_game / src/systems/physics_system.cpp

sdl3_3d_oop_basic_game git · main

SDL3 3D OOP basic game (entities, systems, CMake)

sdl3 3d c++ game cmake · first commit 2026-04-19 · last commit 2026-04-19 (7 months ago) · synced 1 hour ago · upstream: git.ide3.de/chfis005/sdl3_3d_oop_basic_game

C++ 99%
git clone https://git.christianimmanuel.de/sdl-graphics/sdl3_3d_oop_basic_game.gitwget https://git.christianimmanuel.de/sdl-graphics/sdl3_3d_oop_basic_game/archive/sdl3_3d_oop_basic_game.tar.gz
src/systems/physics_system.cpp 704 B · 26 lines raw
#include "systems/physics_system.hpp"
#include <SDL3/SDL.h>

void PhysicsSystem::update(Player& p, double acc_x, double acc_z, double dt) {
    dt = SDL_min(dt, 1.0 / 30.0);

    double rate = 6.0;
    double drag = SDL_exp(-dt * rate);
    double diff = 1.0 - drag;
    double grav = 25.0;

    double vx = p.vel.x;
    double vy = p.vel.y;
    double vz = p.vel.z;

    p.vel.x -= vx * diff;
    p.vel.y -= grav * dt;
    p.vel.z -= vz * diff;

    p.vel.x += diff * acc_x / rate;
    p.vel.z += diff * acc_z / rate;

    p.pos.x += (dt - diff/rate) * acc_x / rate + diff * vx / rate;
    p.pos.y += -0.5 * grav * dt * dt + vy * dt;
    p.pos.z += (dt - diff/rate) * acc_z / rate + diff * vz / rate;
}