summaryrefslogtreecommitdiff
path: root/src/entity.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/entity.c')
-rw-r--r--src/entity.c59
1 files changed, 49 insertions, 10 deletions
diff --git a/src/entity.c b/src/entity.c
index 9765f7f..f8b5de5 100644
--- a/src/entity.c
+++ b/src/entity.c
@@ -4,24 +4,63 @@
#include <stdio.h>
-struct Vec2d vec2d_add(float angle1, float length1, float angle2, float length2)
+void entity_accelerate(struct Entity *e, struct Vec2d *v)
{
- float x = sinf(angle1) * length1 + sinf(angle2) * length2;
- float y = cosf(angle1) * length1 + cosf(angle2) * length2;
+ 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 =
{
- .angle = 0.5f * M_PI - atan2f(y, x),
- .length = hypotf(x, y),
+ .x = x0 + x1,
+ .y = y0 + y1
};
return new_vec;
}
-void entity_accelerate(struct Entity *e, float angle, float acceleration)
+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)
{
- 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;
+ 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;
}