VPhysics: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
Line 55: Line 55:
; <code>IPhysicsObject</code>
; <code>IPhysicsObject</code>
: The core interface into a VPhysics object. Provides functions for tweaking parameters and applying forces. An entity can have multiple <code>IPhysicsObject</code>s (e.g. ragdolls).
: The core interface into a VPhysics object. Provides functions for tweaking parameters and applying forces. An entity can have multiple <code>IPhysicsObject</code>s (e.g. ragdolls).
; <code>IPhysicsCollision</code>
: Provides various <code>CPhysCollide</code> and <code>CPhysConvex</code> (see below) utilities. Use the <code>physcollision</code> global instance.
; <code>CPhysCollide</code>
; <code>CPhysCollide</code>
: A single, convex [[collision mesh]] piece. Despite being named like a server entity, this class is defined in the engine.
: A rigid [[collision mesh]]. Despite being named like a server entity, this class is defined in the engine. It can be passed to various <code>IPhysicsCollision</code> functions.
; <code>vcollide_t</code>
; <code>vcollide_t</code>
: A collection of <code>CPhysCollide</code>s.
: A collection of <code>CPhysCollide</code>s.
; <code>CPhysConvex</code>
: A single, convex collision mesh element.
; <code>CPhysPolysoup</code>
; <code>CPhysPolysoup</code>
: A collection of raw polygons that represent a collision mesh. Call <code>ConvertPolysoupToCollide()</code> to "compile" it to a <code>CPhysCollide</code>.
: A collection of raw polygons that represent a collision mesh. Call <code>physcollision->ConvertPolysoupToCollide()</code> to "compile" it to a <code>CPhysCollide</code>.
; <code>solid_t</code>
; <code>solid_t</code>
: A collection of data passed to the engine when creating new physics objects.
: A collection of data passed to the engine when creating new physics objects.

Revision as of 06:14, 1 June 2011

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.

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.pngTip: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 the usual server/client positioning functions cease to have any effect, and the object's behaviour can only be influenced through the application of forces and constraints.

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 are two other initialisation functions:

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.pngNote:SetSolid() alone does not enable VPhysics collisions.

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