summaryrefslogtreecommitdiff
path: root/src/game/g_physics.c
blob: 3d66e9fe0ee35d6ad3f012413f1db9e700ff6d3d (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
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Copyright (C) 2000-2006 Tim Angus

This file is part of Tremulous.

Tremulous is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

Tremulous is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Tremulous; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

#include "g_local.h"

/*
================
G_Bounce

================
*/
static void G_Bounce( gentity_t *ent, trace_t *trace )
{
  vec3_t    velocity;
  float     dot;
  int       hitTime;
  float     minNormal;
  qboolean  invert = qfalse;

  // reflect the velocity on the trace plane
  hitTime = level.previousTime + ( level.time - level.previousTime ) * trace->fraction;
  BG_EvaluateTrajectoryDelta( &ent->s.pos, hitTime, velocity );
  dot = DotProduct( velocity, trace->plane.normal );
  VectorMA( velocity, -2*dot, trace->plane.normal, ent->s.pos.trDelta );

  if( ent->s.eType == ET_BUILDABLE )
  {
    minNormal = BG_FindMinNormalForBuildable( ent->s.modelindex );
    invert = BG_FindInvertNormalForBuildable( ent->s.modelindex );
  }
  else
    minNormal = 0.707f;

  // cut the velocity to keep from bouncing forever
/* ROTAXfun
  if( ( trace->plane.normal[ 2 ] >= minNormal ||
      ( invert && trace->plane.normal[ 2 ] <= -minNormal ) ) &&
      trace->entityNum == ENTITYNUM_WORLD )
*/
  if( ( trace->plane.normal[ 2 ] >= minNormal ||
      ( invert && trace->plane.normal[ 2 ] <= -minNormal ) ) )
    VectorScale( ent->s.pos.trDelta, ent->physicsBounce, ent->s.pos.trDelta );
  else
    VectorScale( ent->s.pos.trDelta, 0.3f, ent->s.pos.trDelta );

  if( VectorLength( ent->s.pos.trDelta ) < 10 )
  {
    VectorMA( trace->endpos, 0.5f, trace->plane.normal, trace->endpos ); // make sure it is off ground
    G_SetOrigin( ent, trace->endpos );
    ent->s.groundEntityNum = trace->entityNum;
    VectorCopy( trace->plane.normal, ent->s.origin2 );
    VectorSet( ent->s.pos.trDelta, 0.0f, 0.0f, 0.0f );
    return;
  }

  VectorCopy( ent->r.currentOrigin, ent->s.pos.trBase );
  VectorAdd( ent->r.currentOrigin, trace->plane.normal, ent->r.currentOrigin);
  ent->s.pos.trTime = level.time;
}

#define PHYSICS_TIME 200

/*
================
G_Physics

================
*/
void G_Physics( gentity_t *ent, int msec )
{
  vec3_t    origin;
  trace_t   tr;
  int     contents;
  int     mask;

  // if groundentity has been set to -1, it may have been pushed off an edge
  if( ent->s.groundEntityNum == -1 )
  {
    if( ent->s.eType == ET_BUILDABLE )
    {
      if( ent->s.pos.trType != BG_FindTrajectoryForBuildable( ent->s.modelindex ) )
      {
        ent->s.pos.trType = BG_FindTrajectoryForBuildable( ent->s.modelindex );
        ent->s.pos.trTime = level.time;
      }
    }
    else if( ent->s.pos.trType != TR_GRAVITY )
    {
      ent->s.pos.trType = TR_GRAVITY;
      ent->s.pos.trTime = level.time;
    }
  }

  // trace a line from the previous position to the current position
  if( ent->clipmask )
    mask = ent->clipmask;
  else
    mask = MASK_PLAYERSOLID & ~CONTENTS_BODY;//MASK_SOLID;

  if( ent->s.pos.trType == TR_STATIONARY )
  {
    // check think function
    G_RunThink( ent );

    //check floor infrequently
    if( ent->nextPhysicsTime < level.time )
    {
      VectorCopy( ent->r.currentOrigin, origin );

      VectorMA( origin, -2.0f, ent->s.origin2, origin );

      trap_Trace( &tr, ent->r.currentOrigin, ent->r.mins, ent->r.maxs, origin, ent->s.number, mask );

      if( tr.fraction == 1.0f )
        ent->s.groundEntityNum = -1;

      ent->nextPhysicsTime = level.time + PHYSICS_TIME;
    }

    return;
  }

  // get current position
  BG_EvaluateTrajectory( &ent->s.pos, level.time, origin );

  trap_Trace( &tr, ent->r.currentOrigin, ent->r.mins, ent->r.maxs, origin, ent->s.number, mask );

  VectorCopy( tr.endpos, ent->r.currentOrigin );

  if( tr.startsolid )
    tr.fraction = 0;

  trap_LinkEntity( ent ); // FIXME: avoid this for stationary?

  // check think function
  G_RunThink( ent );

  if( tr.fraction == 1.0f )
    return;

  // if it is in a nodrop volume, remove it
  contents = trap_PointContents( ent->r.currentOrigin, -1 );
  if( contents & CONTENTS_NODROP )
  {
    G_FreeEntity( ent );
    return;
  }

  G_Bounce( ent, &tr );
}