summaryrefslogtreecommitdiff
path: root/phys.h
blob: cbdd525255471ed8fda2feac64368b9363d171d0 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
i32 add_entity(entity_t entity, elist_t *list)
{
    assert(list);
    i32 i;
    for (i = 0; i < MAX_ENTITIES; ++i)
        if (!list->nodes[i].id)
            break;
    if (i == MAX_ENTITIES) {
        printf("error: failed to add entity\n");
        return 0;
    }
    ++list->count;
    enode_t *node = list->nodes+i;
    node->id = list->count;
    node->entity = entity;
    dllpushback(list->first, list->last, node);
    return node->id;
}

void delete_entity(i32 id, elist_t *list)
{
    assert(list);
    for (enode_t *node = list->first; node; node = node->next) {
        if (node->id == id) {
            dllremove(list->first, list->last, node);
            memzero_struct(node);
            list->count--;
            printf("info: entity %d deleted\n", id);
            return;
        }
    }
    printf("error: entity %d failed to delete\n", id);
}

entity_t *get_entity(elist_t list, i32 id)
{
    for (enode_t *node = list.first; node; node = node->next)
        if (node->id == id)
            return &node->entity;
    return 0;
}

i32 aabb3d(bbox_t a, bbox_t b)
{
    i32 result = ((b.start.x <= a.end.x) && (b.end.x >= a.start.x)) &&
        ((b.start.y <= a.end.y) && (b.end.y >= a.start.y)) &&
        ((b.start.z <= a.end.z) && (b.end.z >= a.start.z));
    return result;
}

i32 check_collision(entity_t *a, entity_t *b)
{
    if (!(a->flags & ENTITY_COLLIDE) || !(b->flags & ENTITY_COLLIDE))
        return 0;

    bbox_t bboxa = {
        v3_add(a->bbox.start, a->position),
        v3_add(a->bbox.end, a->position)
    };

    bbox_t bboxb = {
        v3_add(b->bbox.start, b->position),
        v3_add(b->bbox.end, b->position)
    };

    i32 result = aabb3d(bboxa, bboxb);

    return result;
}

entity_t move_entity(entity_t old, v3 acceleration, float dt)
{
    entity_t new = old;
    new.position = v3_add(old.position, v3_scalef(old.velocity, dt));
    new.velocity = v3_add(old.velocity, v3_scalef(acceleration, dt*dt));
    new.velocity = v3_sub(new.velocity, v3_scalef(V3_UP, dt));
    return new;
}

/*
   void update_entities(elist_t list)
   {
   for (enode_t *node = list.first; node; node = node->next) {
   if (node->entity.flags & ENTITY_MOVE)
   move_entity(node->entity, );
   if (node->entity.flags & ENTITY_COLLIDE) {
   for (enode_t *test = list.first; test; test = test->next) {
   if (check_collision(node->entity, test->entity)) {

   }
   }
   }
   }
   }
   */