Blitz3D+ Command Reference

PhysicsBodyForce body,x#,y#,z#

Parameters

body - physics-body handle returned by CreatePhysicsBody

x# - force along the world X axis, in newtons

y# - force along the world Y axis

z# - force along the world Z axis

Description

Applies a continuous force to a body's centre of mass.

Forces accelerate a body over time (acceleration is force divided by mass), which makes them the right tool for anything sustained: rocket thrust, wind, fans, magnets, car engines. Apply the force every frame before StepPhysics for as long as the effect lasts - a single call is only a tiny nudge.

Because the force acts at the centre of mass it never makes the body spin. Heavier bodies accelerate less from the same force, so scale by PhysicsBodyMass when you want equal acceleration on mixed props.

Applying a force wakes a sleeping body. For an instant kick - explosions, jumps, bat hits - use PhysicsBodyImpulse instead.

Requires Extended mode.

See also: PhysicsBodyImpulse, PhysicsBodyVelocity, PhysicsBodyMass, StepPhysics.

Example

; PhysicsBodyForce Example
; ------------------------
; Requires Extended mode.

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

Const PHYSICS_STATIC=0
Const PHYSICS_DYNAMIC=2

camera=CreateCamera()
PositionEntity camera,0,3,-10

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)

; Blue ceiling stops the rocket flying out of view
ceiling=CreateCube()
ScaleEntity ceiling,4,0.5,4
PositionEntity ceiling,0,6.5,0,True
EntityColor ceiling,80,160,255
ceiling_body=CreatePhysicsBody(world,ceiling,PHYSICS_STATIC)
ceiling_shape=PhysicsBodyBox(ceiling_body,8,1,8,0)

; Orange rocket ball resting on the ground
rocket=CreateSphere()
ScaleEntity rocket,0.5,0.5,0.5
PositionEntity rocket,0,0.5,0,True
EntityColor rocket,255,120,70
rocket_body=CreatePhysicsBody(world,rocket,PHYSICS_DYNAMIC)
rocket_shape=PhysicsBodySphere(rocket_body,1,1)

thrust#=12

While Not KeyDown(1)

    ; [ and ] adjust the thruster power
    If KeyHit(26) And thrust>4 Then thrust=thrust-2
    If KeyHit(27) And thrust<24 Then thrust=thrust+2

    ; Hold Space to fire: a sustained force applied before every step
    If KeyDown(57) Then PhysicsBodyForce rocket_body,0,thrust,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,"Hold Space: thrust   [ / ]: power   Arrow keys: camera   Esc: exit"
    Text 0,20,"PhysicsBodyForce rocket,0,"+thrust+",0 newtons"
    Text 0,40,"Vertical speed: "+PhysicsBodyVelocityY(rocket_body)+" m/s"

    Flip

Wend

FreePhysicsWorld world
FreeEntity rocket
FreeEntity ceiling
FreeEntity ground
End

Index