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

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


struct Vec2d vec2d_add(float angle1, float length1, float angle2, float length2)
{
    float x = sinf(angle1) * length1 + sinf(angle2) * length2;
    float y = cosf(angle1) * length1 + cosf(angle2) * length2;

    struct Vec2d new_vec =
    {
        .angle = 0.5f * M_PI - atan2f(y, x),
        .length = hypotf(x, y),
    };
    return new_vec;
}


void entity_accelerate(struct Entity *e, float angle, float acceleration)
{
    struct Vec2d new_vec = vec2d_add(e->angle, e->speed, angle, acceleration);
    //printf("(%f, %f)\n", new_vec.angle, new_vec.length);
    e->angle = new_vec.angle;
    e->speed = new_vec.length;
}