PhysicsLinePick
Finds the first physics body along a line.
PhysicsLinePick ( x#,y#,z#,dx#,dy#,dz#[,radius#] )
Parameters
- x#, y#, z#
- where the line starts
- dx#, dy#, dz#
- the line's direction and length
- radius# (optional)
- sweep a ball of this radius along the line instead of a thin line. Defaults to 0.
Description
Returns the first physics body along a line, or 0 if there is none. Like LinePick, it sets PickedX, PickedY, PickedZ, PickedNX, PickedNY, PickedNZ, PickedTime and PickedEntity; PickedTime is how far along the line the hit is, from 0 to 1.
It looks only at physics bodies, and finds them where they are now, even if they have moved since the last UpdateWorld (kinematic bodies are found where the last UpdateWorld left them). Use it for lasers, line of sight, aiming and checking for ground beneath a character; a radius checks whether something the size of a ball could pass.
Example
; Physics queries example (an ArcBasic extension); A turning laser shows what it hits; Space sets off a blast that throws; everything nearby into the airGraphics3D 640,480SetBuffer BackBuffer()camera=CreateCamera()PositionEntity camera,0,10,-14TurnEntity camera,35,0,0light=CreateLight()RotateEntity light,50,30,0ground=CreatePlane()EntityColor ground,60,90,60EntityPhysics ground,1For i=1 To 30 c=CreateCube() ScaleEntity c,.4,.4,.4 PositionEntity c,Rnd(-6,6),.4,Rnd(-4,6) EntityPhysics c,2Nextlaser=CreatePivot()PositionEntity laser,0,.5,0beam=CreateCube()EntityColor beam,255,40,40EntityFX beam,1dot=CreateSphere(6,beam)ScaleEntity dot,.15,.15,.15While Not KeyHit(1) TurnEntity laser,0,1,0 TFormVector 0,0,20,laser,0 hit=PhysicsLinePick(EntityX(laser),.5,EntityZ(laser),TFormedX(),0,TFormedZ()) If lastHit Then EntityColor lastHit,255,255,255 If hit length#=PickedTime()*20 EntityColor hit,255,120,120 Else length#=20 EndIf lastHit=hit ; stretch the beam from the laser to what it hit ScaleEntity beam,.03,.03,length/2 PositionEntity beam,0,.5,0 RotateEntity beam,0,EntityYaw(laser),0 MoveEntity beam,0,0,length/2 If KeyHit(57) ; everything within 4 units of the middle is thrown outwards For i=1 To PhysicsOverlap(0,0,1,4) e=OverlapEntity(i) If e<>ground dx#=EntityX(e)-0 : dz#=EntityZ(e)-1 ApplyImpulse e,dx*.4,1.5,dz*.4 EndIf Next EndIf UpdateWorld RenderWorld Text 0,0,"Space: blast Cubes in the box in front: "+PhysicsOverlapBox(0,.5,-3,6,1,2) FlipWendEnd
