3D Camera Work

Blitz3D Forums/Blitz3D Programming/3D Camera Work

LuckyPhil(Posted 2004) [#1]
Does anyone have any good third person camera routines? While the 'supercam' in the code archives is fine for an empty landscape or game area. When you put objects in front of the player character, or move close to one, the camera doesn't behave very well.

I've spent the wekend trying to come up with one that is very similar to most games out there (tomb raider, neverwinter nights, etc). You know the type that zoom in to the player, or lifts up higher and looks down at a different angle when a player backs into a corner.

I've tried using collisions with the camera and scenery/objects, but I haven't had much luck with it (the camera gets stuck, and the camera movement gest all screwed afterwards.

How do you create a tomb-raider type of camera??


AntonyWells(Posted 2004) [#2]
Here you go, this is a smooth camera, plus it will ensure that no geo obscures the entity, and it's also impossible for it to get 'caught'

Set any geo you want it to avoid to pickable mode 2. (i.e entityPickmode levelMesh,2,true)

spd is in a range of 0 to 1. The closer to 1 it is, the faster the camera will track the entity.
Xoff,yOff,zOff is a vector indicating where the camrea should be *relative* to the playre entity. I.e the default 0,2,-5 will mean it's always 'trying' to be behind and above it. This is needed so that it's still in the right place when your entity turns around or whatever.

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



LuckyPhil(Posted 2004) [#3]
Thanks Otacon,

It is a vast improvement on what I had earlier. However, could you (or someone else) please explain exactly what this fuction is doing?

The function looks very busy (why sys\mx and sys\my)???

Could you please explain the algorithm a little better?

Perhaps I should sleep on it.....


dangerdave(Posted 2004) [#4]
Hi.

sys is type. (container to hold related variables)
So, sys\mx tells Blitz to refer to the mx variable in the sys type.
----------------
the cx,cy,cz hold the 3d positoin (x,y,z) of the camera
the mx,my hold the positon of the mouse
the ex,ey,ez hold the 3d position of the object you are following
the TFormed commands help align the camera.

-----------
There is a pivot in between the camera and the entity.
It is used to make the camera movement smooth and makes
it possible to avoid other objects


Zethrax(Posted 2004) [#5]
You'll also find a decent example of a third person chase cam in Mark Sibly's castle demo.

C:\Program Files\Blitz3D\Samples\Blitz 3D Samples\mak\castle