codemp/game/q_math.c File Reference

#include "q_shared.h"

Go to the source code of this file.

Functions

int Q_rand (int *seed)
float Q_random (int *seed)
float Q_crandom (int *seed)
signed char ClampChar (int i)
signed short ClampShort (int i)
int DirToByte (vec3_t dir)
void ByteToDir (int b, vec3_t dir)
unsigned ColorBytes3 (float r, float g, float b)
unsigned ColorBytes4 (float r, float g, float b, float a)
float NormalizeColor (const vec3_t in, vec3_t out)
qboolean PlaneFromPoints (vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c)
void RotatePointAroundVector (vec3_t dst, const vec3_t dir, const vec3_t point, float degrees)
void RotateAroundDirection (vec3_t axis[3], float yaw)
void vectoangles (const vec3_t value1, vec3_t angles)
void AnglesToAxis (const vec3_t angles, vec3_t axis[3])
void AxisClear (vec3_t axis[3])
void AxisCopy (vec3_t in[3], vec3_t out[3])
void ProjectPointOnPlane (vec3_t dst, const vec3_t p, const vec3_t normal)
void MakeNormalVectors (const vec3_t forward, vec3_t right, vec3_t up)
void VectorRotate (vec3_t in, vec3_t matrix[3], vec3_t out)
float Q_rsqrt (float number)
float Q_fabs (float f)
float LerpAngle (float from, float to, float frac)
float AngleSubtract (float a1, float a2)
void AnglesSubtract (vec3_t v1, vec3_t v2, vec3_t v3)
float AngleMod (float a)
float AngleNormalize360 (float angle)
float AngleNormalize180 (float angle)
float AngleDelta (float angle1, float angle2)
void SetPlaneSignbits (cplane_t *out)
int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
float RadiusFromBounds (const vec3_t mins, const vec3_t maxs)
void ClearBounds (vec3_t mins, vec3_t maxs)
vec_t DistanceHorizontal (const vec3_t p1, const vec3_t p2)
vec_t DistanceHorizontalSquared (const vec3_t p1, const vec3_t p2)
void AddPointToBounds (const vec3_t v, vec3_t mins, vec3_t maxs)
vec_t VectorNormalize (vec3_t v)
vec_t VectorNormalize2 (const vec3_t v, vec3_t out)
void _VectorMA (const vec3_t veca, float scale, const vec3_t vecb, vec3_t vecc)
vec_t _DotProduct (const vec3_t v1, const vec3_t v2)
void _VectorSubtract (const vec3_t veca, const vec3_t vecb, vec3_t out)
void _VectorAdd (const vec3_t veca, const vec3_t vecb, vec3_t out)
void _VectorCopy (const vec3_t in, vec3_t out)
void _VectorScale (const vec3_t in, vec_t scale, vec3_t out)
void Vector4Scale (const vec4_t in, vec_t scale, vec4_t out)
int Q_log2 (int val)
void MatrixMultiply (float in1[3][3], float in2[3][3], float out[3][3])
void AngleVectors (const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
void PerpendicularVector (vec3_t dst, const vec3_t src)
void NormalToLatLong (const vec3_t normal, byte bytes[2])
void Rand_Init (int seed)
float flrand (float min, float max)
float Q_flrand (float min, float max)
int irand (int min, int max)
int Q_irand (int value1, int value2)
float powf (float x, int y)
float DotProductNormalize (const vec3_t inVec1, const vec3_t inVec2)
qboolean G_FindClosestPointOnLineSegment (const vec3_t start, const vec3_t end, const vec3_t from, vec3_t result)
float G_PointDistFromLineSegment (const vec3_t start, const vec3_t end, const vec3_t from)

Variables

vec3_t vec3_origin = {0,0,0}
vec3_t axisDefault [3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } }
vec4_t colorBlack = {0, 0, 0, 1}
vec4_t colorRed = {1, 0, 0, 1}
vec4_t colorGreen = {0, 1, 0, 1}
vec4_t colorBlue = {0, 0, 1, 1}
vec4_t colorYellow = {1, 1, 0, 1}
vec4_t colorMagenta = {1, 0, 1, 1}
vec4_t colorCyan = {0, 1, 1, 1}
vec4_t colorWhite = {1, 1, 1, 1}
vec4_t colorLtGrey = {0.75, 0.75, 0.75, 1}
vec4_t colorMdGrey = {0.5, 0.5, 0.5, 1}
vec4_t colorDkGrey = {0.25, 0.25, 0.25, 1}
vec4_t colorLtBlue = {0.367f, 0.261f, 0.722f, 1}
vec4_t colorDkBlue = {0.199f, 0.0f, 0.398f, 1}
vec4_t g_color_table [8]
vec3_t bytedirs [NUMVERTEXNORMALS]


Function Documentation

vec_t _DotProduct const vec3_t  v1,
const vec3_t  v2
 

Definition at line 1221 of file q_math.c.

References vec3_t, and vec_t.

01221                                                       {
01222         return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
01223 }

void _VectorAdd const vec3_t  veca,
const vec3_t  vecb,
vec3_t  out
 

Definition at line 1231 of file q_math.c.

References vec3_t.

01231                                                                     {
01232         out[0] = veca[0]+vecb[0];
01233         out[1] = veca[1]+vecb[1];
01234         out[2] = veca[2]+vecb[2];
01235 }

void _VectorCopy const vec3_t  in,
vec3_t  out
 

Definition at line 1237 of file q_math.c.

References vec3_t.

01237                                                 {
01238         out[0] = in[0];
01239         out[1] = in[1];
01240         out[2] = in[2];
01241 }

void _VectorMA const vec3_t  veca,
float  scale,
const vec3_t  vecb,
vec3_t  vecc
 

Definition at line 1214 of file q_math.c.

References vec3_t.

01214                                                                                 {
01215         vecc[0] = veca[0] + scale*vecb[0];
01216         vecc[1] = veca[1] + scale*vecb[1];
01217         vecc[2] = veca[2] + scale*vecb[2];
01218 }

void _VectorScale const vec3_t  in,
vec_t  scale,
vec3_t  out
 

Definition at line 1243 of file q_math.c.

References vec3_t, and vec_t.

01243                                                               {
01244         out[0] = in[0]*scale;
01245         out[1] = in[1]*scale;
01246         out[2] = in[2]*scale;
01247 }

void _VectorSubtract const vec3_t  veca,
const vec3_t  vecb,
vec3_t  out
 

Definition at line 1225 of file q_math.c.

References vec3_t.

01225                                                                          {
01226         out[0] = veca[0]-vecb[0];
01227         out[1] = veca[1]-vecb[1];
01228         out[2] = veca[2]-vecb[2];
01229 }

void AddPointToBounds const vec3_t  v,
vec3_t  mins,
vec3_t  maxs
 

Definition at line 1148 of file q_math.c.

References vec3_t.

Referenced by Think_SpawnNewDoorTrigger().

01148                                                                   {
01149         if ( v[0] < mins[0] ) {
01150                 mins[0] = v[0];
01151         }
01152         if ( v[0] > maxs[0]) {
01153                 maxs[0] = v[0];
01154         }
01155 
01156         if ( v[1] < mins[1] ) {
01157                 mins[1] = v[1];
01158         }
01159         if ( v[1] > maxs[1]) {
01160                 maxs[1] = v[1];
01161         }
01162 
01163         if ( v[2] < mins[2] ) {
01164                 mins[2] = v[2];
01165         }
01166         if ( v[2] > maxs[2]) {
01167                 maxs[2] = v[2];
01168         }
01169 }

float AngleDelta float  angle1,
float  angle2
 

Definition at line 738 of file q_math.c.

References AngleNormalize180().

Referenced by ClientThink_real(), InFOV(), InFOV3(), NPC_BSJump(), NPC_BSPointShoot(), NPC_GetHFOVPercentage(), NPC_GetVFOVPercentage(), NPC_UpdateAngles(), NPC_UpdateFiringAngles(), NPC_UpdateShootAngles(), Q3_Lerp2Pos(), R2D2_TurnAnims(), and WP_SaberPositionUpdate().

00738                                                 {
00739         return AngleNormalize180( angle1 - angle2 );
00740 }

float AngleMod float  a  ) 
 

Definition at line 697 of file q_math.c.

Referenced by BG_G2PlayerAngles(), BotAI(), BotChangeViewAngle(), BotChangeViewAngles(), BotUpdateInput(), and InFieldOfVision().

00697                           {
00698         a = (360.0/65536) * ((int)(a*(65536/360.0)) & 65535);
00699         return a;
00700 }

float AngleNormalize180 float  angle  ) 
 

Definition at line 722 of file q_math.c.

References AngleNormalize360().

Referenced by AngleDelta(), BG_G2PlayerAngles(), BG_TouchJumpPad(), BG_UpdateLookAngles(), G_EstimateCamPos(), PM_UpdateViewAngles(), PM_VehFaceHyperspacePoint(), PM_VehForcedTurning(), PM_VehicleImpact(), ProcessOrientCommands(), and VEH_TurretAim().

00722                                         {
00723         angle = AngleNormalize360( angle );
00724         if ( angle > 180.0 ) {
00725                 angle -= 360.0;
00726         }
00727         return angle;
00728 }

float AngleNormalize360 float  angle  ) 
 

Definition at line 710 of file q_math.c.

Referenced by AngleNormalize180(), Droid_Patrol(), Droid_Spin(), ImperialProbe_Wait(), Interrogator_PartsMove(), NAV_Bypass(), NAVNEW_SidestepBlocker(), NPC_BSGM_Attack(), NPC_BSJump(), NPC_BSPointShoot(), NPC_BSSearch(), NPC_BSWander(), NPC_FacePosition(), NPC_GetMoveDirection(), NPC_GetMoveDirectionAltRoute(), NPC_MoveToGoal(), pas_think(), PM_VehFaceHyperspacePoint(), R2D2_PartsMove(), and Sniper_FaceEnemy().

00710                                         {
00711         return (360.0 / 65536) * ((int)(angle * (65536 / 360.0)) & 65535);
00712 }

void AnglesSubtract vec3_t  v1,
vec3_t  v2,
vec3_t  v3
 

Definition at line 690 of file q_math.c.

References AngleSubtract(), and vec3_t.

Referenced by BG_G2PlayerAngles(), CG_CheckPassengerTurretView(), PM_VehicleImpact(), and VEH_TurretAim().

00690                                                        {
00691         v3[0] = AngleSubtract( v1[0], v2[0] );
00692         v3[1] = AngleSubtract( v1[1], v2[1] );
00693         v3[2] = AngleSubtract( v1[2], v2[2] );
00694 }

void AnglesToAxis const vec3_t  angles,
vec3_t  axis[3]
 

Definition at line 530 of file q_math.c.

References AngleVectors(), vec3_origin, vec3_t, and VectorSubtract.

Referenced by BG_G2PlayerAngles(), BG_VehicleAdjustBBoxForOrientation(), CG_AddFragment(), CG_AddPacketEntities(), CG_AddPlayerWeapon(), CG_AddViewWeapon(), CG_CreateDistortionTrailPart(), CG_CreateModelFromSpawnEnt(), CG_Draw3DModel(), CG_DrawActiveFrame(), CG_DrawAutoMap(), CG_DrawPlayerShield(), CG_DrawPlayerSphere(), CG_EmplacedView(), CG_MiscEnt(), CG_Player(), CG_ScorePlum(), CG_TestModel_f(), G_EstimateCamPos(), Item_Model_Paint(), and WP_SaberPositionUpdate().

00530                                                          {
00531         vec3_t  right;
00532 
00533         // angle vectors returns "right" instead of "y axis"
00534         AngleVectors( angles, axis[0], right, axis[2] );
00535         VectorSubtract( vec3_origin, right, axis[1] );
00536 }

float AngleSubtract float  a1,
float  a2
 

Definition at line 675 of file q_math.c.

References fmod().

Referenced by AnglesSubtract(), BG_EmplacedView(), BG_G2PlayerAngles(), CanCounterThrow(), CG_PredictPlayerState(), EWebPositionUser(), EWebUpdateBoneAngles(), pas_think(), PM_VehFaceHyperspacePoint(), PM_VehForcedTurning(), ProcessOrientCommands(), and Q3_Lerp2Angles().

00675                                             {
00676         float   a;
00677 
00678         a = a1 - a2;
00679         a=fmod(a,360);//chop it down quickly, then level it out
00680         while ( a > 180 ) {
00681                 a -= 360;
00682         }
00683         while ( a < -180 ) {
00684                 a += 360;
00685         }
00686         return a;
00687 }

void AngleVectors const vec3_t  angles,
vec3_t  forward,
vec3_t  right,
vec3_t  up
 

Definition at line 1315 of file q_math.c.

References cos(), M_PI, PITCH, ROLL, sin(), vec3_t, and YAW.

Referenced by AnglesToAxis(), AnimateRiders(), Boba_FireDecide(), Boba_StopKnockdown(), BotFallbackNavigation(), BotInputToUserCommand(), BotSurfaceNear(), BotTrace_Duck(), BotTrace_Jump(), BotTrace_Strafe(), CalcEntitySpot(), CG_AddParticles(), CG_AddParticleToScene(), CG_CalcMuzzlePoint(), CG_CalcVehicleMuzzlePoint(), CG_DamageFeedback(), CG_Disintegration(), CG_DrawAutoMap(), CG_DrawRadar(), CG_EntityEvent(), CG_ParticleBloodCloud(), CG_ParticleDust(), CG_Player(), CG_ROFF_NotetrackCallback(), CG_WorldCoordToScreenCoordFloat(), CheckForFriendInLOF(), ClientCommand(), ClientThink_real(), Cmd_EngageDuel_f(), DeathFX(), DoGripAction(), Drop_Item(), emplaced_gun_use(), EWeb_Create(), ExplodeDeath(), finish_spawning_turretG2(), FireVehicleWeapon(), FireWeapon(), ForceGrip(), ForceShootDrain(), ForceShootLightning(), ForceTelepathy(), ForceTelepathyCheckDirectNPCTarget(), ForceThrow(), G_AddPushVecToUcmd(), G_AttackDelay(), G_CanJumpToEnemyVeh(), G_CreateRotationMatrix(), G_Damage(), G_DeflectMissile(), G_EstimateCamPos(), G_GetDismemberLoc(), G_GetHitLocation(), G_GetHitLocFromSurfName(), G_GetHitQuad(), G_ItemUsable(), G_KnockOffVehicle(), G_MissileImpact(), G_SetMovedir(), G_SPSaberDamageTraceLerped(), G_UcmdMoveForDir(), Howler_TryDamage(), hyperspace_touch(), ImperialProbe_FireBlaster(), ImperialProbe_Strafe(), InFront(), Interrogator_Strafe(), ItemUse_Sentry(), ItemUse_UseDisp(), Jedi_CheckFlipEvasions(), Jedi_FindEnemyInCone(), Jedi_SaberBlockGo(), maglock_link(), Mark1_FireBlaster(), Mark1_FireRocket(), Mark2_FireBlaster(), MeleeCombatHandling(), MineMonster_TryDamage(), NAV_Bypass(), NAV_MoveBlocker(), NAV_Steer(), NAVNEW_Bypass(), NAVNEW_SidestepBlocker(), NPC_BSAdvanceFight(), NPC_BSNoClip(), NPC_BSSniper_Attack(), NPC_BSST_Attack(), NPC_CheckCanAttack(), NPC_Droid_Pain(), NPC_Jedi_Pain(), NPC_MoveDirClear(), NPC_PickAlly(), NPC_ShotEntity(), NPC_SpawnType(), Pickup_Powerup(), pitch_roll_for_slope(), PlaceShield(), player_die(), PM_AdjustAngleForWallJump(), PM_AdjustAngleForWallRun(), PM_AdjustAngleForWallRunUp(), PM_AnglesForSlope(), PM_CanBackstab(), PM_CheckStabDown(), PM_HoverTrace(), PM_ItemUsable(), PM_pitch_roll_for_slope(), PM_RocketLock(), PM_SaberAttackForMovement(), PM_SaberFlipOverAttackMove(), PM_SaberJumpAttackMove(), PM_SaberLungeAttackMove(), PM_SomeoneInFront(), PM_VehicleImpact(), PM_WeaponLightsaber(), PmoveSingle(), Rancor_Attack(), Rancor_Swing(), Remote_Fire(), Remote_Strafe(), SaberCombatHandling(), saberFirstThrown(), saberKnockDown(), Seeker_Strafe(), Sentry_Fire(), Sentry_Strafe(), Sniper_FaceEnemy(), SP_misc_weapon_shooter(), StrafeTracing(), TeleportPlayer(), Touch_Multi(), TryUse(), turret_head_think(), Update(), UpdateClientRenderinfo(), ValidateBoard(), ValidBloodPool(), VEH_StartStrafeRam(), VEH_TryEject(), Wampa_Attack(), Wampa_Slash(), waypoint_testDirection(), WP_DropDetPack(), WP_DropThermal(), WP_GetVelocityForForceJump(), WP_SaberBlock(), WP_SaberBlockNonRandom(), WP_SaberPositionUpdate(), WP_SaberStartMissileBlockCheck(), and WP_VehCheckTraceFromCamPos().

01315                                                                                  {
01316         float           angle;
01317         static float            sr, sp, sy, cr, cp, cy;
01318         // static to help MS compiler fp bugs
01319 
01320         angle = angles[YAW] * (M_PI*2 / 360);
01321         sy = sin(angle);
01322         cy = cos(angle);
01323         angle = angles[PITCH] * (M_PI*2 / 360);
01324         sp = sin(angle);
01325         cp = cos(angle);
01326         angle = angles[ROLL] * (M_PI*2 / 360);
01327         sr = sin(angle);
01328         cr = cos(angle);
01329 
01330         if (forward)
01331         {
01332                 forward[0] = cp*cy;
01333                 forward[1] = cp*sy;
01334                 forward[2] = -sp;
01335         }
01336         if (right)
01337         {
01338                 right[0] = (-1*sr*sp*cy+-1*cr*-sy);
01339                 right[1] = (-1*sr*sp*sy+-1*cr*cy);
01340                 right[2] = -1*sr*cp;
01341         }
01342         if (up)
01343         {
01344                 up[0] = (cr*sp*cy+-sr*-sy);
01345                 up[1] = (cr*sp*sy+-sr*cy);
01346                 up[2] = cr*cp;
01347         }
01348 }

void AxisClear vec3_t  axis[3]  ) 
 

Definition at line 538 of file q_math.c.

References vec3_t.

Referenced by CG_Beam(), CG_Draw3DModel(), CG_MakeExplosion(), FX_AddOrientedLine(), and Item_Model_Paint().

00538                                  {
00539         axis[0][0] = 1;
00540         axis[0][1] = 0;
00541         axis[0][2] = 0;
00542         axis[1][0] = 0;
00543         axis[1][1] = 1;
00544         axis[1][2] = 0;
00545         axis[2][0] = 0;
00546         axis[2][1] = 0;
00547         axis[2][2] = 1;
00548 }

void AxisCopy vec3_t  in[3],
vec3_t  out[3]
 

Definition at line 550 of file q_math.c.

References vec3_t, and VectorCopy.

Referenced by CG_Chunks(), CG_LaunchGib(), and CG_ThrowChunk().

00550                                              {
00551         VectorCopy( in[0], out[0] );
00552         VectorCopy( in[1], out[1] );
00553         VectorCopy( in[2], out[2] );
00554 }

int BoxOnPlaneSide vec3_t  emins,
vec3_t  emaxs,
struct cplane_s p
 

Definition at line 809 of file q_math.c.

References cplane_s::dist, cplane_s::normal, cplane_s::signbits, cplane_s::type, and vec3_t.

00810 {
00811         float   dist1, dist2;
00812         int             sides;
00813 
00814 // fast axial cases
00815         if (p->type < 3)
00816         {
00817                 if (p->dist <= emins[p->type])
00818                         return 1;
00819                 if (p->dist >= emaxs[p->type])
00820                         return 2;
00821                 return 3;
00822         }
00823 
00824 // general case
00825         switch (p->signbits)
00826         {
00827         case 0:
00828                 dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
00829                 dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
00830                 break;
00831         case 1:
00832                 dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
00833                 dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
00834                 break;
00835         case 2:
00836                 dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
00837                 dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
00838                 break;
00839         case 3:
00840                 dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
00841                 dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
00842                 break;
00843         case 4:
00844                 dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
00845                 dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
00846                 break;
00847         case 5:
00848                 dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
00849                 dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
00850                 break;
00851         case 6:
00852                 dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
00853                 dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
00854                 break;
00855         case 7:
00856                 dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
00857                 dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
00858                 break;
00859         default:
00860                 dist1 = dist2 = 0;              // shut up compiler
00861                 break;
00862         }
00863 
00864         sides = 0;
00865         if (dist1 >= p->dist)
00866                 sides = 1;
00867         if (dist2 < p->dist)
00868                 sides |= 2;
00869 
00870         return sides;
00871 }

void ByteToDir int  b,
vec3_t  dir
 

Definition at line 324 of file q_math.c.

References bytedirs, NUMVERTEXNORMALS, vec3_origin, vec3_t, and VectorCopy.

Referenced by CG_EntityEvent(), and CG_VehicleWeaponImpact().

00324                                     {
00325         if ( b < 0 || b >= NUMVERTEXNORMALS ) {
00326                 VectorCopy( vec3_origin, dir );
00327                 return;
00328         }
00329         VectorCopy (bytedirs[b], dir);
00330 }

signed char ClampChar int  i  ) 
 

Definition at line 279 of file q_math.c.

00279                                {
00280         if ( i < -128 ) {
00281                 return -128;
00282         }
00283         if ( i > 127 ) {
00284                 return 127;
00285         }
00286         return i;
00287 }

signed short ClampShort int  i  ) 
 

Definition at line 289 of file q_math.c.

00289                                  {
00290         if ( i < -32768 ) {
00291                 return -32768;
00292         }
00293         if ( i > 0x7fff ) {
00294                 return 0x7fff;
00295         }
00296         return i;
00297 }

void ClearBounds vec3_t  mins,
vec3_t  maxs
 

Definition at line 1129 of file q_math.c.

References vec3_t.

01129                                              {
01130         mins[0] = mins[1] = mins[2] = 99999;
01131         maxs[0] = maxs[1] = maxs[2] = -99999;
01132 }

unsigned ColorBytes3 float  r,
float  g,
float  b
 

Definition at line 333 of file q_math.c.

References byte.

00333                                                  {
00334         unsigned        i;
00335 
00336         ( (byte *)&i )[0] = r * 255;
00337         ( (byte *)&i )[1] = g * 255;
00338         ( (byte *)&i )[2] = b * 255;
00339 
00340         return i;
00341 }

unsigned ColorBytes4 float  r,
float  g,
float  b,
float  a
 

Definition at line 343 of file q_math.c.

References byte.

00343                                                           {
00344         unsigned        i;
00345 
00346         ( (byte *)&i )[0] = r * 255;
00347         ( (byte *)&i )[1] = g * 255;
00348         ( (byte *)&i )[2] = b * 255;
00349         ( (byte *)&i )[3] = a * 255;
00350 
00351         return i;
00352 }

int DirToByte vec3_t  dir  ) 
 

Definition at line 301 of file q_math.c.

References bytedirs, DotProduct, NUMVERTEXNORMALS, and vec3_t.

Referenced by ForceDrainDamage(), G_Damage(), G_ExplodeMissile(), G_MissileImpact(), locateCamera(), thermalDetonatorExplode(), and WP_DisruptorAltFire().

00301                             {
00302         int             i, best;
00303         float   d, bestd;
00304 
00305         if ( !dir ) {
00306                 return 0;
00307         }
00308 
00309         bestd = 0;
00310         best = 0;
00311         for (i=0 ; i<NUMVERTEXNORMALS ; i++)
00312         {
00313                 d = DotProduct (dir, bytedirs[i]);
00314                 if (d > bestd)
00315                 {
00316                         bestd = d;
00317                         best = i;
00318                 }
00319         }
00320 
00321         return best;
00322 }

vec_t DistanceHorizontal const vec3_t  p1,
const vec3_t  p2
 

Definition at line 1134 of file q_math.c.

References sqrt(), vec3_t, vec_t, and VectorSubtract.

01134                                                              {
01135         vec3_t  v;
01136 
01137         VectorSubtract( p2, p1, v );
01138         return sqrt( v[0]*v[0] + v[1]*v[1] );   //Leave off the z component
01139 }

vec_t DistanceHorizontalSquared const vec3_t  p1,
const vec3_t  p2
 

Definition at line 1141 of file q_math.c.

References vec3_t, vec_t, and VectorSubtract.

Referenced by ATST_Attack(), Howler_Combat(), ImperialProbe_AttackDecision(), Interrogator_Attack(), Jedi_CheckAmbushPlayer(), Mark1_AttackDecision(), Mark2_AttackDecision(), MineMonster_Combat(), NPC_BSGM_Attack(), NPC_BSGrenadier_Attack(), NPC_FindCombatPoint(), Remote_Attack(), Seeker_Attack(), Seeker_FindEnemy(), Seeker_FollowOwner(), and Sentry_AttackDecision().

01141                                                                     {
01142         vec3_t  v;
01143 
01144         VectorSubtract( p2, p1, v );
01145         return v[0]*v[0] + v[1]*v[1];   //Leave off the z component
01146 }

float DotProductNormalize const vec3_t  inVec1,
const vec3_t  inVec2
 

Definition at line 1508 of file q_math.c.

References DotProduct, vec3_t, and VectorNormalize2().

Referenced by G_FindClosestPointOnLineSegment(), and G_PointDistFromLineSegment().

01509 {
01510         vec3_t  v1, v2;
01511 
01512         VectorNormalize2( inVec1, v1 );
01513         VectorNormalize2( inVec2, v2 );
01514 
01515         return DotProduct(v1, v2);
01516 }

float flrand float  min,
float  max
 

Definition at line 1441 of file q_math.c.

References min.

Referenced by asteroid_field_think(), CG_AddGhoul2Mark(), CG_Chunks(), CG_FireWeapon(), CG_G2MarkEvent(), CG_G2PlayerHeadAnims(), CG_RagDoll(), CG_SaberCompWork(), Do_Strike(), G_RunObject(), misc_faller_create(), NPC_AimWiggle(), NPC_BSFlee(), NPC_BSGM_Attack(), NPC_BSSearch(), NPC_BSWander(), NPC_CheckAttack(), NPC_FacePosition(), NPC_UpdateFiringAngles(), Q_flrand(), Rancor_Swing(), Remote_Attack(), Seeker_MaintainHeight(), Sniper_FaceEnemy(), Sniper_UpdateEnemyPos(), and Wampa_Slash().

01442 {
01443         float   result;
01444 
01445         holdrand = (holdrand * 214013L) + 2531011L;
01446         result = (float)(holdrand >> 17);                                               // 0 - 32767 range
01447         result = ((result * (max - min)) / 32768.0F) + min;
01448 
01449         return(result);
01450 }

qboolean G_FindClosestPointOnLineSegment const vec3_t  start,
const vec3_t  end,
const vec3_t  from,
vec3_t  result
 

Definition at line 1524 of file q_math.c.

References cos(), DEG2RAD, DotProductNormalize(), qboolean, qfalse, qtrue, vec3_t, VectorCopy, VectorMA, VectorNormalize(), and VectorSubtract.

Referenced by NAV_MoveToGoal(), and ShortestLineSegBewteen2LineSegs().

01525 {
01526         vec3_t  vecStart2From, vecStart2End, vecEnd2Start, vecEnd2From;
01527         float   distEnd2From, distEnd2Result, theta, cos_theta, dot;
01528 
01529         //Find the perpendicular vector to vec from start to end
01530         VectorSubtract( from, start, vecStart2From);
01531         VectorSubtract( end, start, vecStart2End);
01532 
01533         dot = DotProductNormalize( vecStart2From, vecStart2End );
01534 
01535         if ( dot <= 0 )
01536         {
01537                 //The perpendicular would be beyond or through the start point
01538                 VectorCopy( start, result );
01539                 return qfalse;
01540         }
01541 
01542         if ( dot == 1 )
01543         {
01544                 //parallel, closer of 2 points will be the target
01545                 if( (VectorLengthSquared( vecStart2From )) < (VectorLengthSquared( vecStart2End )) )
01546                 {
01547                         VectorCopy( from, result );
01548                 }
01549                 else
01550                 {
01551                         VectorCopy( end, result );
01552                 }
01553                 return qfalse;
01554         }
01555 
01556         //Try other end
01557         VectorSubtract( from, end, vecEnd2From);
01558         VectorSubtract( start, end, vecEnd2Start);
01559 
01560         dot = DotProductNormalize( vecEnd2From, vecEnd2Start );
01561 
01562         if ( dot <= 0 )
01563         {//The perpendicular would be beyond or through the start point
01564                 VectorCopy( end, result );
01565                 return qfalse;
01566         }
01567 
01568         if ( dot == 1 )
01569         {//parallel, closer of 2 points will be the target
01570                 if( (VectorLengthSquared( vecEnd2From )) < (VectorLengthSquared( vecEnd2Start )))
01571                 {
01572                         VectorCopy( from, result );
01573                 }
01574                 else
01575                 {
01576                         VectorCopy( end, result );
01577                 }
01578                 return qfalse;
01579         }
01580 
01581         //                    /|
01582         //                c  / |
01583         //                  /  |a
01584         //      theta  /)__|    
01585         //                    b
01586         //cos(theta) = b / c
01587         //solve for b
01588         //b = cos(theta) * c
01589 
01590         //angle between vecs end2from and end2start, should be between 0 and 90
01591         theta = 90 * (1 - dot);//theta
01592         
01593         //Get length of side from End2Result using sine of theta
01594         distEnd2From = VectorLength( vecEnd2From );//c
01595         cos_theta = cos(DEG2RAD(theta));//cos(theta)
01596         distEnd2Result = cos_theta * distEnd2From;//b
01597 
01598         //Extrapolate to find result
01599         VectorNormalize( vecEnd2Start );
01600         VectorMA( end, distEnd2Result, vecEnd2Start, result );
01601         
01602         //perpendicular intersection is between the 2 endpoints
01603         return qtrue;
01604 }

float G_PointDistFromLineSegment const vec3_t  start,
const vec3_t  end,
const vec3_t  from
 

Definition at line 1606 of file q_math.c.

References cos(), DEG2RAD, DotProductNormalize(), vec3_t, VectorMA, VectorNormalize(), and VectorSubtract.

01607 {
01608         vec3_t  vecStart2From, vecStart2End, vecEnd2Start, vecEnd2From, intersection;
01609         float   distEnd2From, distStart2From, distEnd2Result, theta, cos_theta, dot;
01610 
01611         //Find the perpendicular vector to vec from start to end
01612         VectorSubtract( from, start, vecStart2From);
01613         VectorSubtract( end, start, vecStart2End);
01614         VectorSubtract( from, end, vecEnd2From);
01615         VectorSubtract( start, end, vecEnd2Start);
01616 
01617         dot = DotProductNormalize( vecStart2From, vecStart2End );
01618 
01619         distStart2From = Distance( start, from );
01620         distEnd2From = Distance( end, from );
01621 
01622         if ( dot <= 0 )
01623         {
01624                 //The perpendicular would be beyond or through the start point
01625                 return distStart2From;
01626         }
01627 
01628         if ( dot == 1 )
01629         {
01630                 //parallel, closer of 2 points will be the target
01631                 return ((distStart2From<distEnd2From)?distStart2From:distEnd2From);
01632         }
01633 
01634         //Try other end
01635 
01636         dot = DotProductNormalize( vecEnd2From, vecEnd2Start );
01637 
01638         if ( dot <= 0 )
01639         {//The perpendicular would be beyond or through the end point
01640                 return distEnd2From;
01641         }
01642 
01643         if ( dot == 1 )
01644         {//parallel, closer of 2 points will be the target
01645                 return ((distStart2From<distEnd2From)?distStart2From:distEnd2From);
01646         }
01647 
01648         //                    /|
01649         //                c  / |
01650         //                  /  |a
01651         //      theta  /)__|    
01652         //                    b
01653         //cos(theta) = b / c
01654         //solve for b
01655         //b = cos(theta) * c
01656 
01657         //angle between vecs end2from and end2start, should be between 0 and 90
01658         theta = 90 * (1 - dot);//theta
01659         
01660         //Get length of side from End2Result using sine of theta
01661         cos_theta = cos(DEG2RAD(theta));//cos(theta)
01662         distEnd2Result = cos_theta * distEnd2From;//b
01663 
01664         //Extrapolate to find result
01665         VectorNormalize( vecEnd2Start );
01666         VectorMA( end, distEnd2Result, vecEnd2Start, intersection );
01667         
01668         //perpendicular intersection is between the 2 endpoints, return dist to it from from
01669         return Distance( intersection, from );
01670 }

int irand int  min,
int  max
 

Definition at line 1458 of file q_math.c.

References min.

Referenced by Q_irand().

01459 {
01460         int             result;
01461 
01462         assert((max - min) < 32768);
01463 
01464         max++;
01465         holdrand = (holdrand * 214013L) + 2531011L;
01466         result = holdrand >> 17;
01467         result = ((result * (max - min)) >> 15) + min;
01468         return(result);
01469 }

float LerpAngle float  from,
float  to,
float  frac
 

Definition at line 653 of file q_math.c.

Referenced by G_SPSaberDamageTraceLerped().

00653                                                    {
00654         float   a;
00655 
00656         if ( to - from > 180 ) {
00657                 to -= 360;
00658         }
00659         if ( to - from < -180 ) {
00660                 to += 360;
00661         }
00662         a = from + frac * (to - from);
00663 
00664         return a;
00665 }

void MakeNormalVectors const vec3_t  forward,
vec3_t  right,
vec3_t  up
 

Definition at line 587 of file q_math.c.

References DotProduct, vec3_t, VectorMA, and VectorNormalize().

00587                                                                        {
00588         float           d;
00589 
00590         // this rotate and negate guarantees a vector
00591         // not colinear with the original
00592         right[1] = -forward[0];
00593         right[2] = forward[1];
00594         right[0] = forward[2];
00595 
00596         d = DotProduct (right, forward);
00597         VectorMA (right, -d, forward, right);
00598         VectorNormalize (right);
00599         CrossProduct (right, forward, up);
00600 }

void MatrixMultiply float  in1[3][3],
float  in2[3][3],
float  out[3][3]
 

Definition at line 1293 of file q_math.c.

Referenced by CG_GetTagWorldPosition(), CG_PositionEntityOnTag(), CG_PositionRotatedEntityOnTag(), and RotatePointAroundVector().

01293                                                                        {
01294         out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
01295                                 in1[0][2] * in2[2][0];
01296         out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
01297                                 in1[0][2] * in2[2][1];
01298         out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
01299                                 in1[0][2] * in2[2][2];
01300         out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
01301                                 in1[1][2] * in2[2][0];
01302         out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
01303                                 in1[1][2] * in2[2][1];
01304         out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
01305                                 in1[1][2] * in2[2][2];
01306         out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
01307                                 in1[2][2] * in2[2][0];
01308         out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
01309                                 in1[2][2] * in2[2][1];
01310         out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
01311                                 in1[2][2] * in2[2][2];
01312 }

float NormalizeColor const vec3_t  in,