November 28, 2020, 01:41:47 AM

Author Topic: [bb] camera control in 3rd person by Nate the Great [ 1+ years ago ]  (Read 716 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
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
  1. Function Updatecamera(cam1,follow,mindist#,maxdist#)  ;This function updates the camera
  2.  
  3. PointEntity cam1,follow  ;You may need to edit this out depending on your program
  4.  
  5. x# = EntityX(cam1)
  6. y# = EntityY(cam1)
  7. z# = EntityZ(cam1)
  8.  
  9. x1# = EntityX(follow)
  10. y1# = EntityY(follow)+10 ;the plus 10 is optional so your camera stays somewhat above your character... change if needed or remove completely
  11. z1# = EntityZ(follow)
  12.  
  13. dx# = x#-x1#
  14. dy# = y#-y1#
  15. dz# = z#-z1#
  16.  
  17. 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)
  18.  
  19. If dist# > maxdist# Then
  20.         fct# = maxdist#/dist#
  21.         dx# = dx#*fct#
  22.         dy# = dy#*fct#
  23.         dz# = dz#*fct#
  24. ElseIf dist# < mindist# Then
  25.         fct# = mindist#/dist#
  26.         dx# = dx#*fct#
  27.         dy# = dy#*fct#
  28.         dz# = dz#*fct#
  29. EndIf
  30.  
  31. PositionEntity cam1, x1#+dx#,y1#+dy#,z1#+dz#            ;This positions the camera where it needs to go.
  32.  
  33. 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...
Code: [Select]
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]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal