Blitz3D+ Command Reference

PhysicsBodyCapsule ( body,diameter#,height#[,density#][,x#][,y#][,z#][,sensor] )

Parameters

body - physics-body handle returned by CreatePhysicsBody

diameter# - width of the capsule in metres; must be positive

height# - full end-to-end height including the rounded caps; must be at least the diameter

density# (optional) - mass per cubic metre used to work out the body's mass; 1 (default)

x# (optional) - offset of the capsule centre from the body origin, local X; 0 (default)

y# (optional) - offset along local Y; 0 (default)

z# (optional) - offset along local Z; 0 (default)

sensor (optional) - True makes a non-solid sensor that only reports overlaps; False (default)

Description

Adds an upright capsule collision shape to a physics body and returns its shape handle.

A capsule is a cylinder with hemispherical caps, aligned with the body's local Y axis. It is the classic shape for characters and limbs: the rounded bottom slides smoothly over steps, seams and slopes where a box would catch its edge.

The height is the full end-to-end measurement, caps included - a height equal to the diameter is just a sphere, and a height smaller than the diameter raises a runtime error. The optional offset positions the capsule within a compound body, and a dynamic body's mass is recalculated from all its shapes' volumes and densities.

With sensor set to True the capsule reports overlaps through CountPhysicsSensorEvents instead of colliding. For a player you steer directly rather than simulate, see PhysicsMoveCharacter, which uses the same capsule measurements without creating a body.

Requires Extended mode.

See also: CreatePhysicsBody, PhysicsBodyBox, PhysicsBodySphere, PhysicsMoveCharacter, FreePhysicsShape.

Example

; PhysicsBodyCapsule Example
; --------------------------
; Requires Extended mode.

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

Const PHYSICS_STATIC=0
Const PHYSICS_DYNAMIC=2

SeedRnd MilliSecs()

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 pill dropped at a tilt so it topples and rolls
pill=CreateCylinder(16,True)
ScaleEntity pill,0.5,1.25,0.5
PositionEntity pill,0,3,0,True
RotateEntity pill,0,0,25
EntityColor pill,255,120,70
pill_body=CreatePhysicsBody(world,pill,PHYSICS_DYNAMIC)

; Y-aligned capsule: diameter 1, full end-to-end height 2.5
pill_shape=PhysicsBodyCapsule(pill_body,1,2.5,1)

While Not KeyDown(1)

    ; Space tosses the capsule up with a random tumble
    If KeyHit(57)
        PhysicsBodyVelocity pill_body,0,6,0
        PhysicsBodyAngularVelocity pill_body,Rnd(-3,3),0,Rnd(-3,3)
    EndIf

    ; Advance the Box3D simulation by one 60 Hz step
    StepPhysics world,1.0/60.0,4

    ; Respawn the capsule if it rolls off the slab
    If EntityY(pill)<-8
        FreePhysicsBody pill_body
        PositionEntity pill,0,3,0,True
        RotateEntity pill,0,0,25
        pill_body=CreatePhysicsBody(world,pill,PHYSICS_DYNAMIC)
        pill_shape=PhysicsBodyCapsule(pill_body,1,2.5,1)
    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: toss the capsule   Esc: exit"
    Text 0,20,"Capsule shape: diameter 1 m, height 2.5 m   handle: "+pill_shape

    Flip

Wend

FreePhysicsWorld world
FreeEntity pill
FreeEntity ground
End

Index