|
body - live explicit physics body handle index - one-based live-object index |
|
Enumerates live objects in creation order using a one-based index. Destroying an object removes its entry and shifts later indices; new objects append. Take a fresh count after changing ownership. Invalid indices and stale owner handles cause a runtime error. Ordinary accessors and setters reject stale or wrong-kind handles. Physics objects are owned by their world; deleting a body, entity tree or world also invalidates its dependent handles. See also: CreatePhysicsBody, PhysicsBodyImpulseAt, StepPhysics. |
; PhysicsBodyShape Example
; ------------------------
Graphics3D 640,480,0,2
SetBuffer BackBuffer()
Const PHYSICS_STATIC=0
Const PHYSICS_DYNAMIC=2
Const MAX_CRATES=20
SeedRnd MilliSecs()
camera=CreateCamera()
PositionEntity camera,0,5,-12
RotateEntity camera,15,0,0
light=CreateLight()
RotateEntity light,45,-30,0
AmbientLight 60,60,60
; An independent Box3D world with standard gravity
world=CreatePhysicsWorld()
; Blue static floor
ground=CreateCube()
ScaleEntity ground,6,0.5,6
PositionEntity ground,0,-0.5,0
EntityColor ground,80,160,255
ground_body=CreatePhysicsBody(world,ground,PHYSICS_STATIC)
ground_shape=PhysicsBodyBox(ground_body,12,1,12)
; A static hook, and a lamp that swings from it on a ball joint
hook=CreateCube()
ScaleEntity hook,0.2,0.2,0.2
PositionEntity hook,0,6,0
hook_body=CreatePhysicsBody(world,hook,PHYSICS_STATIC)
hook_shape=PhysicsBodyBox(hook_body,0.4,0.4,0.4)
lamp=CreateSphere(12)
ScaleMesh lamp,0.4,0.4,0.4
PositionEntity lamp,1.5,4,0
EntityColor lamp,255,230,120
lamp_body=CreatePhysicsBody(world,lamp,PHYSICS_DYNAMIC)
bulb_shape=PhysicsBodySphere(lamp_body,0.8)
cord=CreatePhysicsBallJoint(hook_body,lamp_body,0,6,0)
; The shade is a second shape on the lamp's body
shade=CreateCone(12,True,lamp)
ScaleEntity shade,0.6,0.3,0.6
PositionEntity shade,0,0.5,0
shade_shape=PhysicsBodyBox(lamp_body,1.2,0.6,1.2,1,0,0.5,0)
Dim crates(MAX_CRATES)
Dim crate_bodies(MAX_CRATES)
Dim crate_shapes(MAX_CRATES)
While Not KeyDown(1)
; Space drops a crate
If KeyHit(57) And count<MAX_CRATES
count=count+1
crates(count)=CreateCube()
ScaleEntity crates(count),0.4,0.4,0.4
EntityColor crates(count),255,120,70
crate_bodies(count)=CreatePhysicsBody(world,crates(count),PHYSICS_DYNAMIC)
crate_shapes(count)=PhysicsBodyBox(crate_bodies(count),0.8,0.8,0.8)
ResetPhysicsBody crate_bodies(count),Rnd(-3,3),5,Rnd(-3,3),0,Rnd(Pi),0
EndIf
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
; Draw the lamp's cord from the hook
CameraProject camera,0,6,0
hook_x=ProjectedX()
hook_y=ProjectedY()
CameraProject camera,EntityX(lamp),EntityY(lamp),EntityZ(lamp)
Line hook_x,hook_y,ProjectedX(),ProjectedY()
Text 0,0,"Arrow keys: move camera Space: drop a crate Esc: exit"
; Walk the lamp's shapes: type 1 is a sphere, type 3 a convex hull
For i=1 To CountPhysicsBodyShapes(lamp_body)
shape=PhysicsBodyShape(lamp_body,i)
Text 0,20+(i-1)*20,"PhysicsBodyShape(lamp_body,"+i+") = "+shape+" type "+PhysicsShapeType(shape)
Next
Flip
Wend
FreePhysicsWorld world
End