summaryrefslogtreecommitdiff
path: root/src/entity.c
blob: f8b5de57163c971f4ed829fee54293383841795a (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
#include "entity.h"

#include <math.h>
#include <stdio.h>


void entity_accelerate(struct Entity *e, struct Vec2d *v)
{
    struct Vec2d new_vec = vec2d_add(e->velocity.x, e->velocity.y, v->x, v->y);
    //printf("(%f, %f)\n", new_vec.x, new_vec.y);
    e->velocity.x = new_vec.x;
    e->velocity.y = new_vec.y;
}


struct Vec2d vec2d_add(float x0, float y0, float x1, float y1)
{
    struct Vec2d new_vec =
    {
        .x = x0 + x1,
        .y = y0 + y1
    };
    return new_vec;
}


bool vec2d_equal(struct Vec2d v0, struct Vec2d v1)
{
    return (v0.x == v1.x) && (v0.y == v1.y);
}


float vec2d_get_angle(float x, float y)
{
    return 0.5f * M_PI - atan2f(y, x);
}


float vec2d_get_length(float x, float y)
{
    return hypotf(x, y);
}


struct Vec2d vec2d_negate(struct Vec2d v)
{
    return vec2d_scale(v.x, v.y, -1.0f);
}


struct Vec2d vec2d_normalize(float x, float y)
{
    float length = vec2d_get_length(x, y);
    return vec2d_scale(x, y, 1.0f / length);
}


struct Vec2d vec2d_scale(float x, float y, float s)
{
    struct Vec2d new_vec =
    {
        .x = x * s,
        .y = y * s
    };
    return new_vec;
}