summaryrefslogtreecommitdiff
path: root/src/game/physics
diff options
context:
space:
mode:
authoraura <nw@moneybot.cc>2026-02-27 04:43:15 +0100
committeraura <nw@moneybot.cc>2026-02-27 04:43:15 +0100
commitb24f37279bcc4b3abd92b697eadcec1feba8d276 (patch)
tree8fe58bee0ae000396d7d048a5630c15bf479af58 /src/game/physics
parentb384930de5044934207d1b2ceb4fa55705094f8b (diff)
fix edge collisions, update readme
Diffstat (limited to 'src/game/physics')
-rw-r--r--src/game/physics/movement.cpp58
1 files changed, 48 insertions, 10 deletions
diff --git a/src/game/physics/movement.cpp b/src/game/physics/movement.cpp
index c9b7688..ecee539 100644
--- a/src/game/physics/movement.cpp
+++ b/src/game/physics/movement.cpp
@@ -125,8 +125,6 @@ void gmove_accelerate( VEC3& wishdir, F32 wishspeed, F32 accel ) {
gmove->velocity += wishdir * accelspeed;
}
-// todo: player hull extends at center instead of at feet
-// should extend at feet
void gmove_stay_on_ground() {
BSP_TRACE tr{};
VEC3 start = gmove->pos;
@@ -155,9 +153,10 @@ void gmove_categorize_pos() {
}
-VEC3 gmove_clip_planes( VEC3 vel, LIST<VEC3>* planes, F32 overbounce ) {
- if( planes->size > 2 )
- vel = {};
+VEC3 gmove_clip_planes( VEC3 vel, VEC3* pos, LIST<VEC3>* planes, F32 overbounce ) {
+ if( planes->size > 2 ) {
+ return {};
+ }
planes->each( fn( VEC3* p ) {
vel = gmove_clip_velocity( vel, *p, overbounce );
@@ -168,6 +167,7 @@ VEC3 gmove_clip_planes( VEC3 vel, LIST<VEC3>* planes, F32 overbounce ) {
if( vec_dot( vel, planes->data[i] ) < 0.f ) {
VEC3 dir = vec_cross( planes->data[0], planes->data[1] );
F32 len = vec_len( dir );
+
if( len > 0.00001f ) {
dir *= 1.0f / len;
vel = dir * vec_dot( vel, dir );
@@ -183,6 +183,43 @@ VEC3 gmove_clip_planes( VEC3 vel, LIST<VEC3>* planes, F32 overbounce ) {
return vel;
}
+void gmove_check_stuck( VEC3* pos ) {
+ BSP_TRACE t;
+ bsp_trace( &t, gmove->bsp, gmove->aabb, *pos, *pos );
+ if( !t.hit )
+ return;
+
+ VEC3 p1 = *pos;
+
+ // try nudge away from wall
+ *pos = p1 + t.normal * BSP_TRACE_EPSILON * 2.f;
+ bsp_trace( &t, gmove->bsp, gmove->aabb, *pos, *pos );
+ if( !t.hit )
+ return;
+
+ // nudge +90deg away horizontal
+ *pos = p1 + VEC3{ -t.normal.y, t.normal.x, t.normal.z } * BSP_TRACE_EPSILON * 2.f;
+ bsp_trace( &t, gmove->bsp, gmove->aabb, *pos, *pos );
+ if( !t.hit )
+ return;
+
+ // nudge -90deg away horizontal
+ *pos = p1 + VEC3{ t.normal.y, -t.normal.x, t.normal.z } * BSP_TRACE_EPSILON * 2.f;
+ bsp_trace( &t, gmove->bsp, gmove->aabb, *pos, *pos );
+ if( !t.hit )
+ return;
+
+ // nudge -180deg away horizontal
+ *pos = p1 + VEC3{ t.normal.y, t.normal.x, -t.normal.z } * BSP_TRACE_EPSILON * 2.f;
+ bsp_trace( &t, gmove->bsp, gmove->aabb, *pos, *pos );
+ if( !t.hit )
+ return;
+
+ // todo : loop over every dir from current pos and incrementally increase distance.
+ // kinda like source
+ *pos = p1; // restore original pos - failed to unstuck
+}
+
F32 gmove_try_move( BSP_TRACE* t, VEC3* pos, VEC3* vel ) {
F32 dt = TICK_INTERVAL;
AABB aabb = gmove->aabb;
@@ -198,7 +235,8 @@ F32 gmove_try_move( BSP_TRACE* t, VEC3* pos, VEC3* vel ) {
*pos += wishmove * t->frac;
// nudge player away from wall
- *pos += t->normal * (BSP_TRACE_EPSILON * 2.f);
+ if( vec_dot( *vel, t->normal ) < 0.f )
+ *pos += t->normal * (BSP_TRACE_EPSILON * 2.f);
planes.push( t->normal );
@@ -206,7 +244,7 @@ F32 gmove_try_move( BSP_TRACE* t, VEC3* pos, VEC3* vel ) {
if( dt <= 0.0001f )
break;
- *vel = gmove_clip_planes( *vel, &planes, 1.f /* todo : surface friction */ );
+ *vel = gmove_clip_planes( *vel, pos, &planes, 1.f /* todo : surface friction */ );
}
return dt < 0 ? 0 : dt;
@@ -279,23 +317,23 @@ void gmove_walk_move() {
gmove_accelerate( wishdir, speed, var_getf( mv_accelerate ) );
speed = vec_len( vel );
+ defer( gmove_check_stuck( &gmove->pos ) );
if( speed < 1.f ) {
gmove->velocity = {};
return;
}
- VEC3 dest = gmove->pos + vel * TICK_INTERVAL;
+ VEC3 dest = gmove->pos + gmove->velocity * TICK_INTERVAL;
BSP_TRACE tr{};
bsp_trace( &tr, gmove->bsp, gmove->aabb, gmove->pos, dest );
+ defer( gmove_stay_on_ground() );
if( !tr.hit ) {
gmove->pos = dest;
- gmove_stay_on_ground();
return;
}
gmove_step_move( dest, &tr );
- gmove_stay_on_ground();
}
void gmove_full_walk_move() {