From b8379bd4d9bd65ae85c7d08554dc6ded0d9e1b7d Mon Sep 17 00:00:00 2001 From: rodri Date: Fri, 3 Jun 2022 20:36:54 +0000 Subject: got rid of GameState. --- dat.h | 10 ----- fns.h | 3 +- musw.c | 14 ++----- muswd.c | 20 +--------- physics.c | 129 +++++++++++-------------------------------------------------- universe.c | 2 +- 6 files changed, 29 insertions(+), 149 deletions(-) diff --git a/dat.h b/dat.h index 823a94a..40171cf 100644 --- a/dat.h +++ b/dat.h @@ -30,7 +30,6 @@ typedef struct Bullet Bullet; typedef struct Ship Ship; typedef struct Star Star; typedef struct Universe Universe; -typedef struct GameState GameState; typedef struct Derivative Derivative; typedef struct Conn Conn; typedef struct Player Player; @@ -106,12 +105,6 @@ struct Universe void (*reset)(Universe*); }; -struct GameState -{ - double t, timeacc; - Point2 p, v; -}; - struct Derivative { Point2 dx; /* v */ @@ -155,9 +148,6 @@ struct Party Player players[2]; /* the needle and the wedge */ Universe *u; Party *prev, *next; - - /* testing */ - GameState state; }; diff --git a/fns.h b/fns.h index e8d026d..565b52c 100644 --- a/fns.h +++ b/fns.h @@ -10,8 +10,7 @@ void *erealloc(void*, ulong); /* * physics */ -void integrate(GameState*, double, double); -void integrateu(Universe*, double, double); +void integrate(Universe*, double, double); /* * nanosec diff --git a/musw.c b/musw.c index e0f031b..51e124f 100644 --- a/musw.c +++ b/musw.c @@ -30,14 +30,7 @@ Keymap kmap[] = { }; ulong kdown; -typedef struct Ball Ball; -struct Ball -{ - Point2 p, v; -}; - RFrame screenrf; -Ball bouncer; Universe *universe; VModel *needlemdl; Image *skymap; @@ -123,11 +116,11 @@ drawship(Ship *ship, Image *dst) Matrix T = { 1, 0, ship->p.x, 0, 1, ship->p.y, - 0, 0, 1 + 0, 0, 1, }, R = { cos(ship->θ), -sin(ship->θ), 0, sin(ship->θ), cos(ship->θ), 0, - 0, 0, 1 + 0, 0, 1, }; mulm(T, R); @@ -212,7 +205,7 @@ threadnetrecv(void *arg) io = ioproc(); while((n = ioread(io, fd, buf, sizeof buf)) > 0){ - unpack(buf, n, "PPdPdP", &bouncer.p, + unpack(buf, n, "PdPdP", &universe->ships[0].p, &universe->ships[0].θ, &universe->ships[1].p, &universe->ships[1].θ, &universe->star.p); @@ -279,7 +272,6 @@ redraw(void) lockdisplay(display); draw(screen, screen->r, skymap, nil, ZP); - fillellipse(screen, toscreen(bouncer.p), 2, 2, display->white, ZP); drawship(&universe->ships[0], screen); drawship(&universe->ships[1], screen); diff --git a/muswd.c b/muswd.c index 88d1bb7..339a4ef 100644 --- a/muswd.c +++ b/muswd.c @@ -91,8 +91,7 @@ broadcaststate(void) Party *p; for(p = theparty.next; p != &theparty; p = p->next){ - n = pack(buf, sizeof buf, "PPdPdP", - p->state.p, + n = pack(buf, sizeof buf, "PdPdP", p->u->ships[0].p, p->u->ships[0].θ, p->u->ships[1].p, p->u->ships[1].θ, p->u->star.p); @@ -111,13 +110,6 @@ broadcaststate(void) } -void -resetsim(Party *p) -{ - memset(&p->state, 0, sizeof p->state); - p->state.p = Pt2(0,100,1); -} - void threadsim(void *) { @@ -136,7 +128,6 @@ threadsim(void *) if(lobby->getcouple(lobby, couple) != -1){ newparty(couple); - resetsim(theparty.prev); theparty.prev->u->reset(theparty.prev->u); } @@ -146,19 +137,12 @@ threadsim(void *) for(p = theparty.next; p != &theparty; p = p->next){ p->u->timeacc += frametime/1e9; - p->state.timeacc += frametime/1e9; while(p->u->timeacc >= Δt){ p->u->step(p->u, Δt); p->u->timeacc -= Δt; p->u->t += Δt; } - - while(p->state.timeacc >= Δt){ - integrate(&p->state, p->state.t, Δt); - p->state.timeacc -= Δt; - p->state.t += Δt; - } } broadcaststate(); @@ -193,8 +177,6 @@ fprintstates(int fd) Ship *s; for(p = theparty.next; p != &theparty; p = p->next, i++){ - fprint(fd, "%lud p %v v %v\n", - i, p->state.p, p->state.v); for(s = &p->u->ships[0]; s-p->u->ships < nelem(p->u->ships); s++){ fprint(fd, "%lud s%lld k%d p %v v %v θ %g ω %g m %g f %d\n", i, s-p->u->ships, s->kind, s->p, s->v, s->θ, s->ω, s->mass, s->fuel); diff --git a/physics.c b/physics.c index 737adf0..5c8879d 100644 --- a/physics.c +++ b/physics.c @@ -7,108 +7,16 @@ static double G = 6.674e-11; -/* - * Dynamics stepper - */ -static Point2 -accel(GameState *s, double) -{ - static double k = 15, b = 0.1; - - return Vec2(0, -k*s->p.y - b*s->v.y); -} - -static Derivative -eval(GameState *s0, double t, double Δt, Derivative *d) -{ - GameState s; - Derivative res; - - s.p = addpt2(s0->p, mulpt2(d->dx, Δt)); - s.v = addpt2(s0->v, mulpt2(d->dv, Δt)); - - res.dx = s.v; - res.dv = accel(&s, t+Δt); - return res; -} - -/* - * Explicit Euler Integrator - */ -static void -euler0(GameState *s, double t, double Δt) -{ - static Derivative ZD = {0}; - Derivative d; - - d = eval(s, t, Δt, &ZD); - - s->p = addpt2(s->p, mulpt2(d.dx, Δt)); - s->v = addpt2(s->v, mulpt2(d.dv, Δt)); -} - -/* - * Semi-implicit Euler Integrator - */ -static void -euler1(GameState *s, double t, double Δt) -{ - static Derivative ZD = {0}; - Derivative d; - - d = eval(s, t, Δt, &ZD); - - s->v = addpt2(s->v, mulpt2(d.dv, Δt)); - s->p = addpt2(s->p, mulpt2(s->v, Δt)); -} /* - * RK4 Integrator - */ -static void -rk4(GameState *s, double t, double Δt) -{ - static Derivative ZD = {0}; - Derivative a, b, c, d; - Point2 dxdt, dvdt; - - a = eval(s, t, 0, &ZD); - b = eval(s, t, Δt/2, &a); - c = eval(s, t, Δt/2, &b); - d = eval(s, t, Δt, &c); - - dxdt = divpt2(addpt2(addpt2(a.dx, mulpt2(addpt2(b.dx, c.dx), 2)), d.dx), 6); - dvdt = divpt2(addpt2(addpt2(a.dv, mulpt2(addpt2(b.dv, c.dv), 2)), d.dv), 6); - - s->p = addpt2(s->p, mulpt2(dxdt, Δt)); - s->v = addpt2(s->v, mulpt2(dvdt, Δt)); -} - -/* - * The Integrator - */ -void -integrate(GameState *s, double t, double Δt) -{ - //euler0(s, t, Δt); - //euler1(s, t, Δt); - rk4(s, t, Δt); -} - -/* - * - * UNIVERSE MIGRATION. KEEP CALM AND FASTEN YOUR SEAT BELTS. - * - */ - -/* - * XXX: remember to take thrust into account, based on user input. + * Dynamics stepper */ static Point2 accelship(Universe *u, Particle *p, double) { double g, d; + /* XXX: remember to take thrust into account, based on user input. */ d = vec2len(subpt2(u->star.p, p->p)); d *= 1e5; /* scale to the 100km/px range */ g = G*u->star.mass/(d*d); @@ -122,7 +30,7 @@ accelbullet(Universe *, Particle *, double) } static Derivative -evalu(Universe *u, Particle *p0, double t, double Δt, Derivative *d, Point2 (*a)(Universe*,Particle*,double)) +eval(Universe *u, Particle *p0, double t, double Δt, Derivative *d, Point2 (*accel)(Universe*,Particle*,double)) { Particle p; Derivative res; @@ -131,33 +39,39 @@ evalu(Universe *u, Particle *p0, double t, double Δt, Derivative *d, Point2 (*a p.v = addpt2(p0->v, mulpt2(d->dv, Δt)); res.dx = p.v; - res.dv = a(u, &p, t+Δt); + res.dv = accel(u, &p, t+Δt); return res; } +/* + * Semi-implicit Euler Integrator + */ static void -euler1u(Universe *u, Particle *p, double t, double Δt) +euler1(Universe *u, Particle *p, double t, double Δt, Point2 (*accel)(Universe*,Particle*,double)) { static Derivative ZD = {0}; Derivative d; - d = evalu(u, p, t, Δt, &ZD); + d = eval(u, p, t, Δt, &ZD, accel); p->v = addpt2(p->v, mulpt2(d.dv, Δt)); p->p = addpt2(p->p, mulpt2(p->v, Δt)); } +/* + * RK4 Integrator + */ static void -rk4u(Universe *u, Particle *p, double t, double Δt, Point2 (*acc)(Universe*,Particle*,double)) +rk4(Universe *u, Particle *p, double t, double Δt, Point2 (*accel)(Universe*,Particle*,double)) { static Derivative ZD = {0}; Derivative a, b, c, d; Point2 dxdt, dvdt; - a = evalu(u, p, t, 0, &ZD, acc); - b = evalu(u, p, t, Δt/2, &a, acc); - c = evalu(u, p, t, Δt/2, &b, acc); - d = evalu(u, p, t, Δt, &c, acc); + a = eval(u, p, t, 0, &ZD, accel); + b = eval(u, p, t, Δt/2, &a, accel); + c = eval(u, p, t, Δt/2, &b, accel); + d = eval(u, p, t, Δt, &c, accel); dxdt = divpt2(addpt2(addpt2(a.dx, mulpt2(addpt2(b.dx, c.dx), 2)), d.dx), 6); dvdt = divpt2(addpt2(addpt2(a.dv, mulpt2(addpt2(b.dv, c.dv), 2)), d.dv), 6); @@ -166,15 +80,18 @@ rk4u(Universe *u, Particle *p, double t, double Δt, Point2 (*acc)(Universe*,Par p->v = addpt2(p->v, mulpt2(dvdt, Δt)); } +/* + * The Integrator + */ void -integrateu(Universe *u, double t, double Δt) +integrate(Universe *u, double t, double Δt) { int i, j; for(i = 0; i < nelem(u->ships); i++){ - rk4u(u, &u->ships[i], t, Δt, accelship); + rk4(u, &u->ships[i], t, Δt, accelship); for(j = 0; j < nelem(u->ships[i].rounds); j++) if(u->ships[i].rounds[j].fired) - euler1u(u, &u->ships[i].rounds[j], t, Δt, accelbullet); + euler1(u, &u->ships[i].rounds[j], t, Δt, accelbullet); } } diff --git a/universe.c b/universe.c index e1e92d3..b620a61 100644 --- a/universe.c +++ b/universe.c @@ -8,7 +8,7 @@ static void universe_step(Universe *u, double Δt) { - integrateu(u, u->t, Δt); + integrate(u, u->t, Δt); } static void -- cgit v1.2.3