|
world - physics-world handle returned by CreatePhysicsWorld entity - the entity to move; it must not own a physics body diameter# - width of the character capsule in metres; must be positive height# - full capsule height including the rounded caps; at least the diameter dx# - requested movement this call, world X, in metres dy# - requested movement, world Y dz# - requested movement, world Z |
The entity must have no physics body, hidden classic body, pending EntityPhysics enrollment or modern Collisions participation. Movement and dimensions must be finite and representable. Parented movement requires a finite invertible parent transform and a representable final local position; rejection leaves the entity and returned fraction unchanged. Borrowed worlds from GetWorldPhysics are supported and queries read their committed geometry.
|
Moves an entity as a capsule character, sliding along whatever it touches. The classic collide-and-slide character controller. An upright capsule is placed at the entity's position, the requested movement is adjusted against nearby eligible shapes (including sensors under its native default query filter) - walls redirect it, floors stop the downward part - and the entity is moved to where the capsule ends up. Call it once per frame with your input movement, and include a small downward dy each frame as your character's gravity. The return value is the fraction (0 to 1) of the slide-adjusted movement that was actually performed - handy for detecting a dead stop. The entity's position is the capsule's centre, so keep it half the height above the floor. The capsule is a query, not a body: it collides with bodies but never pushes them, it produces no contact or sensor events, and the entity must not own a physics body of its own (that raises a runtime error). It works immediately when called - no StepPhysics needed for the movement itself, though the bodies it collides with only move during steps. The fixed 32-plane buffer raises a runtime error on overflow and retains the entity position. For configurable capacity, filters and copied solver feedback, use MovePhysicsCharacter with an owned query. See also: PhysicsBodyCapsule, PhysicsSphereCast, CreatePhysicsWorld, StepPhysics. |
; PhysicsMoveCharacter Example
; ----------------------------
Graphics3D 640,480,0,2
SetBuffer BackBuffer()
Const PHYSICS_STATIC=0
camera=CreateCamera()
PositionEntity camera,0,5,-8
RotateEntity camera,25,0,0
light=CreateLight()
RotateEntity light,45,-30,0
AmbientLight 50,50,50
world=CreatePhysicsWorld()
; Static ground slab to walk on
ground=CreateCube()
ScaleEntity ground,6,0.5,6
PositionEntity ground,0,-0.5,0,True
EntityColor ground,80,160,255
ground_body=CreatePhysicsBody(world,ground,PHYSICS_STATIC)
ground_shape=PhysicsBodyBox(ground_body,12,1,12,0)
; Scatter some static crates as obstacles
For i=1 To 4
crate=CreateCube()
ScaleEntity crate,0.75,0.75,0.75
PositionEntity crate,Cos(i*85)*2.5,0.75,Sin(i*85)*2.5,True
EntityColor crate,200,170,90
crate_body=CreatePhysicsBody(world,crate,PHYSICS_STATIC)
crate_shape=PhysicsBodyBox(crate_body,1.5,1.5,1.5,0)
Next
; The player capsule: it must NOT own a physics body of its own
character=CreateCylinder(16,True)
ScaleEntity character,0.5,1,0.5
PositionEntity character,0,1,0,True
EntityColor character,255,120,70
While Not KeyDown(1)
; WASD chooses this frame's walk translation
dx#=0
dz#=0
If KeyDown(17) Then dz=0.06
If KeyDown(31) Then dz=-0.06
If KeyDown(30) Then dx=-0.06
If KeyDown(32) Then dx=0.06
; Slide the capsule through the world and keep the accepted fraction
fraction#=PhysicsMoveCharacter(world,character,1,2,dx,0,dz)
; A second call applies simple gravity so the capsule stays grounded
fall#=PhysicsMoveCharacter(world,character,1,2,0,-0.05,0)
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,"WASD: walk the capsule Arrow keys: move camera Esc: exit"
Text 0,20,"PhysicsMoveCharacter(world,character,1,2,"+dx+",0,"+dz+") = "+fraction
Text 0,40,"Fraction 1.0 = free move; smaller means a crate blocked the walk"
Flip
Wend
FreePhysicsWorld world
End