Physics Entities on Server & Client

From Valve Developer Community
Jump to navigation Jump to search
English (en)Русский (ru)Translate (Translate)

Overview

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
     $concave
}

$keyvalues
{
	prop_data
	{
		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:

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

Appendix

Valid $collisionmodel definitions

$mass
Manually set the mass of the model
$automass
Tell the physics system to compute a mass for the model, based on it's surfaceprops & volume
$inertia
Inertia scale
$damping
Linear damping scale
$rotdamping
Rotational damping scale
$drag
Scales the air resistance
$concave
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.
$masscenter
Override the center of mass, in local coords
$jointskip
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)
$jointmerge
Merges the vertex assignments for two joints.
$rootbone
The parent-most bone that actually has collision geometry.
$jointconstrain
The limits of the joint's movement
$jointinertia
Like $inertia, but per-bone
$jointdamping
Like $damping, but per-bone
$jointrotdamping
Like $rotdamping, but per-bone
$jointmassbias
Mass is automatically distributed by volume, this lets you bias it per-bone
$noselfcollisions
Turns off all collisions between bones in this model, usually for perf.
$jointcollide
If any $jointcollide pairs are specified, only those joints collide with each other.
$animatedfriction
Used to animate the amount of friction on joints over time.

Valid propdata.txt definitions

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