📄 g_phys.cpp
字号:
//-----------------------------------------------------------------------------
//
// $Logfile:: /Quake 2 Engine/Sin/code/game/g_phys.cpp $
// $Revision:: 61 $
// $Author:: Markd $
// $Date:: 11/20/98 7:17p $
//
// Copyright (C) 1998 by Ritual Entertainment, Inc.
// All rights reserved.
//
// This source is may not be distributed and/or modified without
// expressly written permission by Ritual Entertainment, Inc.
//
// $Log:: /Quake 2 Engine/Sin/code/game/g_phys.cpp $
//
// 61 11/20/98 7:17p Markd
// Took out G_SnapPosition
//
// 60 11/19/98 9:28p Jimdose
// fixed some more gravaxis issues
//
// 59 11/19/98 7:54p Jimdose
// Added G_SnapPosition. Fixes bug with player getting hurt when standing on
// some moving objects.
//
// 58 11/18/98 5:23a Jimdose
// G_Physics_Toss properly checks normal an alternate grav axis
//
// 57 11/16/98 9:43p Jimdose
// fixed sliding on alternate gravaxis
//
// 56 11/16/98 8:24p Markd
// fixed vehicles getting stuck
//
// 55 11/15/98 11:32p Markd
// change fat rocket behavior a bit
//
// 54 11/15/98 8:50p Jimdose
// added sv_fatrockets
// made SV_Physics_Toss do a fulltrace when sv_fatrockets is true, allowing you
// to shoot rockets and grenades through character's legs.
//
// 53 11/13/98 10:03p Jimdose
// added G_CheckWater
// G_Physics_Step now checks water
//
// 52 10/27/98 1:49a Markd
// made physics work better with waterlevel
//
// 51 10/25/98 11:57p Jimdose
// don't call animate during intermissions
//
// 50 10/18/98 8:43p Jimdose
// removed unnecessary setting or the origin and angles when bindmaster is set
// in G_RunEntity
//
// 49 10/16/98 2:21a Jimdose
// Made gravity different in water
//
// 48 10/16/98 2:09a Aldie
//
// 47 10/08/98 6:30p Markd
// Fixed TestEntityPosition
//
// 46 10/06/98 10:17p Markd
// fixed vehicle physics
//
// 45 10/04/98 10:28p Aldie
// Added multiple weapon changes. Damage, flashes, quantum stuff
//
// 44 9/22/98 10:21p Markd
// changed default vehicle clipmask
//
// 43 9/18/98 10:59p Jimdose
// made pointcontents test in G_Physics_Toss use worldorigin
//
// 42 9/15/98 3:49p Markd
// Put blocking functionality into MOVETYPE_VEHICLES
//
// 41 9/14/98 6:12p Markd
// Added MOVETYPE_VEHICLE
//
// 40 8/31/98 7:45p Aldie
// Updated surface data structure and removed surfinfo field
//
// 39 8/29/98 9:42p Jimdose
// Made all function names consistantly begin with G_
// Added call info to G_Trace
//
// 38 8/27/98 9:05p Jimdose
// Moved STEPSIZE definition to g_local.h
//
// 37 8/22/98 9:36p Jimdose
// Added support for alternate gravity axis
//
// 36 8/18/98 11:50p Jimdose
// Made G_PushEntity so that the entity doesn't touch itself
//
// 35 8/18/98 11:08p Markd
// Added new Alias System
//
// 34 7/25/98 8:38p Markd
// put in landing sound
//
// 33 7/18/98 8:27p Jimdose
// Fixed sliding bug with entities standing on rotating entities
//
// 32 7/11/98 8:23p Jimdose
// G_Push does one less setorigin
//
// 31 7/09/98 9:38p Jimdose
// Changed calls to getLocalVector to getParentVector
// Removed quantization of move to 1/8 of a unit from G_Push
//
// 30 7/02/98 7:58p Markd
// When MOVETYPE_BOUNCE We still rotate if avelocity is non-zero
//
// 29 6/27/98 2:16p Aldie
// Changed some sliding stuff
//
// 28 6/09/98 4:19p Jimdose
// worked on ai
//
// 27 6/08/98 11:35a Aldie
// Added some slide move stuff
//
// 26 5/26/98 1:54a Markd
// Put in default water transition sounds
//
// 25 5/25/98 6:47p Jimdose
// Made animateframe, prethink and posthink into functions built into the base
// entity class
//
// 24 5/24/98 8:46p Jimdose
// Made a lot of functions more str-friendly.
// Got rid of a lot of char * based strings
// Cleaned up get spawn arg functions and sound functions
// sound functions now use consistant syntax
//
// 23 5/22/98 7:18p Jimdose
// Working on ai stuff
//
// 22 5/14/98 10:12p Jimdose
// Added extended result information for G_Movestep
//
// 21 5/03/98 4:32p Jimdose
// Changed Vector class
//
// 20 5/02/98 12:02a Jimdose
// added groundplane, groundsurface, groundcontents
// added current and conveyors to non-player entities
//
// 19 4/30/98 4:48p Jimdose
// G_Push no longer gets blocked by SOLID_TRIGGER or SOLID_NOT
//
// 18 4/20/98 2:45p Jimdose
// working on ai
//
// 17 4/18/98 3:04p Jimdose
// Made G_Physics_Toss properly update angles using setAngles
// working on ai
//
// 16 4/16/98 2:03p Jimdose
// Added G_TestMovestep
//
// 15 4/10/98 4:56p Jimdose
// only play water transitition sound on ents with mass of 0 or spawntime older
// than FRAMETIME
//
// 14 4/09/98 3:32p Jimdose
// G_RunEntity now does a setOrigin and setAngle on bound entities to keep
// their worldorigin and worlangles up to date
//
// 13 4/04/98 6:04p Jimdose
// cleared out level.impact_trace after impact
//
// 12 3/31/98 1:04a Jimdose
// Added monster movement code
//
// 11 3/30/98 2:42p Jimdose
// Made stepmove take the animation delta into account
//
// 10 3/26/98 8:20p Jimdose
// Made groundentity an edict_t *
// Changed landing sound
//
// 9 3/24/98 7:35p Jimdose
// Made change to try to fix problem where non-solid entities were being
// blocked by solid ones.
//
// 8 3/23/98 1:31p Jimdose
// Revamped event and command system
//
// 7 3/18/98 7:15p Markd
// Changed sound code call in game.h
//
// 6 3/04/98 8:01p Aldie
// More support for damage surfaces.
//
// 5 2/21/98 1:15p Jimdose
// Added G_TestMove and G_TestStepMove for use in pathfinding code
//
// 4 2/16/98 2:25p Jimdose
// Added G_PushMove to allow non-physics functions to push an entity into
// place without having to use setOrigin or messing with velocities.
// Added hierarchial object binding. Lots of changes all over the place.
//
// 3 2/06/98 5:52p Jimdose
// Converted physics to be c++
//
// 2 2/03/98 11:05a Jimdose
// In process of converting to work with Sin progs
//
// 1 1/26/98 5:48p Jimdose
//
// 3 12/30/97 6:04p Jimdose
// Added header text
//
// DESCRIPTION:
//
#include "g_local.h"
#include "sentient.h"
#include "actor.h"
extern cvar_t *sv_fatrockets;
/*
pushmove objects do not obey gravity, and do not interact with each other or trigger fields, but block normal movement and push normal objects when they move.
onground is set for toss objects when they come to a complete rest. it is set for steping or walking objects
doors, plats, etc are SOLID_BSP, and MOVETYPE_PUSH
bonus items are SOLID_TRIGGER touch, and MOVETYPE_TOSS
corpses are SOLID_NOT and MOVETYPE_TOSS
crates are SOLID_BBOX and MOVETYPE_TOSS
walking monsters are SOLID_SLIDEBOX and MOVETYPE_STEP
flying/floating monsters are SOLID_SLIDEBOX and MOVETYPE_FLY
solid_edge items only clip against bsp models.
*/
typedef struct
{
Entity *ent;
Vector origin;
Vector worldorigin;
Vector angles;
Vector worldangles;
float deltayaw;
} pushed_t;
pushed_t pushed[ MAX_EDICTS ];
pushed_t *pushed_p;
Entity *obstacle;
/*
============
G_TestEntityPosition
============
*/
Entity *G_TestEntityPosition
(
Entity *ent
)
{
int mask;
trace_t trace;
mask = ent->edict->clipmask;
if ( !mask )
mask = MASK_SOLID;
trace = G_Trace( ent->worldorigin, ent->mins, ent->maxs, ent->worldorigin, ent, mask, "G_TestEntityPosition" );
if ( trace.startsolid )
{
//return g_edicts->entity;
assert( trace.ent->entity );
return trace.ent->entity;
}
return NULL;
}
/*
================
G_CheckVelocity
================
*/
void G_CheckVelocity
(
Entity *ent
)
{
int i;
//
// bound velocity
//
for( i = 0; i < 3; i++ )
{
if ( ent->velocity[ i ] > sv_maxvelocity->value )
{
ent->velocity[ i ] = sv_maxvelocity->value;
}
else if ( ent->velocity[ i ] < -sv_maxvelocity->value )
{
ent->velocity[ i ] = -sv_maxvelocity->value;
}
}
}
/*
==================
G_Impact
Two entities have touched, so run their touch functions
==================
*/
void G_Impact
(
Entity *e1,
trace_t *trace
)
{
edict_t *e2;
Event *ev;
qboolean doImpactDamage=false;
e2 = trace->ent;
if ( ( e1->movetype == MOVETYPE_HURL ) || ( e2->entity->movetype == MOVETYPE_HURL ) )
{
doImpactDamage = true;
}
//FIXME - this should be passed in the event.
level.impact_trace = *trace;
if ( e1->edict->solid != SOLID_NOT )
{
ev = new Event( EV_Touch );
ev->AddEntity( e2->entity );
e1->ProcessEvent( ev );
if ( doImpactDamage )
{
ev = new Event( EV_Sentient_ImpactDamage );
e1->ProcessEvent( ev );
}
}
if ( e2->entity && e2->solid != SOLID_NOT )
{
ev = new Event( EV_Touch );
ev->AddEntity( e1 );
e2->entity->ProcessEvent( ev );
if ( doImpactDamage )
{
ev = new Event( EV_Sentient_ImpactDamage );
e2->entity->ProcessEvent( ev );
}
}
memset( &level.impact_trace, 0, sizeof( level.impact_trace ) );
}
/*
=============
G_AddCurrents
=============
*/
void G_AddCurrents
(
Entity *ent,
Vector *basevel
)
{
float speed;
csurface_t *surface;
float angle;
Vector vel;
vel = vec_zero;
//
// add water currents
//
if ( ent->watertype & MASK_CURRENT )
{
speed = sv_waterspeed->value;
if ( ( ent->waterlevel == 1 ) && ( ent->groundentity ) )
{
speed /= 2;
}
if ( ent->watertype & CONTENTS_CURRENT_0 )
{
vel[ 0 ] += speed;
}
if ( ent->watertype & CONTENTS_CURRENT_90 )
{
vel[ 1 ] += speed;
}
if ( ent->watertype & CONTENTS_CURRENT_180 )
{
vel[ 0 ] -= speed;
}
if ( ent->watertype & CONTENTS_CURRENT_270 )
{
vel[ 1 ] -= speed;
}
if ( ent->watertype & CONTENTS_CURRENT_UP )
{
vel[ 2 ] += speed;
}
if ( ent->watertype & CONTENTS_CURRENT_DOWN )
{
vel[ 2 ] -= speed;
}
}
//
// add conveyor belt velocities
//
if ( ent->groundentity && ent->groundsurface )
{
surface = ent->groundsurface;
if ( ( surface->flags & SURF_CONVEYOR ) && ( surface->flags & SURF_TRANSLATE ) )
{
angle = surface->trans_angle * ( 3.14159 / 180.0 );
vel[ 0 ] += -cos( angle ) * surface->trans_mag;
vel[ 1 ] += sin( angle ) * surface->trans_mag;
}
}
*basevel = vel;
}
/*
==================
G_ClipVelocity
Slide off of the impacting object
returns the blocked flags (1 = floor, 2 = step / wall)
==================
*/
#define STOP_EPSILON 0.1
int G_ClipVelocity
(
Vector& in,
Vector& normal,
Vector& out,
float overbounce,
int gravaxis
)
{
int i;
int blocked;
float backoff;
blocked = 0;
if ( ( normal[ gravity_axis[ gravaxis ].z ] * gravity_axis[ gravaxis ].sign ) > 0 )
{
// floor
blocked |= 1;
}
if ( !normal[ gravity_axis[ gravaxis ].z ] )
{
// step
blocked |= 2;
}
backoff = ( in * normal ) * overbounce;
out = in - normal * backoff;
for( i = 0; i < 3; i++ )
{
if ( out[ i ] > -STOP_EPSILON && out[ i ] < STOP_EPSILON )
{
out[ i ] = 0;
}
}
return blocked;
}
/*
============
G_FlyMove
The basic solid body movement clip that slides along multiple planes
Returns the clipflags if the velocity was modified (hit something solid)
1 = floor
2 = wall / step
4 = dead stop
============
*/
#define MAX_CLIP_PLANES 5
int G_FlyMove
(
Entity *ent,
Vector basevel,
float time,
int mask
)
{
Entity *hit;
edict_t *edict;
int bumpcount, numbumps;
Vector dir;
float d;
int numplanes;
vec3_t planes[ MAX_CLIP_PLANES ];
Vector primal_velocity, original_velocity, new_velocity;
int i, j;
trace_t trace;
Vector end;
float time_left;
int blocked;
#if 0
Vector move;
Vector v;
#endif
edict = ent->edict;
numbumps = 4;
blocked = 0;
original_velocity = ent->velocity;
primal_velocity = ent->velocity;
numplanes = 0;
#if 1
time_left = time;
#else
time_left = 1.0;//time;
v = ent->total_delta;
v[ 1 ] = -v[ 1 ]; // sigh...
MatrixTransformVector( v.vec3(), ent->orientation, move.vec3() );
move += ent->velocity * time;
ent->total_delta = vec_zero;
#endif
ent->groundentity = NULL;
for( bumpcount = 0; bumpcount < numbumps; bumpcount++ )
{
#if 1
end = ent->worldorigin + time_left * ( ent->velocity + basevel );
#else
end = ent->worldorigin + time_left * move;
#endif
trace = G_Trace( ent->worldorigin, ent->mins, ent->maxs, end, ent, mask, "G_FlyMove" );
if (
( trace.allsolid ) ||
(
( trace.startsolid ) &&
( ent->movetype == MOVETYPE_VEHICLE )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -