Home
Products
Community
Manuals
Contact
Login or Signup

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 AntonyWells(Posted 1+ 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

Comments

AntonyWells(Posted 1+ years ago)
SimonH, you need to sort the archives out...half the time they don't work, and even when they do things like this happen..(Formatting screwed up.)


AntonyWells(Posted 1+ years ago)
Double post. (And this is another thing..adding comments nearly always hangs the browser. This doesn't happen on the regular forums so I doubt it's anything else)


aCiD2(Posted 1+ years ago)
Very impressive Otacon!! For those who aren't sure how to use it: here's an example
; ID: 1083
; Author: Otacon
; Date: 2004-06-13 04:37:46
; Title: Smart 3rd person camera
; Description: Follows a entity and avoids geo.

Type CSys

   Field cx#, cy#, cz#
   Field mx#, my#, sps%
   
End Type

Function ChaseCam(cam, Entity, XOff#, YOff# = 2, ZOff# = -5, Spd# = 0.8)

	Local sys.csys = First csys
	Local nx#, ny#, nz#
	Local dx#, dy#, dz#
	Local ex#, ey#, ez#
	Local hit%
	
	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


Graphics3D 800, 600, 0, 2
SetBuffer BackBuffer()

Global cube = CreateCube()
Global cam = CreateCamera()
Global ops = CreateCube()
ScaleEntity ops, 10, 10, 1
	MoveEntity ops, 0, 0, -5
	EntityPickMode ops, 2

While Not KeyHit(1)
	
	Cls
	
	ChaseCam(cam, cube, 0, 2, -20, .1)
	
	MoveEntity ops, KeyDown(205) - KeyDown(203), 0, 0
	
	UpdateWorld
	RenderWorld
	
	Flip
	
Wend



Jeroen(Posted 1+ years ago)
aCid2: your demonstration made it less clear, not more :0


jfk EO-11110(Posted 1+ years ago)
We disussed such a smart camera some time ago in theory. So thanks for posting a working example. Very useful.


GC-Martijn(Posted 1+ years ago)
@The Dude
The first good / stable / short code camerafollow function !
its working very cool :)

When I found a problem I mail you ! ;)


AntonyWells(Posted 1+ years ago)
Glad you guys found it useful.

(Puts out hand in expectation of brown envelope)


Caff(Posted 1+ years ago)
Thanks a lot, nice bit of code.

(Hands over empty brown envelope) ;)


AntonyWells(Posted 1+ years ago)
Excellent, excellent...

Now, bring me a stamp!


AntonyWells(Posted 1+ years ago)
dp.


Sanosake1(Posted 1+ years ago)
type csys.sys
; ID: 1083
; Author: Otacon
; Date: 2004-06-13 04:37:46
; Title: Smart 3rd person camera
; Description: Follows a entity and avoids geo.

Type CSys

Field cx#, cy#, cz#
Field mx#, my#, sps%

End Type

Function ChaseCam(cam, Entity, XOff#, YOff# = 2, ZOff# = -5, Spd# = 0.8)

Local sys.CSys = First CSys
Local nx#, ny#, nz#
Local dx#, dy#, dz#
Local ex#, ey#, ez#
Local hit%

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


Graphics3D 800, 600, 0, 2
SetBuffer BackBuffer()


plane=CreatePlane()
Global cube = CreateCube()



Global cam = CreateCamera()
Global ops = CreateCube()
ScaleEntity ops, 10, 10, 1
MoveEntity ops, 0, 0, -5
EntityPickMode ops, 2


;[Block]
; all of this is used to make pretty grids to help you see what's happening
EntityColor plane,25,100,150
PositionEntity plane ,x,-20,0
grid_tex2=CreateTexture( 32,32,8 )
ScaleTexture grid_tex2,10,10
SetBuffer TextureBuffer( grid_tex2 )
Color 0,0,0:Rect 0,0,32,32
Color 0,255,0:Rect 0,0,32,32,False
EntityTexture plane,grid_tex2



grid_tex1=CreateTexture( 32,32,8 )
ScaleTexture grid_tex1,.1,.2
SetBuffer TextureBuffer( grid_tex1 )
Color 0,0,255:Rect 0,0,32,32
Color 255,255,0:Rect 0,0,32,32,False
EntityTexture cube,grid_tex1

SetBuffer BackBuffer()
;[End Block]



While Not KeyHit(1)

Cls

ChaseCam(cam, cube, 0, 2, -20, .1)

MoveEntity ops, KeyDown(205) - KeyDown(203), 0, 0

UpdateWorld
RenderWorld

Flip

Wend


Code Archives Forum