VPhysics

From Valve Developer Community
Jump to: navigation, search

VPhysics is Source's built-in 3D physics simulation engine. VPhysics objects move and collide believably thanks to the system's simulation of mass, gravity, friction, air resistance, inertia and buoyancy.

VPhysics was originally created by modifying the commercial Havok 1 and 2 physics engines. It also integrates some elements from Ipion Virtual Physics.

Collision models

Dr. Breen's jointed collision mesh.

VPhysics objects ("collision models") are invisible, because in order for the simulation to be efficient they must be extremely simple. The visible part of an entity will obviously be a close match to the shape of the VPhysics object, but it is entirely ignored by the simulator.

Collision models are typically loaded from a collision mesh embedded inside an entity's assigned model, but they can also be generated, from a bounding box for instance. Spherical collision models are also possible.

Tip:You can see collision meshes with the console command vcollide_wireframe 1.

QPhysics

VPhysics co-exists with QPhysics (a retroactive name - it's only ever referred to in the codebase as "game physics"), an older and far less sophisticated simulator that Source inherits from Quake. QPhysics is still used for players and walking NPCs because full VPhysics would lead to situations too complex for either AI or players to handle. Vehicles and flying NPCs do use VPhysics, however.

QPhysics entities are given a VPhysics "shadow" that allows them to interact with VPhysics entities in a limited way.

Programming

In programming terms, VPhysics is an engine library that takes direct control of the motion of an entity. When it is in full effect QPhysics positioning functions cease to have any effect, and the object's behaviour is instead influenced through the application of forces and constraints (though direct modification is still possible).

This spawn function creates a physically-simulated model:

#define MODEL "models/props_c17/FurnitureDresser001a.mdl"

void CMyEnt::Spawn()
{
	PrecacheModel(MODEL);
	SetModel(MODEL);

	VPhysicsInitNormal(SOLID_VPHYSICS,0,false);
}

There initialisation functions are:

VPhysicsInitNormal
Used above. Makes the entity a free VPhysics object with full simulation and collisions.
VPhysicsInitStatic()
An object that never moves.
VPhysicsInitShadow( bool allowPhysicsMovement, bool allowPhysicsRotation, solid_t *pSolid = 0 )
An object that collides with other VPhysics objects, but has its location defined by the QPhysics motion of its entity. This can lead to very strong forces being exerted on normal VPhysics objects, so use with care!

Unlike VPhysicsInitNormal(), neither of the functions above implicitly call SetSolid(). You will need to do so yourself as the default SOLID_NONE causes both to return early.

Note:SetSolid() alone does not enable VPhysics collisions.

Units

Measurement Unit
Distance Inches
Mass Kilogrammes
Volume Cubic inches
Velocity Inches per second
Acceleration Inches per second per second
Density Kilogrammes per cubic metre
Force Inches per second per kilogramme
Torque Degrees per second per kilogramme

See also Origin and QAngle.

Structures

IPhysicsEnvironment
A VPhysics environment within which objects are simulated. New IPhysicsObjects are created here, and global settings like gravity can be configured.
IPhysicsEnvironment* physenv is created by default. Multiple environments are entirely possible, which is how Portal works.
IPhysicsObject
The core interface into a VPhysics object. Provides functions for tweaking parameters and applying forces. An entity can have multiple IPhysicsObjects (e.g. ragdolls).
IPhysicsCollision
Provides various CPhysCollide and CPhysConvex (see below) utilities. Use the physcollision global instance.
CPhysCollide
A rigid collision mesh. Despite being named like a server entity, this class is defined in the engine. It can be passed to various IPhysicsCollision functions.
vcollide_t
A collection of CPhysCollides.
CPhysConvex
A single, convex collision mesh element.
CPhysPolysoup
A collection of raw polygons that represent a collision mesh. Call physcollision->ConvertPolysoupToCollide() to "compile" it to a CPhysCollide.
solid_t
A collection of data passed to the engine when creating new physics objects.

See also

Notable VPhysics entities