# Vphysics & Qphysics

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

## Mechanics of Motion

Source uses four methods to simulate the effects Mechanical Forces on the Motion of World Objects in game:

1. Particle-simulated objects : Source Particle Motion appears to be scripted? See info_particle_system. Particles are never Solid.
2. Ray-simulated objects : very small objects travelling at infinite speed are simulated as simple but accurate, straight-line ray-traces. They are not treated as Solid, until they hit a Solid Object.
• LOS tests are raytraced until blocked by an opaque surface (eg normal Solid surface is Solid to LOS). Range is also restricted to 2048 units (or more - see NPC_Sensing) and affected by env_fog_controller limited visibility.
• Bullets are also Ray-traced. Some surfaces (eg Grates) are NotSolid to Bullets. Their Range is determined by ... ?
3. Hull-simulated objects : have simple, Solid Collision boxes which are moved by a controlling intelligence (or script), and only restricted by simple QPhys collisions and QPhys gravity.
4. VPhysics-simulated objects are either Rigid or Deformable :
• VPhys Rigid Body : position (\$origin) affected by VPhys Translational Motion plus orientation (\$angles) affected by VPhys Rotational Motion (which requires a Collision Model)
• VPhys \$concave Rigid Body : = unbreakable (rigid) constraints holding physboxes (bones?) together?
• VPhys Deformable Body is a constrained system of Rigid Bodies:
• VPhys Ragdoll object made of bones?: joints = (flexible) contraints allowing limited articulation by external forces.
• VPhys(?) Breakable object made of gibs: health = (rigid) constraints holding gibs together, damage = external force.

To do: what is the correct term for a single piece of convex geometry within a concave or jointed collision model? a PhysBox? a PhysBone? ... some \$bones in a Ragdoll may be NotSolid to Collision.

### Basic Concepts

• Linear Motion is a change in the Location of the entity's origin in the World.
• Push attempts to increase the distance between the objects: eg Collisions, Gravgun Attack1, Explosion, Wind, etc.
• Pull attempts to decrease the distance between the objects: eg Constraints, Gravgun Attack2, Gravity, etc.
• Angular Motion is a change in the Orientation (\$angles) of the object in the World.

• Mass of an object is its Inertial Mass ie its resistance to acceleration.
Mass appears to have negligible affect on Torque, but Center of Mass is very important.
Game constant : Water = 1kg/liter = 28.3 kg/cubic foot (= 6.9 gram / map unit³?)
• Static objects cannot be moved by VPhys or QPhys forces. In effect, a static object has infinite inertial mass. This makes them very cheap in terms of VPhysics calculations.
• Solidity : The Solid CollisionProperty generally means this object will attempt to Block Motion of other Solid objects. But there's also Block Bullets and Block LOS Collision Properties.
• Solidity is a boolean, either Solid or NotSolid. The Viscosity of Water, etc are handled separately. (see below).
• A Solid object is basically anything which has Mass.
• Physicsmode:
1 : Solid, pushes the player away.
2 : Non-solid, but gets pushed away by the player.
3 : Non-solid, clientside simulated only.
• Volume : CollisionModel ...

• Velocity
Velocity is a vector; it has both magnitude (speed) and direction. `Velocity = Time * Acceleration / Mass`.
Maximum Velocity of an object is achieved when the forces of acceleration and deceleration are balanced. Deceleration is usually caused by Friction, eg Air Resistance, Surface Friction.
• Gravity accelerates objects at 32 ft/sec² (= 21.9 mph/sec)
• Game variable (Gravity = 1 by default) seems to be applied equally to both VPhys and QPhys calculations ...
• To do:  does changing gravity affect trajectory of crossbow bolt, etc ?
• Air Resistance or Aerodynamic Friction decelerates objects moving through Air.
• Hull-objects use a simple "max speed" value for "flying", "running", etc. specified per Entity Class.
• Freefalling Hull-objects use a worldconstant Terminal Velocity = 3500 units/sec (218.75 ft/sec) (ie reached after 6.8 sec of freefall).
• VPhys-objects use To do: ...?.
• Wind Forces ...
• Water Resistance or Hydrodynamic Friction decelerates objects moving through Water.
• Hull-objects use a simple "max speed" value for "surface swim" and "underwater swim" (if they don't just drown) specified per Entity Class.
• Sinking Hull-objects ... Player sinks at 48 units/sec (3 ft/sec) To do:  check that NPCs sink at same speed ?.
• VPhys-objects use To do: ... ?.
• Water is approx 50-100 times more viscous than air than air ... Newtonian Fluid?
• Current Forces ...
• Surface Friction between two parallel solid surfaces in contact = Perpendicular Pressure * Friction Coefficient:
• Perpendicular Pressure : eg Object's on a Horizontal surface, Weight*Friction = resistance to sliding Horizontal movement.
• Friction Coefficient is specific to each combination of surfaces; eg Rubber & Wood, Metal & Wood, Rubber & Metal, Rubber & Rubber, ... look-up table in Materials System ?
• Sliding on Slope : where F = Friction Coefficient, object will slip on rise:run of `1:1/F` or steeper. So if F = 1, slope = 1:1, if F = 0.5, slope = 1:2, etc
• Sliding on Tilt Angle : where F = Friction Coefficient, and A = Angle of Elevation: `F = tan A`. So F 1 = 45°, F 0.5 = 26.6°, F 2 = 63.4°(calc)

## Physics Consistency

Whilst the scientific field of Physics describes interactions between all physical phenomena (including the behaviour of light, sound, heat, etc), the Source Physics Engine(s) deal exclusively with Mechanical interactions between Solid objects in the World.

An important aspect of Source gameplay is that the physical interactions between World objects remains reasonably consistent and predictable. There would be no point in using a physical-mechanics simulation system that wasn't. (Actually, level designers frequently use physics as a "special effect" - so, given we are making games here [not scientific simulations], level designers should have more control over the implementation of physics in each given context.) For technical (legacy) and CPU performance optimisation reasons, not all World objects can be physically simulated to the same level of detail.

VPhysics is derived from the Havok Physics Engine, and determines the Linear and Angular Motion of VPhys Objects resulting from the interaction of various (simulated) VPhys Forces. It is a kind of Procedural Animation Engine designed to simulate Newtonian Mechanics. The Gravgun is a key part of the VPhys system, enhancing the Player's ability to influence VPhys Objects and events.

QPhysics (stands for "Quake Physics" - because I guess this is where it comes from) is not nearly as sophisticated as VPhys, but equally important as it is used in determining the Motion of all Hull-simulated NPCs and Players, several types of projectile and a number of Moving Brush Entities (eg func_door, etc).

The biggest difference is that VPhysics alone determines the motion and behaviour of Vphys Objects, whereas QPhysics merely imposes a few simple restrictions on objects that are primarily controlled by either Player Input, NPC AI, an I/O script, Scripted Animation Sequence, Entity Script, etc, etc.

To do:  Where do these "physics interactions" fit in ?

• Heat System : Flammable objects & fire spreading between them.
• Damage System : Breakable objects, Falling & Impact Damage calculations
• Gravgun Attack1 'knock-back' effect on Antlions, Manhacks, Scanners, etc

## Projectiles

• A VPhys object can be Pushed or Damaged by anything (solid) that hits it - Bullet, Hull, or other VPhys object.
• A Bullet (ray) is so small and fast that nothing can Push or Damage it.
• All explosions push and damage VPhys objects. Hulls may be damaged.
```Projectile 	Gravity	Deflection	Impact Damage 	Solid Model 	NPC Push
bullet 		no 	no 		skill.cfg 	Ray 		no?
grenade-SMG 	yes 	no (explode) 	no? 		Hull? 		no?
crossbow bolt 	yes 	no (impale) 	skill.cfg 	Hull? 		no
rocket 		no 	no (explode) 	no? 		Hull 		no?
NPC/Player 	yes 	no 		no? 		Hull 		yes
grenade-frag 	VPhys 	VPhys 		yes? 		VPhys 		yes?
grenade-AR2 	no ? 	VPhys 		yes? 		VPhys 		yes?
gravgun-prop 	VPhys 	VPhys 		yes? 		VPhys 		yes?
explosion 	no 	no 		no 		VPhys 		no
func_door 	no 	no? 		crush? 		Hull? 		yes?
```
• Gravity : Will Gravity affect its Trajectory (Linear Motion).
• Deflection : Will it bounce/ricochet off a solid object rather than simply stopping ? (Linear & Angular Motion)
• Impact Damage : Will it inflict damage on collision (excluding explosion damage defined in skill.cfg).
Presumambly derived from Projectile's Momentum (kinetic energy): calculated or look-up table ?
Constraint is a static pull force; it resists movement away. ?
Impact Damage Table suggests vphys object mass is classed ("tiny, small, medium and large") relative to "Player, Player in Vehicle, NPC or Glass". Damage clamps & multipliers are specified for some classes, but the basic calculation ... ?
Prop Data#Damage Modifiers for "bullet, club, explosive" are only applied to breakable-props.
why should glass use a damage_table plus prop_data dam-mods ?
• Solid Model : Physics interaction simulation system used.
• NPC Push : Some collisions seem to affect (stop/slow/deflect) an NPC's Hull Momentum? Isn't there something like this in DOD? Hull Motion seems to be entirely immune to external (VPhys) forces unless specifically scripted otherwise - eg Combine_Soldier melee attack: Push back player ?. What about Rocket Jumping in TF2?
• To do:  Are Collision Hulls really boxes? They seem to behave more like Cylinders in game. Eg A Player should not be able to walk diagonally (W+A) along a 34 unit passageway, as the diagonal dimension of his 32x32 Hull would be 45 units. For that matter, he would not be able to rotate (much) either. Cylinders don't have corners to catch on things and cause rotational deflection. The test would be an octagonal hole ...

## Hull QPhysics Interactions

• Hull Translation and Rotation Motion : controlled by PlayerInput/AI/IO/WeaponScript/etc ...
nb: dynamic light, turret & camera entities can track a moving target<target_destination>.
• Hull Gravity: eg Jump Trajectory. Alternative, separate Gravity system to VPhys ... ?
• Hull Linear Deflection : Static Solids cause Hulls to simply stop moving. Deflection/Ricochet/Knock-Back is not simulated.
• Hull Angular Deflection : vectors striking a hitbox but not passing through the CofMass do not induce rotation. Not simulated.
• Hull Friction : There is definitely some kind of Player Hull friction simulation going on in 33 unit passageways.
Mud material reduces Player movements through it's own mechanism, not VPhys.
• Hull Impact Damage : Fall Damage to Player/NPC. Impact Damage to breakable object he lands on.... Impact Damage Tables.
• Physics Shadow: "If Players don't have a physics shadow they can't move objects just by touching them; they have to press USE to apply a pushing force" (from Alternate Multiplayer Physics).
• compare physicsmode_choices per prop_phys_mp : "Solid, pushes player away" vs "Non-solid, but gets pushed away by the player".

## Collision Event

1. Collision Detection - if projectile touches target's Bounding Box :
• If projectile = bullet, test against target's Hitbox and then CollisionProperty
• If projectile = Hull or VPhys, test against target's CollisionModel ?
2. Calculate Angular Force of Impact on both objects; ie
• projectile's movement vector in relation to center of mass of target
• projectile's movement vector in relation to center of mass of projectile
3. Projectile Momentum absorbed by / transferred to:
1. damage to target
2. damage to projectile
3. angular deflection (spin force) on target (unless t = Hull)
4. angular deflection (spin force) on projectile (unless p = Hull)
5. linear deflection (push vector) on target
6. linear deflection (bounce vector) on projectile (unless p = Hull)

## Mechanical Properties & Interactions

• Qphys Collision Hull
Geometry & rigging = is bbox-defined in Entity Class (eg NPC Hull, Player Hull, projectile hulls?)
Player & NPC Hull always have "square" footprint. Player & some NPCs have multiple Hulls eg Player's DUCK_HULL and OBS_HULL.
Ground Hull: Origin = center of the base face; Flying Hull (centered hull) Origin = geometric center of box.
Qhull is not deflected by linear vphys forces. Solid obstacles simply block movement.
Qhull cannot rotate so not deflected by angular forces. Qhulls are always AABB to world axes - any Entity rotation is not applied to Qhull.
• Vphys Collision Model (=IPhysicsObject?)
Geometry & Rigging is vertex-defined in Studiomodel.phy (or func_physbox)
Vphys hull rotation induces rotation of EntityOrigin or BoneOrigin.
Vphys forces act on Collisionmodel (Vphys hulls), causing the Entity to move and/or rotate.
Collision Model total Volume is calculated for \$automass
Angular Momentum (ie spinning about Center of Mass) as opposed to Linear Momentum (ie linear velocity of Center of Mass)
By default: Center of mass/rotation is aligned to ModelOrigin and EntityOrigin = geometric center of bounding box.
• Rotation : Settling / Stability
Gravity applies as Torque about the CofMass until CollModel (Fulcrum) provides stable support.
Collision Model might be translated into a collection of tetrahedra/tripods: Triangles relative to \$CofMass?
so Rotational Stability = Triangular "foot" perpendicular to Center of Momentum vector.
• Impact Damage & Vphys push (to both colliding objects): -- does damage absorb momentum?
Flex - ie ragdoll model
Break - ie breakable model
Explode -

## Player & NPC Hulls

```Hull Type               Hull-1 vectors                  Hull-2 vectors                  Hull-1 dimensions    Hull-2 dimensions

Ground Nav NPC (from ai_hull.cpp)
*  Tiny_Hull		(-12,-12,  0),	(12, 12,  24),	(-12,-12,  0),	(12, 12,  24) 	= 24 x 24 x 24	 ==   24 x 24 x 24
*  Human_Hull		(-13,-13,  0),	(13, 13,  72),	( -8, -8,  0),	( 8,  8,  72) 	= 26 x 26 x 72	 &/   16 x 16 x 72
*  Wide_Human_Hull	(-15,-15,  0),	(15, 15,  72),	(-10,-10,  0),	(10, 10,  72)  	= 30 x 30 x 72	 &/   20 x 20 x 72
*  Medium_Hull		(-16,-16,  0),	(16, 16,  64),	( -8, -8,  0),	( 8,  8,  64) 	= 32 x 32 x 64	 &/   16 x 16 x 64
*  Medium_Tall_Hull	(-18,-18,  0),	(18, 18, 100),	(-12,-12,  0),	(12, 12, 100) 	= 36 x 36 x 100	 &/   24 x 24 x 100
*  Wide_Short_Hull	(-35,-35,  0),	(35, 35,  32),	(-20,-20,  0),	(20, 20,  32)   = 70 x 70 x 32	 &/   40 x 40 x 32
*  Large_Hull		(-40,-40,  0),	(40, 40, 100),	(-40,-40,  0),	(40, 40, 100) 	= 80 x 80 x 100	 ==   80 x 80 x 100

Air Nav NPC (from ai_hull.cpp)
*  Tiny_Centered_Hull	( -8, -8, -4),	( 8,  8,   4),	( -8, -8, -4),	( 8,  8,   4) 	= 16 x 16 x  8	 ==   16 x 16 x  8
*  Small_Centered_Hull	(-20,-20,-20),	(20, 20,  20),	(-12,-12,-12),	(12, 12,  12) 	= 40 x 40 x 40	 &/   24 x 24 x 24
*  Large_Centered_Hull	(-38,-38,-38),	(38, 38,  38),	(-30,-30,-30),	(30, 30,  30) 	= 76 x 76 x 76	 &/   60 x 60 x 60

Player (from gamerules.cpp)
* (normal)_HULL	(-16,-16,  0),	(16, 16,  72),	                         	= 32 x 32 x 72
* DUCK_HULL	        (-16,-16,  0),	(16, 16,  36),	                         	= 32 x 32 x 36
* OBS_HULL	        (-10,-10,-10),	(10, 10,  10),	                         	= 20 x 20 x 20

note: All Hulls have a square footprint with the origin in its centre.
note: Ground nav Hulls' \$origin is at the base of the vertical axis, whereas Air-nav (_Centered_Hull) \$origin is mid-vertical axis.
note: The mysterious NPC Hull-2 always fits inside Hull-1, and shares the same axes of symmetry as Hull-1.
```