shithub: sce

Download patch

ref: 5fc2eb49dfbd74b98298494e6b153edde1f8bc62
parent: d54fa816224b4611420dd2f8c3931daaeab29514
author: qwx <qwx@sciops.net>
date: Tue Sep 8 18:48:09 EDT 2020

sim: use correct grid for movement and adjust deceleration treshold

using the pathfinding grid for movement means that we'll stop
moving as soon as target path coordinates are reached, but
that means we'd undershoot the target if we only touch the
path node boundary.
use pixel grid instead.

adjustment of deceleration heuristic needed to prevent stopping
before target is reached.

--- a/drw.c
+++ b/drw.c
@@ -253,7 +253,7 @@
 	}
 	if((mo = selected[0]) != nil && mo->pathp != nil){
 		for(p=mo->paths; p<mo->pathe; p++)
-			compose(p->x, p->y, 0x00ff00);
+			compose(p->x / Tlsubwidth, p->y / Tlsubheight, 0x00ff00);
 		compose(mo->target.x, mo->target.y, 0x00ffff);
 	}
 }
--- a/path.c
+++ b/path.c
@@ -381,8 +381,8 @@
 	p = mo->paths + n->step;
 	mo->pathe = p--;
 	for(; n!=a; n=n->from){
-		x = n->x;
-		y = n->y;
+		x = n->x * Tlsubwidth;
+		y = n->y * Tlsubheight;
 		*p-- = (Point){x, y};
 	}
 	assert(p == mo->paths - 1);
--- a/sim.c
+++ b/sim.c
@@ -58,8 +58,8 @@
 	int dx, dy;
 	double vx, vy, d, θ, θ256, Δθ;
 
-	dx = p.x - mo->x;
-	dy = p.y - mo->y;
+	dx = p.x - mo->px;
+	dy = p.y - mo->py;
 	d = sqrt(dx * dx + dy * dy);
 	vx = dx / d;
 	vy = dy / d;
@@ -173,7 +173,7 @@
 static void
 updatespeed(Mobj *mo)
 {
-	if(mo->pathlen < (mo->speed / 8) * (mo->speed / 8) / 2 / (mo->o->accel / 8)){
+	if(1 + mo->pathlen < (mo->speed / 8) * (mo->speed / 8) / 2 / (mo->o->accel / 8)){
 		mo->speed -= mo->o->accel;
 		if(mo->speed < 0.0)
 			mo->speed = 0.0;
@@ -201,8 +201,8 @@
 	Δy = abs(Δv);
 	Δrx = fabs(mo->u * mo->speed) * (1 << Subpxshift);
 	Δry = fabs(mo->v * mo->speed) * (1 << Subpxshift);
-	Δpx = abs((mo->pathp->x * Tlsubwidth << Subpxshift) - sx);
-	Δpy = abs((mo->pathp->y * Tlsubwidth << Subpxshift) - sy);
+	Δpx = abs((mo->pathp->x << Subpxshift) - sx);
+	Δpy = abs((mo->pathp->y << Subpxshift) - sy);
 	if(Δpx < Δrx)
 		Δrx = Δpx;
 	if(Δpy < Δry)
@@ -299,7 +299,7 @@
 		}
 		goto restart;
 	}
-	if(mo->x == mo->pathp->x && mo->y == mo->pathp->y){
+	if(mo->px == mo->pathp->x && mo->py == mo->pathp->y){
 		mo->pathp++;
 		if(mo->pathp < mo->pathe){
 			nextmove(mo);