Code archives/3D Graphics - Misc/Smart 3rd person camera

This code has been declared by its author to be Public Domain code.

Download source code

Smart 3rd person camera by AntMan - Banned in the line of duty.
(Posted 23 years ago)
Will track an entity and will ensure no geo is obscuring it.

It will only avoid entities that are using a collision pick mode. I suggest 2 for levels. (i.e entityPickMode mesh,2,true)

xoff,yoff,zoff is the offset vector to where the camera should *try* to be. It may not be there, if there's geo in the line of sight between it and the entity.

spd is in a range of 0 to 1. 1=instant.0=never.
type csys.sys
   field cx#,cy#,cz#
   field mx#,my#,sps
end type
Function chaseCam(cam,entity,xoff#,yoff#=2,zoff#=-5,spd#=0.8)
      sys.csys=first csys
      if sys=null 
             sys=new cSys
             sys\sps=createpivot()
      endif
      sys\mx=mousex()
      sys\my=mousey()
sps=sys\sps
PositionEntity sps,EntityX(entity),EntityY(entity),EntityZ(entity)
TFormVector xOff,yOff,zOff,entity,0
ex#=EntityX(entity)
ey#=EntityY(entity)
ez#=EntityZ(entity)
nx#=ex+TFormedX()
ny#=ey+TFormedY()
nz#=ez+TFormedZ()
dx#=nx-ex
dy#=ny-ey
dz#=nz-ez
hit=LinePick(ex,ey,ez,dx,dy,dz,0.2)
If hit
nx=PickedX()
ny=PickedY()
nz=PickedZ()
EndIf
sys\cx=sys\cx+(nx-sys\cx)*spd
sys\cy=sys\cy+(ny-sys\cy)*spd
sys\cz=sys\cz+(nz-sys\cz)*spd
positionEntity cam,sys\cx,sys\cy,sys\cz
pointentity cam,entity,0
End Function