summaryrefslogtreecommitdiff
path: root/src/game/physics/movement.cpp
blob: e9e0132fdb5e4e0c2956522d93f0d63f4510462b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "movement.h"
#include "../world/trace.h"
#include "../../util/math.h"
#include "../../util/string.h"
#include "../../game.h"

GAME_MOVEMENT* gmove;

void gmove_init( GAME_DATA* game ) {
  gmove = new GAME_MOVEMENT;
  gmove->game = game;
}

U8 gmove_check_valid( STR<256> from ) {
  if( !gmove->game ) {
    dlog( "%s : game not init\n", from.data );
    return 0;
  }

  if( !gmove->pl ) {
    dlog( "%s : player null\n", from.data );
    return 0;
  }

  if( !gmove->input ) {
    dlog( "%s : input null\n", from.data );
    return 0;
  }

  if( !gmove->game->state.map ) {
    dlog( "%s : no map loaded\n", from.data );
    return 0;
  }

  if( !gmove->game->state.map->bsp ) {
    dlog( "%s : no bsp loaded\n", from.data );
    return 0;
  }

  return 1;
}

void gmove_set_player( PLAYER* player ) {
  gmove->pl = player;
  gmove->input = &player->input;
}

VEC3 gmove_clip_velocity( VEC3 in, VEC3 norm, F32 overbounce ) {
  F32 blocked = vec_dot( in, norm );
  F32 backoff = blocked * overbounce;
  VEC3 out = in - norm * backoff;

  F32 adjust = vec_dot( out, norm );
  if( adjust < 0.f )
    out -= (norm * adjust);

  // uncomment to enable quake/hl1-like wallboosting
  // F32 len = vec_len( out );
  // if( len > 0.f )
  //   out *= ( -1.f * blocked + len ) / len;

  return out;
}

VEC3 gmove_clip_planes( VEC3 vel, LIST<VEC3>* planes, F32 overbounce ) {
  if( planes->size > 2 )
    vel = {};

  planes->each( fn( VEC3* p ) {
    vel = gmove_clip_velocity( vel, *p, overbounce );
  } );

  if( planes->size > 1 ) {
    for( U32 i = 0; i < planes->size; ++i ) {
      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 );
        } else {
          vel = {};
        }

        break;
      }
    }
  }

  return vel;
}

F32 gmove_try_move( BSP_TRACE* t, AABB aabb, VEC3* pos, VEC3 vel ) {
  F32 dt = TICK_INTERVAL;
  LIST<VEC3> planes;

  for( U32 bump = 0; bump < 4; ++bump ) {
    VEC3 wishmove = vel * dt;
    bsp_trace( t, gmove->game->state.map->bsp, aabb, *pos, *pos + wishmove );
    if( !t->hit ) {
      *pos = t->point;
      break;
    }

    *pos += wishmove * t->frac;
    // nudge player away from wall
    *pos += t->normal * (BSP_TRACE_EPSILON * 2.f);

    planes.push( t->normal );

    dt *= (1.f - t->frac);
    if( dt <= 0.0001f )
      break;

    vel = gmove_clip_planes( vel, &planes, 1.f /* surface friction */ );
  }

  return dt < 0 ? 0 : dt;
}

void gmove_walk_move() {
  PLAYER* p = gmove->pl;
  VEC2    move = gmove->input->move;
  F32     yawrad = m_deg2rad( p->rot.y );
  VEC3    wishdir = {
    move.x * cosf( yawrad ) - move.y * sinf( yawrad ),
    move.y * cosf( yawrad ) + move.x * sinf( yawrad ),
    0
  };

  VEC3 vel = wishdir * p->maxspeed;
  VEC3 wishmove = vel * TICK_INTERVAL;
  p->velocity = vel;

  if( vec_len( wishmove ) < BSP_TRACE_EPSILON )
    return;

  BSP_TRACE tr{};
  AABB aabb{};
  aabb.min = p->mins;
  aabb.max = p->maxs;
  VEC3 npos = p->pos;

  F32 frac = gmove_try_move( &tr, aabb, &npos, vel );
  p->velocity *= frac / TICK_INTERVAL;
  p->pos = npos;
}

void gmove_process_move() {
  gmove_walk_move();
}

void gmove_tick() {
  if( !gmove_check_valid( "gmove_tick()" ) )
    return;
  gmove_process_move();
}