aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine/physics.hpp
blob: 6a8abda2f1541c8972a078e751d939fa56da8f6c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#pragma once

#include <box2d/box2d.h>
#include <vector>

// Box2D rigid-body world. The building structure runs on the custom Verlet
// solver (see build_graph / verlet); this exists as the foundation for M4
// projectiles and any terrain collision they need.
struct PhysicsBody {
    b2BodyId  id;
    b2Vec2    size;      // half-width, half-height for box shapes
    float     red, green, blue, alpha;
    bool      is_static;
};

class PhysicsWorld {
public:
    PhysicsWorld();
    ~PhysicsWorld();

    PhysicsWorld(const PhysicsWorld&) = delete;
    PhysicsWorld& operator=(const PhysicsWorld&) = delete;

    void step(float dt);

    // Create a static box (terrain, walls).
    PhysicsBody* add_static_box(b2Vec2 center, float half_w, float half_h,
                                float r = 0.3f, float g = 0.3f, float b = 0.3f);

    // Create a dynamic box (projectiles, debris).
    PhysicsBody* add_dynamic_box(b2Vec2 center, float half_w, float half_h,
                                 float r = 0.8f, float g = 0.6f, float b = 0.2f);

    const std::vector<PhysicsBody>& bodies() const { return bodies_; }

    b2WorldId world_id() const { return world_id_; }

private:
    b2WorldId               world_id_;
    std::vector<PhysicsBody> bodies_;
};