[bb] camera control in 3rd person by Nate the Great [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:39

Previous topic - Next topic

BlitzBot

Title : camera control in 3rd person
Author : Nate the Great
Posted : 1+ years ago

Description : 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


Code :
Code (blitzbasic) Select
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)(Posted 1+ years ago)

 I'm just wondering, How can I use this? :s


Guy Fawkes(Posted 1+ years ago)

 Like this...
Graphics3D 800, 600, 0, 2


Global cam = CreateCamera()
CameraRange cam, .1, 999999999


AmbientLight 255, 255, 255


Global land = CreatePlane()
EntityColor land, 64, 64, 64

Global type_player
Global type_object

Collisions type_player, type_object, 2, 2

Global player = CreateCube()
PositionEntity player, 0, 1, 10
EntityType player, type_player

For x = 1 To 255
cube = CreateCube()
PositionEntity cube, Rnd(20, 255), 1, Rnd(20, 255)
EntityColor cube, Rnd(0, 255), Rnd(0, 255), Rnd(0, 255)
EntityType cube, type_object
Next

While Not KeyDown(1)
Updatecamera(cam,player,1.0,10.0)
MoveEntity player, 0, 0, (KeyDown(17)-KeyDown(31) Or KeyDown(200)-KeyDown(208))*0.5
TurnEntity player, 0, (KeyDown(30)-KeyDown(32) Or KeyDown(203)-KeyDown(205))*0.5, 0
UpdateWorld()
RenderWorld()
Flip
Wend
End

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)+5 ;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
Although... the collision needs work. I wrote this in about 5 minutes :) [/i]