Code archives/3D Graphics - Maths/camera control in 3rd person

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

Download source code

camera control in 3rd person by Nate the Great2008
This is function of a program I am writing that keeps the camera in a certain range of a character or car without being fixed or chopy enjoy :)

-NTG
Function Updatecamera(cam1,follow,mindist#,maxdist#)  ;This function updates the camera

PointEntity cam1,follow  ;You may need to edit this out depending on your program

x# = EntityX(cam1)
y# = EntityY(cam1)
z# = EntityZ(cam1)

x1# = EntityX(follow)
y1# = EntityY(follow)+10 ;the plus 10 is optional so your camera stays somewhat above your character... change if needed or remove completely
z1# = EntityZ(follow)

dx# = x#-x1#
dy# = y#-y1#
dz# = z#-z1#

dist# = Sqr((dx#*dx#) + (dy#*dy#) + (dz#*dz#))	;distance formula 3d (the reason I 'reinvented the wheel' here is because I needed the x y and z differences for later anyway so it is more efficient to use the variables twice than to have the computer do it for you)

If dist# > maxdist# Then
	fct# = maxdist#/dist#
	dx# = dx#*fct#
	dy# = dy#*fct#
	dz# = dz#*fct#
ElseIf dist# < mindist# Then
	fct# = mindist#/dist#
	dx# = dx#*fct#
	dy# = dy#*fct#
	dz# = dz#*fct#
EndIf

PositionEntity cam1, x1#+dx#,y1#+dy#,z1#+dz#		;This positions the camera where it needs to go.

End Function

Comments

Captain Wicker (crazy hillbilly)2012
I'm just wondering, How can I use this? :s


Guy Fawkes2012
Like this...




Although... the collision needs work. I wrote this in about 5 minutes :)


Code Archives Forum