Blitz3D+ Command Reference

PhysicsBodyEntity ( body )

Parameters

body - live explicit physics body handle

Description

Returns the live Blitz entity attached to this explicit physics body.

The returned entity uses the normal entity ABI on Win32 and Win64 and can be passed directly to EntityX, EntityName or other entity commands.

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.

Requires Extended mode.

See also: CreatePhysicsBody, PhysicsBodyImpulseAt, StepPhysics.

Example

; PhysicsBodyEntity Example
; -------------------------
; Requires Extended mode.

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

    ; Find the newest crate's entity from its body, and light it up
    If count>0
        If newest Then EntityColor newest,255,120,70
        newest=PhysicsBodyEntity(crate_bodies(count))
        EntityColor newest,255,230,60
        height#=EntityY(newest)
    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

    ; 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"
    If count>0
        Text 0,20,"PhysicsBodyEntity(newest crate) = "+newest+" at height "+height
    EndIf

    Flip

Wend

FreePhysicsWorld world
End

Index