Blitz3D+ Command Reference

PhysicsGravity world,x#,y#,z#

Parameters

world - physics-world handle returned by CreatePhysicsWorld

x# - gravity acceleration along the world X axis, in metres per second squared

y# - gravity acceleration along the world Y axis; negative values pull downwards

z# - gravity acceleration along the world Z axis

Description

Sets a physics world's gravity vector.

A new world starts with gravity 0,-10,0. For an Earth-like feel with Y up, use 0,-9.81,0; for a space game or top-down play, 0,0,0 switches gravity off entirely. You can point gravity any way you like - sideways gravity makes for fun wall-walking experiments.

The new vector is picked up by the next StepPhysics call and only affects dynamic bodies; static and kinematic bodies never fall. To make one particular body float, sink faster or fall upwards without touching everything else, use PhysicsBodyGravityScale instead.

Requires Extended mode.

See also: CreatePhysicsWorld, StepPhysics, PhysicsBodyGravityScale, PhysicsBodyDamping.

Example

; PhysicsGravity 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,-6

light=CreateLight()
RotateEntity light,45,-30,0
AmbientLight 50,50,50

world=CreatePhysicsWorld()

; Static ground slab
ground=CreateCube()
ScaleEntity ground,4,0.5,4
PositionEntity ground,0,-0.5,5,True
EntityColor ground,80,160,255
ground_body=CreatePhysicsBody(world,ground,PHYSICS_STATIC)
ground_shape=PhysicsBodyBox(ground_body,8,1,8,0)

; A bouncy ball to show off the pull of gravity
ball=CreateSphere(16)
ScaleEntity ball,0.5,0.5,0.5
PositionEntity ball,0,4,5,True
EntityColor ball,255,120,70
ball_body=CreatePhysicsBody(world,ball,PHYSICS_DYNAMIC)
ball_shape=PhysicsBodySphere(ball_body,1,1)
PhysicsShapeRestitution ball_shape,0.8

gy#=-9.81

While Not KeyDown(1)

    ; 1 / 2 / 3 choose Earth, Moon or zero gravity
    If KeyHit(2) Then gy=-9.81
    If KeyHit(3) Then gy=-1.62
    If KeyHit(4) Then gy=0

    ; [ and ] fine-tune the downward pull
    If KeyDown(26) Then gy=gy-0.05
    If KeyDown(27) Then gy=gy+0.05
    If gy<-30 Then gy=-30
    If gy>0 Then gy=0

    ; Apply the current gravity vector to the whole world
    PhysicsGravity world,0,gy,0

    ; Space kicks the ball into the air
    If KeyHit(57) Then PhysicsBodyImpulse ball_body,0,3,0

    StepPhysics world,1.0/60.0,4

    ; Bring the ball back if it drifts out of the arena
    If EntityY(ball,True)<-6 Or EntityY(ball,True)>25
        FreePhysicsBody ball_body
        PositionEntity ball,0,4,5,True
        ball_body=CreatePhysicsBody(world,ball,PHYSICS_DYNAMIC)
        ball_shape=PhysicsBodySphere(ball_body,1,1)
        PhysicsShapeRestitution ball_shape,0.8
    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,"1/2/3: Earth/Moon/zero-g   [ / ]: tune   Space: kick   Arrows: camera   Esc: exit"
    Text 0,20,"PhysicsGravity world,0,"+gy+",0"
    Text 0,40,"Ball Y: "+EntityY(ball,True)

    Flip

Wend

FreePhysicsWorld world
End

Index