Camera behind a moving object (tomb raider style)

Blitz3D Forums/Blitz3D Programming/Camera behind a moving object (tomb raider style)

Here's what I use, it's very simple and looks nice:

; Change -100 to whatever distance behind your character you want
; Change -30 to whatever distance above your character you want
; Change Player to the name of your player character
; Also change 'Cam' to the name of your Camera
TFormPoint 0, -30, -100, Player, 0
cx# = CurveValue(EntityX#(Cam), TFormedX#(), 15.0)
cy# = CurveValue(EntityY#(Cam), TFormedY#(), 15.0)
cz# = CurveValue(EntityZ#(Cam), TFormedZ#(), 15.0)
PositionEntity Cam, cx#, cy#, cz#
PointEntity Cam, Player

; Put this function at the bottom of your code
Function CurveValue(Current#, Destination#, Speed#)
  Return Current# + ((Destination# - Current#) / Speed#)
End Function


Here's all that in one general purpose function you can call to make any camera follow any object at any distance:

Function CameraFollow(Cam, EN, YD# = 0, ZD# = 0, Speed# = 15.0)
  TFormPoint 0, -YD#, -ZD#, EN, 0
  cx# = cx# + ((cx# - TFormedX#()) / Speed#)
  cy# = cy# + ((cy# - TFormedY#()) / Speed#)
  cz# = cz# + ((cz# - TFormedZ#()) / Speed#)
  PositionEntity Cam, cx#, cy#, cz#
  PointEntity Cam, EN
End Function


So to use it just stick that function at the bottom of your code and do something like:

CameraFollow(MyCam, MyPlayer, -30, -100)


Just set up sliding collision between the camera and the level so the camera doesn't go through walls.