From b57dba67153279a84a16bba40ae007c8ae710056 Mon Sep 17 00:00:00 2001 From: rodri Date: Thu, 29 Jul 2021 20:03:15 +0000 Subject: incorporated libgeometry into the project. transitioned the integrator from scalar to vector-based. added a packing procedure for Point2. created the structure to model bullets. --- dat.h | 29 ++++++++++++++++------------- lobby.c | 3 ++- mkfile | 1 + musw.c | 11 +++++++---- muswd.c | 12 +++++++----- pack.c | 19 ++++++++++++++++++- party.c | 3 ++- physics.c | 35 ++++++++++++++++++----------------- sprite.c | 1 + 9 files changed, 72 insertions(+), 42 deletions(-) diff --git a/dat.h b/dat.h index 9290562..6857e69 100644 --- a/dat.h +++ b/dat.h @@ -23,10 +23,10 @@ enum { SCRHB = SCRH+2*Borderwidth }; -typedef struct Vector Vector; typedef struct VModel VModel; typedef struct Sprite Sprite; typedef struct Particle Particle; +typedef struct Bullet Bullet; typedef struct Ship Ship; typedef struct Star Star; typedef struct Universe Universe; @@ -37,23 +37,18 @@ typedef struct Player Player; typedef struct Lobby Lobby; typedef struct Party Party; -struct Vector -{ - double x, y; -}; - /* * Vector model - made out of lines and curves */ struct VModel { - Vector *pts; + Point2 *pts; ulong npts; /* WIP * l(ine) → takes 2 points * c(urve) → takes 3 points */ -// char *strokefmt; + char *strokefmt; }; struct Sprite @@ -72,18 +67,25 @@ struct Sprite struct Particle { - Vector p, v; + Point2 p, v; double yaw; double mass; }; +struct Bullet +{ + Particle; + ulong ttl; /* in s */ + int fired; /* XXX: |v| != 0 */ +}; + struct Ship { Particle; Kind kind; - uint ammo; + Bullet rounds[10]; VModel *mdl; -// Matrix mdlxform; + Matrix mdlxform; }; struct Star @@ -103,12 +105,13 @@ struct Universe struct GameState { double t, timeacc; - double x, v; + Point2 p, v; }; struct Derivative { - double dx, dv; + Point2 dx; /* v */ + Point2 dv; /* a */ }; struct Conn diff --git a/lobby.c b/lobby.c index 8ed5789..f515670 100644 --- a/lobby.c +++ b/lobby.c @@ -1,6 +1,7 @@ #include #include -#include /* because of dat.h */ +#include +#include "libgeometry/geometry.h" #include "dat.h" #include "fns.h" diff --git a/mkfile b/mkfile index 7b27036..e6c5077 100644 --- a/mkfile +++ b/mkfile @@ -13,6 +13,7 @@ OFILES=\ pack.$O\ lobby.$O\ party.$O\ + sprite.$O\ HFILES=\ dat.h\ diff --git a/musw.c b/musw.c index fddec50..006884a 100644 --- a/musw.c +++ b/musw.c @@ -4,6 +4,7 @@ #include #include #include +#include "libgeometry/geometry.h" #include "dat.h" #include "fns.h" @@ -31,7 +32,7 @@ ulong kup, kdown; typedef struct Ball Ball; struct Ball { - double x, v; + Point2 p, v; }; Ball bouncer; @@ -102,10 +103,10 @@ threadnetrecv(void *arg) io = ioproc(); while((n = ioread(io, fd, buf, sizeof buf)) > 0){ - unpack(buf, n, "dd", &bouncer.x, &bouncer.v); + unpack(buf, n, "PP", &bouncer.p, &bouncer.v); if(debug) - fprint(2, "bouncer [%g %g]\n", bouncer.x, bouncer.v); + fprint(2, "bouncer %v %v\n", bouncer.p, bouncer.v); } closeioproc(io); } @@ -134,7 +135,7 @@ redraw(void) lockdisplay(display); draw(screen, screen->r, display->black, nil, ZP); - fillellipse(screen, addpt(screen->r.min,Pt(Dx(screen->r)/2,Dy(screen->r)/2+bouncer.x)), 2, 2, display->white, ZP); + fillellipse(screen, addpt(screen->r.min,Pt(Dx(screen->r)/2,Dy(screen->r)/2+bouncer.p.y)), 2, 2, display->white, ZP); flushimage(display, 1); unlockdisplay(display); @@ -153,6 +154,7 @@ resize(void) sysfatal("resize failed"); unlockdisplay(display); + /* ignore move events */ if(Dx(screen->r) != SCRW || Dy(screen->r) != SCRH){ fd = open("/dev/wctl", OWRITE); if(fd >= 0){ @@ -179,6 +181,7 @@ threadmain(int argc, char *argv[]) Mousectl *mc; Ioproc *io; + GEOMfmtinstall(); ARGBEGIN{ case 'd': debug++; diff --git a/muswd.c b/muswd.c index d23f906..c553e29 100644 --- a/muswd.c +++ b/muswd.c @@ -1,7 +1,8 @@ #include #include #include -#include /* because of dat.h */ +#include +#include "libgeometry/geometry.h" #include "dat.h" #include "fns.h" @@ -70,7 +71,7 @@ broadcaststate(void) Party *p, *np; for(p = theparty.next; p != &theparty; p = p->next){ - n = pack(buf, sizeof buf, "dd", p->state.x, p->state.v); + n = pack(buf, sizeof buf, "PP", p->state.p, p->state.v); for(i = 0; i < nelem(p->players); i++){ if(write(p->players[i].conn.data, buf, n) != n){ @@ -90,7 +91,7 @@ void resetsim(Party *p) { memset(&p->state, 0, sizeof p->state); - p->state.x = 100; + p->state.p = Pt2(0,100,1); } void @@ -159,8 +160,8 @@ fprintstates(int fd) Party *p; for(p = theparty.next; p != &theparty; p = p->next, i++) - fprint(fd, "%lud [x %g v %g]\n", - i, p->state.x, p->state.v); + fprint(fd, "%lud [p %v v %v]\n", + i, p->state.p, p->state.v); } @@ -215,6 +216,7 @@ threadmain(int argc, char *argv[]) int acfd; char adir[40], *addr; + GEOMfmtinstall(); addr = "tcp!*!112"; /* for testing. will work out udp soon */ ARGBEGIN{ case 'a': diff --git a/pack.c b/pack.c index a1335c2..50d72f1 100644 --- a/pack.c +++ b/pack.c @@ -1,6 +1,7 @@ #include #include -#include /* because of dat.h */ +#include +#include "libgeometry/geometry.h" #include "dat.h" #include "fns.h" @@ -21,6 +22,7 @@ vpack(uchar *p, int n, char *fmt, va_list a) { uchar *p0 = p, *e = p+n; FPdbleword d; + Point2 P; for(;;){ switch(*fmt++){ @@ -35,6 +37,14 @@ vpack(uchar *p, int n, char *fmt, va_list a) put4(p, d.hi), p += 4; put4(p, d.lo), p += 4; + break; + case 'P': + P = va_arg(a, Point2); + + if(p+3*8 > e) + goto err; + + pack(p, n, "ddd", P.x, P.y, P.w), p += 3*8; break; } } @@ -47,6 +57,7 @@ vunpack(uchar *p, int n, char *fmt, va_list a) { uchar *p0 = p, *e = p+n; FPdbleword d; + Point2 P; for(;;){ switch(*fmt++){ @@ -61,6 +72,12 @@ vunpack(uchar *p, int n, char *fmt, va_list a) *va_arg(a, double*) = d.x; break; + case 'P': + if(p+3*8 > e) + goto err; + + unpack(p, n, "ddd", &P.x, &P.y, &P.w), p += 3*8; + *va_arg(a, Point2*) = P; } } err: diff --git a/party.c b/party.c index 050bd18..dd52831 100644 --- a/party.c +++ b/party.c @@ -1,6 +1,7 @@ #include #include -#include /* because of dat.h */ +#include +#include "libgeometry/geometry.h" #include "dat.h" #include "fns.h" diff --git a/physics.c b/physics.c index d0c6ae8..945ff8c 100644 --- a/physics.c +++ b/physics.c @@ -1,6 +1,7 @@ #include #include -#include /* because of dat.h */ +#include +#include "libgeometry/geometry.h" #include "dat.h" #include "fns.h" @@ -9,12 +10,12 @@ static double G = 6.674e-11; /* * Dynamics stepper */ -static double +static Point2 accel(GameState *s, double) { static double k = 15, b = 0.1; - return -k*s->x - b*s->v; + return Vec2(0, -k*s->p.y - b*s->v.y); } static Derivative @@ -23,8 +24,8 @@ eval(GameState *s0, double t, double Δt, Derivative *d) GameState s; Derivative res; - s.x = s0->x + d->dx*Δt; - s.v = s0->v + d->dv*Δt; + 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); @@ -37,13 +38,13 @@ eval(GameState *s0, double t, double Δt, Derivative *d) static void euler0(GameState *s, double t, double Δt) { - static Derivative ZD = {0,0}; + static Derivative ZD = {0}; Derivative d; d = eval(s, t, Δt, &ZD); - s->x += d.dx*Δt; - s->v += d.dv*Δt; + s->p = addpt2(s->p, mulpt2(d.dx, Δt)); + s->v = addpt2(s->v, mulpt2(d.dv, Δt)); } /* @@ -52,13 +53,13 @@ euler0(GameState *s, double t, double Δt) static void euler1(GameState *s, double t, double Δt) { - static Derivative ZD = {0,0}; + static Derivative ZD = {0}; Derivative d; d = eval(s, t, Δt, &ZD); - s->v += d.dv*Δt; - s->x += s->v*Δt; + s->v = addpt2(s->v, mulpt2(d.dv, Δt)); + s->p = addpt2(s->p, mulpt2(s->v, Δt)); } /* @@ -67,20 +68,20 @@ euler1(GameState *s, double t, double Δt) static void rk4(GameState *s, double t, double Δt) { - static Derivative ZD = {0,0}; + static Derivative ZD = {0}; Derivative a, b, c, d; - double dxdt, dvdt; + 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 = 1.0/6 * (a.dx + 2*(b.dx + c.dx) + d.dx); - dvdt = 1.0/6 * (a.dv + 2*(b.dv + c.dv) + d.dv); + 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->x += dxdt*Δt; - s->v += dvdt*Δt; + s->p = addpt2(s->p, mulpt2(dxdt, Δt)); + s->v = addpt2(s->v, mulpt2(dvdt, Δt)); } /* diff --git a/sprite.c b/sprite.c index a2bf290..cf5af36 100644 --- a/sprite.c +++ b/sprite.c @@ -1,6 +1,7 @@ #include #include #include +#include "libgeometry/geometry.h" #include "dat.h" #include "fns.h" -- cgit v1.2.3