Physics Entities on Server & Client

From Valve Developer Community
Jump to: navigation, search


A major feature of the Source Engine is the physical simulation of rigid bodies. This simulation implements mostly mechanical and Newtonian physics like gravity, trajectory, friction, collisions, springs and damping. Models have to support this simulation by providing information about their collision model, material type, weight etc. In single player mode all physics entities are controlled and simulated by the server (server-side physics) and networked to the client. In multiplayer mode smaller objects like cans or bottles that don't affect gameplay are completely simulated client-side and are therefore not synchronized between clients. This is necessary because moving physics entities generate significant network traffic since they can change their position and orientation with every frame. Networking these updates would almost stall any connection as soon as lots of physics object start to move at the same time (explosions, etc). Client-side physics objects don't affect player movement, and they should always be significantly smaller than players so that a player can not hide behind the objects. When destroying server-side breakable objects, they break apart into smaller client-side simulated fragments.

Adding physics entities

For a mapmaker it's pretty easy to place physics entities in Hammer. For single player maps the entity class physics_prop must be used to create server-side controlled physics entities. The player does correctly collide with these entities, can walk over them, push them around or pick them up. Unfortunately this class can't be used in multiplayer mode since player interaction with physics entities isn't predicted on the client and would cause jittery or delayed movement. Therefore a special entity class prop_physics_multiplayer must be used, which implements a simpler collision behavior (COLLISION_GROUP_PUSHAWAY). Multiplayer physics entities can only be pushed away, but not be used to walk over or picked up. If they are larger, a player just bounces off. Whether a multiplayer physics entity becomes a server-side or a client-side simulated entity, is defined by the object model.

The physical properties of a model are defined in its .QC file by 3 entries: $surfaceprop, $collisionmodel, $keyvalues. The first entry $surfaceprop sets the model surface properties as defined in text file \scripts\surfaceproperties.txt. There properties like friction, elasticity and collision sounds are defined. The next entry $collisionmodel sets the collision model and the objects weight. The see collision models for entities in game enable console variable "vcollide_wireframe 1". The last section are the prop_data entries under $keyvalues. Here the object properties like health, break models and physics mode are defined (full description of prop_data). Here is an example:

$surfaceprop cardboard // object surface properties

$collisionmodel "mymodel.smd" {
     $Mass 40	// Mass in kilograms

		base 		"Cardboard.Medium" // base material defined in propdata.txt
		health		"40" // overriding material properties
		physicsmode	"1" // setting a custom physics mode

The physics mode defines if the object becomes a server-side or client-side physics entity. There are to 2 server-side modes and one client-side mode define in props_shared.h:

#define PHYSICS_MULTIPLAYER_AUTODETECT	0	// autodetect mode based on mass & size
#define PHYSICS_MULTIPLAYER_SOLID	1	// server-side, solid (collides with player)
#define PHYSICS_MULTIPLAYER_NON_SOLID	2	// server-side, non-solid
#define PHYSICS_MULTIPLAYER_CLIENTSIDE	3	// client-side, non-solid

If the physics mode property is not set in propdata.txt neither in the QC file, the physics mode will be picked by the function GetAutoMultiplayerPhysicsMode() based on the model's size and weight. A model will become a client-side physics entity, if it's size is below a certain threshold (set by console variable sv_pushaway_clientside_size).

Collision Groups

For quite some reason, it's not necessary that all dynamic physic entity collide with each other. That may be for game play reasons, but most of the time it's a performance issue. Especially creating dozens of small, fast moving debris pieces would drag down client performance significantly. As for the visual effect of breaking debris or exploding particle-like objects it's sufficient enough when they collide with static world objects, but not with themselves or other dynamic objects. To implement specific collision behavior the Source engine allows to define collision groups and specify if elements of these groups should collide or not. Each entity belongs to a single group at a time set with SetCollisionGroup(). New groups and new rules can be added easily. The physics subsystem queries the virtual function bool CGameRules::ShouldCollide(int group0, int group1) to see if a collision check must be run for between two objects. Already existing collision groups are:

Default; collides with static and dynamic objects
Collides with nothing but world and static stuff
Same as debris, but hits triggers
Collides with everything except other interactive debris or debris
Collides with everything except interactive debris or debris
Collision group for player
Special group for glass debris
Collision group for driveable vehicles
For singleplayer, same as Collision_Group_Player, for multiplayer, this filters out other players and CBaseObjects
Generic NPC group
For any entity inside a vehicle
For any weapons that need collision detection
vehicle clip brush to restrict vehicle movement
Blocks entities not permitted to get near moving doors
Doors that the player shouldn't collide with
Things that are dissolving are in this group
Nonsolid on client and server, pushs player away


Valid $collisionmodel definitions

Manually set the mass of the model
Tell the physics system to compute a mass for the model, based on it's surfaceprops & volume
Inertia scale
Linear damping scale
Rotational damping scale
Scales the air resistance
The vphysics collision model is not a single convex hull. If not set, it'll make a single convex hull out of whatever geometry you give it.
Override the center of mass, in local coords
Rarely used. Eliminates a joint in the collision model that you don't want to use. (i.e. if you were using a render model as a ragdoll, and it has bones you don't want)
Merges the vertex assignments for two joints.
The parent-most bone that actually has collision geometry.
The limits of the joint's movement
Like $inertia, but per-bone
Like $damping, but per-bone
Like $rotdamping, but per-bone
Mass is automatically distributed by volume, this lets you bias it per-bone
Turns off all collisions between bones in this model, usually for perf.
If any $jointcollide pairs are specified, only those joints collide with each other.
Used to animate the amount of friction on joints over time.

Valid propdata.txt definitions

Specify a base propdata class to derive from (base types can be found in propdata.txt)
Override whether this prop should block NPC's Line-Of-Sight.
Override whether AI should consider this prop as walkable on.
Mod damage done by bullets to this prop.
Mod damage done by clubs to this prop.
Mod damage done by explosives to this prop.
Amount of damage this prop should take before breaking.
Explosive damage done by this prop.
Radius of the explosion caused by this prop when it breaks.
The type of breakable gibs this prop should break into
The number of breakable gibs to break into.
Allow this prop to be static as well as physically simulated.
Set multiplayer physics behavior (1=full, 2=non-solid,3=clientside)