Blitz3D+ Command Reference

PhysicsBodyVelocityX# ( body )

Parameters

body - physics-body handle returned by CreatePhysicsBody

Description

Returns the X component of a body's linear velocity.

The value is in metres per second along the world X axis. Together with PhysicsBodyVelocityY and PhysicsBodyVelocityZ it gives the full velocity vector - the overall speed is Sqr(vx*vx+vy*vy+vz*vz).

Typical uses: speedometers, capping a body's top speed by rescaling the vector and calling PhysicsBodyVelocity, or steering sound and animation from how fast something is really moving.

Requires Extended mode.

See also: PhysicsBodyVelocity, PhysicsBodyVelocityY, PhysicsBodyVelocityZ, PhysicsBodyAngularVelocityX.

Example

; PhysicsBodyVelocityX 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,4,-12

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 arena floor
ground=CreateCube()
ScaleEntity ground,5,0.5,5
PositionEntity ground,0,-0.5,0,True
EntityColor ground,80,160,255
ground_body=CreatePhysicsBody(world,ground,PHYSICS_STATIC)
ground_shape=PhysicsBodyBox(ground_body,10,1,10,0)

; Four blue walls keep the ball inside the arena
For i=0 To 3
    wall=CreateCube()
    ScaleEntity wall,0.25,1.5,5
    RotateEntity wall,0,i*90,0
    PositionEntity wall,Cos(i*90)*5,1.5,Sin(i*90)*5,True
    EntityColor wall,80,160,255
    wall_body=CreatePhysicsBody(world,wall,PHYSICS_STATIC)
    wall_shape=PhysicsBodyBox(wall_body,0.5,3,10,0)
Next

; Lively orange ball bouncing around the arena
ball=CreateSphere()
ScaleEntity ball,0.5,0.5,0.5
PositionEntity ball,0,2,0,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

; Set the ball's starting velocity in metres per second
PhysicsBodyVelocity ball_body,3,5,2

While Not KeyDown(1)

    ; Space overwrites the velocity with a new random kick
    If KeyHit(57) Then PhysicsBodyVelocity ball_body,Rnd(-3,3),Rnd(3,6),Rnd(-3,3)

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

    ; Read back the live velocity components
    vel_x#=PhysicsBodyVelocityX(ball_body)
    vel_y#=PhysicsBodyVelocityY(ball_body)
    vel_z#=PhysicsBodyVelocityZ(ball_body)

    ; 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: kick the ball   Esc: exit"
    Text 0,20,"Velocity X: "+vel_x
    Text 0,40,"Velocity Y: "+vel_y
    Text 0,60,"Velocity Z: "+vel_z

    Flip

Wend

FreePhysicsWorld world
FreeEntity ball
FreeEntity ground
End

Index