PhysicsBodyMass# ( body )
Parameters
| body - physics-body handle returned by CreatePhysicsBody |
Description
|
Returns a body's mass in kilograms. Mass is not set directly - it is computed from the volume and density of every shape attached to the body, and recalculated whenever a shape is added or freed. A 1x1x1 box at density 1 weighs 1 kilogram; make the box bigger or denser and the body gets heavier. Static and kinematic bodies always report 0, as do bodies whose only shapes are triangle meshes or terrain (those shapes have no volume). The classic use is scaling pushes: PhysicsBodyImpulse changes velocity by impulse divided by mass, so multiply your desired velocity change by this value and light and heavy props respond identically. Requires Extended mode. See also: PhysicsBodyBox, PhysicsBodySphere, PhysicsBodyImpulse, PhysicsBodyForce, PhysicsBodyGravityScale. |
Example
; PhysicsBodyMass Example ; ----------------------- ; Requires Extended mode. Graphics3D 640,480,0,2 SetBuffer BackBuffer() Const PHYSICS_STATIC=0 Const PHYSICS_DYNAMIC=2 camera=CreateCamera() PositionEntity camera,0,3,-9 light=CreateLight() RotateEntity light,45,-30,0 AmbientLight 50,50,50 ; Independent Box3D world with Earth-like gravity world=CreatePhysicsWorld() PhysicsGravity world,0,-9.81,0 ; Blue static ground slab ground=CreateCube() ScaleEntity ground,4,0.5,4 PositionEntity ground,0,-0.5,0,True EntityColor ground,80,160,255 ground_body=CreatePhysicsBody(world,ground,PHYSICS_STATIC) ground_shape=PhysicsBodyBox(ground_body,8,1,8,0) ; Orange crate whose density - and so mass - you can change density#=1 crate=CreateCube() ScaleEntity crate,0.5,0.5,0.5 PositionEntity crate,0,0.5,0,True EntityColor crate,255,120,70 crate_body=CreatePhysicsBody(world,crate,PHYSICS_DYNAMIC) crate_shape=PhysicsBodyBox(crate_body,1,1,1,density) While Not KeyDown(1) ; Keys 1/2/3 rebuild the shape with a new density new_density#=0 If KeyHit(2) Then new_density=0.5 If KeyHit(3) Then new_density=1.0 If KeyHit(4) Then new_density=4.0 If new_density>0 density=new_density FreePhysicsShape crate_shape crate_shape=PhysicsBodyBox(crate_body,1,1,1,density) EndIf ; Space kicks with a FIXED impulse - heavier crates jump less If KeyHit(57) Then PhysicsBodyImpulse crate_body,0,4,0 ; Advance the Box3D simulation by one 60 Hz step StepPhysics world,1.0/60.0,4 ; Read the mass Box3D calculated from shape volume times density mass#=PhysicsBodyMass(crate_body) ; Arrow keys move the camera If KeyDown(200) Then MoveEntity camera,0,0,0.1 If KeyDown(208) Then MoveEntity camera,0,0,-0.1 If KeyDown(203) Then TurnEntity camera,0,1,0 If KeyDown(205) Then TurnEntity camera,0,-1,0 RenderWorld Text 0,0,"1/2/3: density Space: fixed kick Arrow keys: camera Esc: exit" Text 0,20,"Density: "+density+" PhysicsBodyMass = "+mass+" kg" Flip Wend FreePhysicsWorld world FreeEntity crate FreeEntity ground End
Index