| shape - physics-shape handle returned by a shape command such as PhysicsBodyBox |
|
Destroys a single collision shape. The owning body and its entity live on - only the one shape disappears. A dynamic body's mass is recalculated from its remaining shapes, and a body with no shapes left stops colliding with anything at all. The usual reason to free a shape is swapping it: to change a shape's size or type, free the old handle and attach a new shape in its place. The freed handle becomes invalid and using it again raises a runtime error. Explicit cleanup is optional at teardown - FreePhysicsBody and FreePhysicsWorld destroy their shapes automatically. See also: PhysicsBodyBox, PhysicsBodySphere, PhysicsBodyCapsule, FreePhysicsBody, FreePhysicsWorld. |
; FreePhysicsShape Example
; ------------------------
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 platform whose collision shape we will free and restore
platform=CreateCube()
ScaleEntity platform,4,0.5,4
PositionEntity platform,0,-0.5,0,True
EntityColor platform,80,160,255
platform_body=CreatePhysicsBody(world,platform,PHYSICS_STATIC)
platform_shape=PhysicsBodyBox(platform_body,8,1,8,0)
; Bouncy orange ball dropped onto the platform
ball=CreateSphere()
ScaleEntity ball,0.5,0.5,0.5
PositionEntity ball,0,4,0,True
EntityColor ball,255,120,70
ball_body=CreatePhysicsBody(world,ball,PHYSICS_DYNAMIC)
ball_shape=PhysicsBodySphere(ball_body,1,1)
PhysicsShapeRestitution ball_shape,0.6
While Not KeyDown(1)
; Space frees the platform's shape, or attaches a new one
If KeyHit(57)
If platform_shape<>0
; Free the shape - the body and entity remain, but nothing collides
FreePhysicsShape platform_shape
platform_shape=0
EntityAlpha platform,0.3
Else
; Attach a new box shape to restore collision
platform_shape=PhysicsBodyBox(platform_body,8,1,8,0)
EntityAlpha platform,1
EndIf
EndIf
; Advance the Box3D simulation by one 60 Hz step
StepPhysics world,1.0/60.0,4
; Respawn the ball above the platform once it falls through
If EntityY(ball)<-6
FreePhysicsBody ball_body
PositionEntity ball,0,4,0,True
ball_body=CreatePhysicsBody(world,ball,PHYSICS_DYNAMIC)
ball_shape=PhysicsBodySphere(ball_body,1,1)
PhysicsShapeRestitution ball_shape,0.6
EndIf
; 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: free / restore shape Esc: exit"
If platform_shape<>0
Text 0,20,"Platform shape handle: "+platform_shape+" (solid)"
Else
Text 0,20,"Shape freed - the ball falls straight through the ghost platform"
EndIf
Flip
Wend
FreePhysicsWorld world
FreeEntity ball
FreeEntity platform
End