summaryrefslogtreecommitdiff
path: root/src/physics.c
blob: 51e80f587874079feee4edf3b5a27e0dec547034 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "physics.h"
#include "game.h"

extern Game game;

static inline bool PhysicsEnabled(const Entity* e) {
    return EntityAllocated(e) && (e->flags & ENTITY_PHYSICS_ACTIVE);
}

static inline Rectangle GetPhysicsColliderGlobal(const Entity* e) {
    Rectangle physicsColliderGlobal = e->physicsCollider;
    physicsColliderGlobal.x += e->position.x;
    physicsColliderGlobal.y += e->position.y;

    return physicsColliderGlobal;
}

void MoveAndSlide(Entity* e, float deltaTime) {
    Vector2 velocity = e->velocity;
    Rectangle physicsCollider = GetPhysicsColliderGlobal(e);

    physicsCollider.x += e->velocity.x * deltaTime;
    for (int i = 0; i < MAX_ENTITY_COUNT; i++) {
        Entity* other = &game.entities[i];

        if(!PhysicsEnabled(other) || SameEntity(other, e)) continue;
        Rectangle otherCollider = GetPhysicsColliderGlobal(other);

        if(CheckCollisionRecs(physicsCollider, otherCollider)) {
            if (velocity.x > 0) {
                physicsCollider.x = otherCollider.x - physicsCollider.width;
            } else {
                physicsCollider.x = otherCollider.x + otherCollider.width;
            }
        }
    }

    physicsCollider.y += e->velocity.y * deltaTime;
    for (int i = 0; i < MAX_ENTITY_COUNT; i++) {
        Entity* other = &game.entities[i];

        if(!PhysicsEnabled(other) || SameEntity(other, e)) continue;

        Rectangle otherCollider = GetPhysicsColliderGlobal(other);

        if(CheckCollisionRecs(physicsCollider, otherCollider)) {
            if (velocity.y > 0) {
                physicsCollider.y = otherCollider.y - physicsCollider.height;
            } else {
                physicsCollider.y = otherCollider.y + otherCollider.height;
            }
        }
    }

    e->position.x = physicsCollider.x;
    e->position.y = physicsCollider.y;
}

#ifdef BEATEMUP_DEBUG
void DebugHighlights(const Entity* e) {
    if (!PhysicsEnabled(e))
        return;

    Rectangle dstRect = GetPhysicsColliderGlobal(e);

    Color colliderDrawColor = e->physicsColliderColor;
    colliderDrawColor.a = 170;

    DrawRectangle(dstRect.x, dstRect.y, dstRect.width, dstRect.height, colliderDrawColor);
}
#endif