ref: b57dba67153279a84a16bba40ae007c8ae710056
parent: 3241d4b8c80f9424a3f725b5905def22916fc854
author: rodri <rgl@antares-labs.eu>
date: Thu Jul 29 16:03:15 EDT 2021
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.
--- a/dat.h
+++ b/dat.h
@@ -23,10 +23,10 @@
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 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 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 GameState
{
double t, timeacc;
- double x, v;
+ Point2 p, v;
};
struct Derivative
{
- double dx, dv;
+ Point2 dx; /* v */
+ Point2 dv; /* a */
};
struct Conn
--- a/lobby.c
+++ b/lobby.c
@@ -1,6 +1,7 @@
#include <u.h>
#include <libc.h>
-#include <draw.h> /* because of dat.h */
+#include <draw.h>
+#include "libgeometry/geometry.h"
#include "dat.h"
#include "fns.h"
--- a/mkfile
+++ b/mkfile
@@ -13,6 +13,7 @@
pack.$O\
lobby.$O\
party.$O\
+ sprite.$O\
HFILES=\
dat.h\
--- a/musw.c
+++ b/musw.c
@@ -4,6 +4,7 @@
#include <draw.h>
#include <mouse.h>
#include <keyboard.h>
+#include "libgeometry/geometry.h"
#include "dat.h"
#include "fns.h"
@@ -31,7 +32,7 @@
typedef struct Ball Ball;
struct Ball
{
- double x, v;
+ Point2 p, v;
};
Ball bouncer;
@@ -102,10 +103,10 @@
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 @@
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 @@
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 @@
Mousectl *mc;
Ioproc *io;
+ GEOMfmtinstall();
ARGBEGIN{
case 'd':
debug++;
--- a/muswd.c
+++ b/muswd.c
@@ -1,7 +1,8 @@
#include <u.h>
#include <libc.h>
#include <thread.h>
-#include <draw.h> /* because of dat.h */
+#include <draw.h>
+#include "libgeometry/geometry.h"
#include "dat.h"
#include "fns.h"
@@ -70,7 +71,7 @@
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 @@
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 @@
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 @@
int acfd;
char adir[40], *addr;
+ GEOMfmtinstall();
addr = "tcp!*!112"; /* for testing. will work out udp soon */
ARGBEGIN{
case 'a':
--- a/pack.c
+++ b/pack.c
@@ -1,6 +1,7 @@
#include <u.h>
#include <libc.h>
-#include <draw.h> /* because of dat.h */
+#include <draw.h>
+#include "libgeometry/geometry.h"
#include "dat.h"
#include "fns.h"
@@ -21,6 +22,7 @@
{
uchar *p0 = p, *e = p+n;
FPdbleword d;
+ Point2 P;
for(;;){
switch(*fmt++){
@@ -36,6 +38,14 @@
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;
}
}
err:
@@ -47,6 +57,7 @@
{
uchar *p0 = p, *e = p+n;
FPdbleword d;
+ Point2 P;
for(;;){
switch(*fmt++){
@@ -61,6 +72,12 @@
*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:
--- a/party.c
+++ b/party.c
@@ -1,6 +1,7 @@
#include <u.h>
#include <libc.h>
-#include <draw.h> /* because of dat.h */
+#include <draw.h>
+#include "libgeometry/geometry.h"
#include "dat.h"
#include "fns.h"
--- a/physics.c
+++ b/physics.c
@@ -1,6 +1,7 @@
#include <u.h>
#include <libc.h>
-#include <draw.h> /* because of dat.h */
+#include <draw.h>
+#include "libgeometry/geometry.h"
#include "dat.h"
#include "fns.h"
@@ -9,12 +10,12 @@
/*
* 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 @@
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 @@
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 @@
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,9 +68,9 @@
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);
@@ -76,11 +77,11 @@
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));
}
/*
--- a/sprite.c
+++ b/sprite.c
@@ -1,6 +1,7 @@
#include <u.h>
#include <libc.h>
#include <draw.h>
+#include "libgeometry/geometry.h"
#include "dat.h"
#include "fns.h"