Physics Entities on Server & Client: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
m (Nesciuse moved page Physics Entities on Server & Client/en to Physics Entities on Server & Client without leaving a redirect: Move en subpage to basepage)
 
(12 intermediate revisions by 11 users not shown)
Line 1: Line 1:
[http://rumour.servemp3.com/blog/jackpot-party.html jackpot party] [http://baffle.servemp3.com/blog/olympus-vn-480pc.html olympus vn-480pc] [http://capdv.servemp3.com/models.html models] [http://jeshu.servemp3.com/free-mp3-ringtone.htm free mp3 ringtone editor] [http://bigger.servemp3.com/web/work-boots.html work boots] [http://while.servemp3.com/resources/downloads-sex.html downloads sex] [http://shrewd.servemp3.com/web/pottery-barn-promotion.htm pottery barn promotion code] [http://shrewd.servemp3.com/web/clitoral-hood.htm clitoral hood piercings] [http://weigh.servemp3.com/library/spanish-lessons.html spanish lessons] [http://baffle.servemp3.com/blog/wedding-gown.html wedding gown] [http://while.servemp3.com/resources/vodafone-mobile.html vodafone mobile phones] [http://while.servemp3.com/resources/heart-disease.html heart disease] [http://while.servemp3.com/resources/street-legal.html street legal scooters] [http://capdv.servemp3.com/handjob-free.html handjob free] [http://weigh.servemp3.com/library/bone-thugs-n.html bone thugs n harmony] [http://rumour.servemp3.com/blog/wedding-stationary.html wedding stationary] [http://incite.servemp3.com/q/oakley-sun-glasses.html oakley sun glasses] [http://baffle.servemp3.com/blog/free-email-search.html free email search] [http://rumour.servemp3.com/blog/picture-woman.html picture woman] [http://rumour.servemp3.com/blog/nurses-sexy.html nurses sexy] [http://shrewd.servemp3.com/web/gallbladder.htm gallbladder symptoms] [http://rumour.servemp3.com/blog/haverty.html haverty] [http://rumour.servemp3.com/blog/glass-cake-dome.html glass cake dome] [http://rumour.servemp3.com/blog/www-lilkim.html www lilkim] [http://rumour.servemp3.com/blog/porn-star-houston.html porn star houston] [http://while.servemp3.com/resources/discount-scrubs.html discount scrubs] [http://shrewd.servemp3.com/web/coat.htm coat] [http://jeshu.servemp3.com/tano-handbags.htm tano handbags] [http://jeshu.servemp3.com/pussy-fucking.htm pussy fucking] [http://bigger.servemp3.com/web/animated-cartoons.html animated cartoons] [http://incite.servemp3.com/q/bed-bath-and-beyond.html bed bath and beyond] [http://baffle.servemp3.com/blog/proposal-writing.html proposal writing] [http://bigger.servemp3.com/web/holiday-insurance.html holiday insurance] [http://capdv.servemp3.com/buy-xanax-online.html buy xanax online] [http://weigh.servemp3.com/library/post-office.html post office] [http://rumour.servemp3.com/blog/femdom-porn.html femdom porn] [http://while.servemp3.com/resources/masschusetts.html masschusetts] [http://incite.servemp3.com/q/flight-school.html flight school] [http://bigger.servemp3.com/web/colorado-corporations.html colorado corporations] [http://rumour.servemp3.com/blog/peter-north-cum.html peter north cum shot] [http://while.servemp3.com/resources/latinas.html latinas] [http://jeshu.servemp3.com/portable-dvd-player.htm portable dvd player] [http://weigh.servemp3.com/library/esomeprazole.html esomeprazole] [http://rumour.servemp3.com/blog/black-video.html black video] [http://incite.servemp3.com/q/etodolac.html etodolac] [http://incite.servemp3.com/q/first-timers.html first timers] [http://shrewd.servemp3.com/web/dubai-jobs.htm dubai jobs] [http://weigh.servemp3.com/library/misoprostol.html misoprostol] [http://capdv.servemp3.com/homes-for-sale-by.html homes for sale by owner] [http://shrewd.servemp3.com/web/pontiac-parts.htm pontiac parts] [http://rumour.servemp3.com/blog/wet-hot-sex.html wet hot sex] [http://rumour.servemp3.com/blog/dlp-tv.html dlp tv] [http://jeshu.servemp3.com/capital-one.htm capital one] [http://while.servemp3.com/resources/foto.html foto] [http://weigh.servemp3.com/library/asiansex.html asiansex] [http://while.servemp3.com/resources/video-slot-machine.html video slot machine] [http://capdv.servemp3.com/free-criminal-background.html free criminal background check] [http://baffle.servemp3.com/blog/thank-you.html thank you] [http://capdv.servemp3.com/free-online-game.html free online game] [http://incite.servemp3.com/q/free-anti-virus.html free anti-virus for nokia 6630] [http://bigger.servemp3.com/web/sharon-stone-nude.html sharon stone nude] [http://rumour.servemp3.com/blog/india-porno.html india porno] [http://while.servemp3.com/resources/hard-rock.html hard rock] [http://baffle.servemp3.com/blog/ohio-colleges.html ohio colleges] [http://jeshu.servemp3.com/home-finance.htm home finance] [http://weigh.servemp3.com/library/confidentiality.html confidentiality agreement] [http://while.servemp3.com/resources/lexmark-cartridges.html lexmark cartridges] [http://capdv.servemp3.com/pussy-pump.html pussy pump] [http://incite.servemp3.com/q/kawasaki-atvs.html kawasaki atvs] [http://shrewd.servemp3.com/web/enlarged-thyroid.htm enlarged thyroid] [http://rumour.servemp3.com/blog/anal-pictures.html anal pictures] [http://incite.servemp3.com/q/las-vegas-massage.html las vegas massage] [http://jeshu.servemp3.com/black-hardcore.htm black hardcore mature] [http://bigger.servemp3.com/web/ovral.html ovral] [http://weigh.servemp3.com/library/arizona-newspapers.html arizona newspapers] [http://bigger.servemp3.com/web/adult-sex-movies.html adult sex movies] [http://rumour.servemp3.com/blog/windows-washer.html windows washer] [http://bigger.servemp3.com/web/beluga-whales.html beluga whales] [http://jeshu.servemp3.com/rocket-dog-shoes.htm rocket dog shoes] [http://rumour.servemp3.com/blog/milf-lessons.html milf lessons] [http://baffle.servemp3.com/blog/coach-outlet.html coach outlet stores] [http://incite.servemp3.com/q/currency-rates.html currency rates] [http://while.servemp3.com/resources/sexy-milf.html sexy milf] [http://bigger.servemp3.com/web/canada-job-bank.html canada job bank ca] [http://while.servemp3.com/resources/darth-maul.html darth maul] [http://bigger.servemp3.com/web/french-translator.html french translator] [http://bigger.servemp3.com/web/cursive-writing.html cursive writing exercises] [http://rumour.servemp3.com/blog/spanking-porn.html spanking porn] [http://rumour.servemp3.com/blog/myspace-posters.html myspace posters] [http://capdv.servemp3.com/houseboat.html houseboat] [http://jeshu.servemp3.com/pictures.htm pictures] [http://while.servemp3.com/resources/nissan-altima.html nissan altima] [http://rumour.servemp3.com/blog/japanese-used-car.html japanese used car] [http://bigger.servemp3.com/web/latin-sex.html latin sex] [http://weigh.servemp3.com/library/monster.html monster] [http://bigger.servemp3.com/web/diabetes.html diabetes] [http://capdv.servemp3.com/manila-hotels.html manila hotels] [http://jeshu.servemp3.com/pansat.htm pansat] [http://capdv.servemp3.com/female-feet.html female feet bastinado] [http://weigh.servemp3.com/library/autocad-symbols.html autocad symbols] [http://capdv.servemp3.com/softporn.html softporn] [http://while.servemp3.com/resources/italian-shoes.html italian shoes] [http://capdv.servemp3.com/baby-crib-bedding.html baby crib bedding set] [http://capdv.servemp3.com/elavil.html elavil] [http://jeshu.servemp3.com/wynn-hotel.htm wynn hotel] [http://rumour.servemp3.com/blog/tattoo-heart.html tattoo heart] [http://rumour.servemp3.com/blog/housewives-porn.html housewives porn] [http://rumour.servemp3.com/blog/washington-property.html washington property] [http://rumour.servemp3.com/blog/dj-sammy-heaven.html dj sammy heaven 911 video] [http://incite.servemp3.com/q/russian-porn.html russian porn] [http://incite.servemp3.com/q/amc-theater.html amc theater] [http://weigh.servemp3.com/library/solitaire-card.html solitaire card game] [http://weigh.servemp3.com/library/free-bingo-game.html free bingo game] [http://while.servemp3.com/resources/shade-garden-designs.html shade garden designs] [http://while.servemp3.com/resources/matchmaking.html matchmaking] [http://shrewd.servemp3.com/web/eurodollar.htm eurodollar] [http://weigh.servemp3.com/library/car-ins.html car ins] [http://weigh.servemp3.com/library/dictionary-spanish.html dictionary spanish to english] [http://capdv.servemp3.com/lesbian-dating.html lesbian dating] [http://weigh.servemp3.com/library/interest-rates.html interest rates] [http://incite.servemp3.com/q/philippine-airline.html philippine airline] [http://shrewd.servemp3.com/web/detroit-airport.htm detroit airport] [http://weigh.servemp3.com/library/bargains-sales.html bargains sales] [http://while.servemp3.com/resources/living-trust.html living trust] [http://jeshu.servemp3.com/adultfriendfinder.htm adultfriendfinder] [http://rumour.servemp3.com/blog/yeast-infection.html yeast infection causes] [http://weigh.servemp3.com/library/america-west-airline.html america west airline schedule] [http://weigh.servemp3.com/library/bed-and-breakfast.html bed and breakfast in london] [http://jeshu.servemp3.com/limewire-pro.htm limewire pro] [http://jeshu.servemp3.com/biggest-boobs.htm biggest boobs] [http://jeshu.servemp3.com/designer-dress.htm designer dress shoes] [http://while.servemp3.com/resources/the-star-spangled.html the star spangled banner] [http://shrewd.servemp3.com/web/sex-dvd.htm sex dvd] [http://capdv.servemp3.com/disney-girls.html disney girls] [http://while.servemp3.com/resources/sexy-girl.html sexy girl] [http://rumour.servemp3.com/blog/car-sale-wash.html car sale wash] [http://baffle.servemp3.com/blog/gmail.html gmail] [http://capdv.servemp3.com/nude-movie-stars.html nude movie stars] [http://while.servemp3.com/resources/culinary-schools.html culinary schools] [http://rumour.servemp3.com/blog/high-rate-cds.html high rate cds] [http://rumour.servemp3.com/blog/airline-tickets.html airline tickets under 100] [http://baffle.servemp3.com/blog/travel-insurance.html travel insurance] [http://baffle.servemp3.com/blog/gay-black.html gay black] [http://baffle.servemp3.com/blog/xbox.html xbox] [http://bigger.servemp3.com/web/escorts.html escorts] [http://capdv.servemp3.com/last-minute.html last minute airfare discount] [http://baffle.servemp3.com/blog/free-music-downloads.html free music downloads] [http://rumour.servemp3.com/blog/cartier-replica.html cartier replica watch] [http://while.servemp3.com/resources/us-map.html us map] [http://rumour.servemp3.com/blog/cheep-airline-tickets.html cheep airline tickets] [http://while.servemp3.com/resources/university-of.html university of miami logo] [http://capdv.servemp3.com/discount-soma.html discount soma] [http://shrewd.servemp3.com/web/buy-birkenstocks.htm buy birkenstocks] [http://rumour.servemp3.com/blog/motorola-fax.html motorola fax modem] [http://while.servemp3.com/resources/la-weightloss.html la weightloss] [http://capdv.servemp3.com/cellphone-ringtones.html cellphone ringtones] [http://shrewd.servemp3.com/web/free-lesbian-videos.htm free lesbian videos] [http://rumour.servemp3.com/blog/xxx-movies.html xxx movies] [http://bigger.servemp3.com/web/ebony-personal.html ebony personal] [http://capdv.servemp3.com/kitchen.html kitchen] [http://incite.servemp3.com/q/analsex.html analsex] [http://capdv.servemp3.com/hard.html hard] [http://weigh.servemp3.com/library/chevy-truck-accessory.html chevy truck accessory] [http://shrewd.servemp3.com/web/oxycontin-no.htm oxycontin no prescription] [http://baffle.servemp3.com/blog/san-juan-fiberglass.html san juan fiberglass pool] [http://capdv.servemp3.com/xantrex-inverters.html xantrex inverters] [http://rumour.servemp3.com/blog/outdoor-oven.html outdoor oven] [http://incite.servemp3.com/q/divorce-laws.html divorce laws] [http://incite.servemp3.com/q/norton-anti-virus.html norton anti virus download] [http://incite.servemp3.com/q/sexy-women-pics.html sexy women pics] [http://weigh.servemp3.com/library/code.html code] [http://bigger.servemp3.com/web/gift-idea-for.html gift idea for my boyfriend] [http://rumour.servemp3.com/blog/tennis-shoes.html tennis shoes] [http://capdv.servemp3.com/bedwetting.html bedwetting] [http://rumour.servemp3.com/blog/games-for-girls.html games for girls] [http://shrewd.servemp3.com/web/cartier-watch.htm cartier watch] [http://rumour.servemp3.com/blog/bald-pussy.html bald pussy] [http://incite.servemp3.com/q/eye-glass-frame.html eye glass frame] [http://baffle.servemp3.com/blog/vintage-designer.html vintage designer handbags] [http://capdv.servemp3.com/renters-insurance.html renters insurance] [http://jeshu.servemp3.com/doughboy.htm doughboy] [http://while.servemp3.com/resources/sexy-jeans.html sexy jeans] [http://rumour.servemp3.com/blog/online-music.html online music] [http://while.servemp3.com/resources/pittsburgh-emergency.html pittsburgh emergency room error attorneys] [http://baffle.servemp3.com/blog/sympathy.html sympathy] [http://weigh.servemp3.com/library/googli.html googli] [http://baffle.servemp3.com/blog/isuzu-axiom.html isuzu axiom] [http://capdv.servemp3.com/wallpapper.html wallpapper] [http://weigh.servemp3.com/library/flower-tattoo.html flower tattoo designs] [http://weigh.servemp3.com/library/orange-wanadoo.html orange wanadoo] [http://capdv.servemp3.com/wachovia-bank.html wachovia bank] [http://incite.servemp3.com/q/john-cena.html john cena] [http://incite.servemp3.com/q/sedu-hair-straightener.html sedu hair straightener] [http://incite.servemp3.com/q/nero-mp3-plugin.html nero mp3 plugin] [http://weigh.servemp3.com/library/free-gay-sex.html free gay sex] [http://weigh.servemp3.com/library/terrario.html terrario] [http://jeshu.servemp3.com/georgetown-university.htm georgetown university] [http://while.servemp3.com/resources/replacement.html replacement parts for kenmore dishwashers] [http://shrewd.servemp3.com/web/jeep-parts.htm jeep parts] [http://incite.servemp3.com/q/display-pictures.html display pictures]
{{LanguageBar}}
[[Category:Programming]]
=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 dampening. 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 doesn'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 change their position and orientation basically 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.
==Overview==


=Adding physics entities=
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.


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 (<code>COLLISION_GROUP_PUSHAWAY</code>). 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.
==Adding physics entities==


The physical properties of a model are defined in it's .QC file by 3 entries: <code>$surfaceprop</code>, <code>$collisionmodel</code>, <code>$keyvalues</code>. The first entry $surfaceprop sets the model surface properties as defined in text file <code>\scripts\surfaceproperties.txt</code>. There properties like friction, elasticity and collision sounds are defined. The next entry <code>$collisionmodel</code> sets the collision model and the objects weight. The see collision models for entities in game enable console variable <code>"vcollide_wireframe 1"</code>. The last section are the <code>"prop_data"</code> entries under <code>$keyvalues</code>. Here the object properties like health, break models and physics mode are defined ([[Prop Data|full description of prop_data]]). Here an example:
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 (<code>[[COLLISION_GROUP_PUSHAWAY]]</code>). 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.


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


$collisionmodel "mymodel.smd" {
$surfaceprop cardboard // object surface properties
    $Mass 40 // Mass in kilograms
    $concave
$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
}
}


$keyvalues
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 <code>props_shared.h</code>:
{
"prop_data"
{
"base" "Cardboard.Medium" // base material defined in propdata.txt
"health" "40" // overriding material properties
"physicsmode" "1" // setting a custom physics mode
}
}
</pre>


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 <code>props_shared.h</code>:
#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 <code>[[GetAutoMultiplayerPhysicsMode()]]</code> 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 <code>[[sv_pushaway_clientside_size]]</code>).


<pre>
==Collision Groups==
#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>
</pre>


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 <code>GetAutoMultiplayerPhysicsMode()</code> 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 <code>sv_pushaway_clientside_size</code>).
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 <code>[[SetCollisionGroup()]]</code>. New groups and new rules can be added easily. The physics subsystem queries the virtual function bool <code>CGameRules::[[ShouldCollide()|ShouldCollide]](int group0, int group1)</code> to see if a collision check must be run for between two objects. Already existing collision groups are:


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


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 <code>SetCollisionGroup()</code>. New groups and new rules can be added easily. The physics subsystem queries the virtual function bool <code>CGameRules::ShouldCollide(int group0, int group1)</code> to see if a collision check must be run for between two objects. Already existing collision groups are:
==Appendix==
{|
| <code>COLLISION_GROUP_NONE</code> || Default, collsides with static and dynamic objects
|-
| <code>COLLISION_GROUP_DEBRIS</code> || Collides with nothing but world and static stuff
|-
| <code>COLLISION_GROUP_DEBRIS_TRIGGER</code> || Same as debris, but hits triggers
|-
| <code>COLLISION_GROUP_INTERACTIVE_DEBRIS</code> || Collides with everything except other interactive debris or debris
|-
| <code>COLLISION_GROUP_INTERACTIVE</code> || Collides with everything except interactive debris or debris
|-
| <code>COLLISION_GROUP_PLAYER</code> || Collision group for player
|-
| <code>COLLISION_GROUP_BREAKABLE_GLASS</code> || Special group for glass debris
|-
| <code>COLLISION_GROUP_VEHICLE</code> || Collision group for driveable vehicles
|-
| <code>COLLISION_GROUP_PLAYER_MOVEMENT</code> || For singleplayer, same as Collision_Group_Player, for multiplayer, this filters out other players and CBaseObjects
|-
| <code>COLLISION_GROUP_NPC</code> || Generic NPC group
|-
| <code>COLLISION_GROUP_IN_VEHICLE</code> || for any entity inside a vehicle
|-
| <code>COLLISION_GROUP_WEAPON</code> || for any weapons that need collision detection
|-
| <code>COLLISION_GROUP_VEHICLE_CLIP</code> || vehicle clip brush to restrict vehicle movement
|-
| <code>COLLISION_GROUP_PROJECTILE</code> || Projectiles
|-
| <code>COLLISION_GROUP_DOOR_BLOCKER</code> || Blocks entities not permitted to get near moving doors
|-
| <code>COLLISION_GROUP_PASSABLE_DOOR</code> || Doors that the player shouldn't collide with
|-
| <code>COLLISION_GROUP_DISSOLVING</code> || Things that are dissolving are in this group
|-
| <code>COLLISION_GROUP_PUSHAWAY</code> || Nonsolid on client and server, pushs player away
|}


=Appendix=
===Valid $collisionmodel definitions===


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


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


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


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

Latest revision as of 08:49, 12 July 2024

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)