Blitz3D+ Command Reference

PhysicsBodyMesh ( body,entity[,sensor] )

Parameters

body - physics-body handle returned by CreatePhysicsBody; the body must be static

entity - mesh entity supplying the triangles

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

Description

Builds triangle-accurate static collision from a mesh entity and returns its shape handle.

This is the shape for level geometry: floors, walls, ramps, caves - anything concave that dynamic bodies should roll and slide through exactly as it looks. Every triangle of every surface is used, scaled by the entity's current scale, with vertices welded and edges analysed so bodies cross triangle seams without snagging.

Triangle meshes can only be attached to static bodies - trying on a kinematic or dynamic body raises a runtime error. They also have no volume, so they contribute no mass. Moving props want PhysicsBodyHull or the primitive shapes instead.

The geometry is copied when the command runs; later mesh edits or rescaling do not update the shape. Detailed meshes cost memory and build time, so consider a simplified collision mesh for very dense visual geometry.

Requires Extended mode.

See also: CreatePhysicsBody, PhysicsBodyHull, PhysicsBodyTerrain, PhysicsBodyBox, FreePhysicsShape.

Example

; PhysicsBodyMesh 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,-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

; Tilted blue ramp - triangle-mesh collision follows the visual mesh
ramp=CreateCube()
ScaleEntity ramp,4,0.25,4
PositionEntity ramp,0,-0.25,0,True
RotateEntity ramp,0,0,12
EntityColor ramp,80,160,255
ramp_body=CreatePhysicsBody(world,ramp,PHYSICS_STATIC)

; Mesh shapes may only be attached to static bodies
ramp_shape=PhysicsBodyMesh(ramp_body,ramp)

; Orange ball that rolls down the ramp
ball=CreateSphere()
ScaleEntity ball,0.5,0.5,0.5
PositionEntity ball,2,3,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.4

While Not KeyDown(1)

    ; Space drops the ball onto the high side again
    If KeyHit(57)
        FreePhysicsBody ball_body
        PositionEntity ball,2,3,0,True
        ball_body=CreatePhysicsBody(world,ball,PHYSICS_DYNAMIC)
        ball_shape=PhysicsBodySphere(ball_body,1,1)
        PhysicsShapeRestitution ball_shape,0.4
    EndIf

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

    ; Respawn automatically once the ball rolls off the ramp
    If EntityY(ball)<-8
        FreePhysicsBody ball_body
        PositionEntity ball,2,3,0,True
        ball_body=CreatePhysicsBody(world,ball,PHYSICS_DYNAMIC)
        ball_shape=PhysicsBodySphere(ball_body,1,1)
        PhysicsShapeRestitution ball_shape,0.4
    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: drop the ball again   Esc: exit"
    Text 0,20,"Mesh shape handle: "+ramp_shape+"   (static bodies only)"

    Flip

Wend

FreePhysicsWorld world
FreeEntity ball
FreeEntity ramp
End

Index