From d40305baf8a0722cf6128520a5b792fc173c5a93 Mon Sep 17 00:00:00 2001 From: rodri Date: Wed, 22 Feb 2023 21:40:54 +0000 Subject: take player input into account when stepping the simulations. --- universe.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'universe.c') diff --git a/universe.c b/universe.c index eea95a8..be481c9 100644 --- a/universe.c +++ b/universe.c @@ -8,10 +8,65 @@ #include "dat.h" #include "fns.h" +/* Ship */ + +static void +ship_forward(Ship *s, double Δt) +{ + Point2 v; + Matrix R = { + cos(s->θ), -sin(s->θ), 0, + sin(s->θ), cos(s->θ), 0, + 0, 0, 1, + }; + + v = mulpt2(xform(Vec2(1,0), R), THRUST*Δt); + s->v = addpt2(s->v, v); +} + +static void +ship_rotate(Ship *s, int dir, double Δt) +{ + s->θ += PI*dir*Δt; +} + +static void +ship_hyperjump(Ship *s) +{ + USED(s); + return; +} + +static void +ship_fire(Ship *s) +{ + int i; + Point2 bv; + Matrix R = { + cos(s->θ), -sin(s->θ), 0, + sin(s->θ), cos(s->θ), 0, + 0, 0, 1, + }; + + bv = mulpt2(xform(Vec2(1,0), R), 10*THRUST); + + for(i = 0; i < nelem(s->rounds); i++) + if(!s->rounds[i].fired){ + s->rounds[i].v = addpt2(s->v, bv); + s->rounds[i].θ = s->θ; + s->rounds[i].fired++; + break; + } +} + +/* Universe */ + static void universe_step(Universe *u, double Δt) { integrate(u, u->t, Δt); + u->timeacc -= Δt; + u->t += Δt; } static void @@ -59,6 +114,11 @@ inituniverse(Universe *u) u->ships[1].mass = 40e3; /* 40 tons */ u->ships[1].kind = WEDGE; u->ships[1].fuel = 200; + + u->ships[0].forward = u->ships[1].forward = ship_forward; + u->ships[0].rotate = u->ships[1].rotate = ship_rotate; + u->ships[0].hyperjump = u->ships[1].hyperjump = ship_hyperjump; + u->ships[0].fire = u->ships[1].fire = ship_fire; } Universe * -- cgit v1.2.3