CreatePhysicsBody ( world,entity,body_type )
Parameters
|
world - physics-world handle returned by CreatePhysicsWorld entity - the Blitz entity the body is attached to; an entity can own at most one physics body body_type - how the body behaves: 0: static - never moves; floors, walls and level geometry 1: kinematic - follows its entity; moving platforms and doors you animate yourself 2: dynamic - fully simulated; crates, balls, debris and anything that should fall |
Description
|
Creates a physics body for an entity and returns its handle. The body is created at the entity's current world position and rotation, so place the entity first. From then on the two are linked: kinematic bodies copy the entity's transform into the simulation before every StepPhysics call, while dynamic bodies move the entity to match the simulation after every step. Positioning a dynamic body's entity by hand does not stick - the next step overwrites it, so steer dynamic bodies with PhysicsBodyVelocity, PhysicsBodyForce or PhysicsBodyImpulse instead. A new body has no collision geometry at all. Attach at least one shape with PhysicsBodyBox, PhysicsBodySphere, PhysicsBodyCapsule, PhysicsBodyHull, PhysicsBodyMesh or PhysicsBodyTerrain, or a dynamic body will simply fall through the level. One body per entity: creating a second body for the same entity raises a runtime error. FreePhysicsBody removes a body, and FreeEntity on the entity removes its body automatically. Requires Extended mode. See also: CreatePhysicsWorld, PhysicsBodyBox, PhysicsBodySphere, PhysicsBodyCapsule, StepPhysics, FreePhysicsBody. |
Example
; CreatePhysicsBody Example ; ------------------------- ; Requires Extended mode. Graphics3D 640,480,0,2 SetBuffer BackBuffer() ; Body types: 0 static, 1 kinematic, 2 dynamic 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 ; The blue ground gets a static body - it never moves 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) ; The orange crate gets a dynamic body - Box3D drives its entity crate=CreateCube() ScaleEntity crate,0.5,0.5,0.5 PositionEntity crate,0,3,0,True EntityColor crate,255,120,70 crate_body=CreatePhysicsBody(world,crate,PHYSICS_DYNAMIC) crate_shape=PhysicsBodyBox(crate_body,1,1,1,1) PhysicsShapeRestitution crate_shape,0.4 While Not KeyDown(1) ; Space tosses the crate back up If KeyHit(57) Then PhysicsBodyVelocity crate_body,0,6,0 ; Advance the Box3D simulation by one 60 Hz step StepPhysics world,1.0/60.0,4 ; 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,"Arrow keys: move camera Space: toss the crate Esc: exit" Text 0,20,"Dynamic crate body: "+crate_body+" Static ground body: "+ground_body Flip Wend FreePhysicsWorld world FreeEntity crate FreeEntity ground End
Index