Add random jitter to fire

This commit is contained in:
2025-10-18 17:43:22 +01:00
parent b4b46623da
commit 10b675618f

25
main.c
View File

@@ -3,10 +3,15 @@
#include <linux/input-event-codes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/time.h>
#define SHIP 0
#define FIRE 1
#define FIRE_MEAN -0.15
#define FIRE_JITTER 0.01
#define LIN_PWR 0.0001
#define ROT_PWR 0.002
@@ -48,9 +53,9 @@ static shape_t shapes[MAX_SHAPES] = {
.entity = SHIP,
.vert_count = 3,
.verts = {
{ 0.0, -0.15 },
{ 0.015, -0.07 },
{ -0.015, -0.07 },
{ 0.0, -0.15 },
},
},
};
@@ -59,6 +64,8 @@ static entity_t entities[MAX_ENTITIES] = {
{ .dir = { { 1, 0 }, { 0, 1 } } },
};
static mat3_t transforms[MAX_ENTITIES];
static unsigned shape_count = 2;
static unsigned entity_count = 1;
static float aspect;
@@ -121,6 +128,11 @@ static mat3_t entity_transform(unsigned id)
return mat3_mul_mat3(mat3_translation(e->pos), mat2_extend(e->dir));
}
static float rand_plusminus()
{
return 2.0f * ((float)rand() / RAND_MAX - 0.5f);
}
static void ship_update()
{
entity_t *ship = entities + SHIP;
@@ -132,6 +144,7 @@ static void ship_update()
ship->vel = vec2_add(ship->vel, acc);
shapes[FIRE].visible = input.fwd != 0;
shapes[FIRE].verts[0].y = FIRE_MEAN + FIRE_JITTER * rand_plusminus();
}
static void update()
@@ -152,15 +165,13 @@ static void update()
e->pos.x -= 2 * aspect;
else if (e->pos.x < -aspect)
e->pos.x += 2 * aspect;
transforms[i] = entity_transform(i);
}
}
static void draw()
{
static mat3_t transforms[MAX_ENTITIES];
for (unsigned i = 0; i < entity_count; ++i)
transforms[i] = entity_transform(i);
for (unsigned i = 0; i < shape_count; ++i) {
if (!shapes[i].visible)
continue;
@@ -172,6 +183,10 @@ static void draw()
int main()
{
struct timeval tv;
gettimeofday(&tv, NULL);
srand(tv.tv_usec);
const int input_fd = input_init();
input_on_press(key_press_callback);
input_on_release(key_release_callback);