Simple Camera?

Blitz3D Forums/Blitz3D Beginners Area/Simple Camera?

po(Posted 2004) [#1]
Anyone know a simple way to get the camera to follow your character in 3D? I took a look at the castle demo but I didn't like that camera to much. I just need a camera to follow the character just above it's head and to turn when it turns and jumps, etc. A 3rd person view I guess.
I'm using B3D.


wizzlefish(Posted 2004) [#2]
This is what I did.
Put this near the top:
player=CreateCylinder()
camera=CreateCamera(player)
PositionEntity camera, EntityX(player), EntityY(player)-3, EntityZ(player)-5

And this in your main loop:
PositionEntity camera, EntityX(player), EntityY(player)-3, EntityZ(player)-5


It's not the best way, though.


MSW(Posted 2004) [#3]
There are many ways...parent the camera to the player and such...part of it will depend on how you want the player to control the character.

for example...Do you want direct control where pressing the up arrow (or up on the gamepad/joystick) always makes the character move foreward as in resident Evil games?...or do you want relitive control where pressing up makes the character move toword the top of the screen no matter what direction the camera is faceing?

If its the first way you may want something along the lines of parenting the camera to the character inorder to give a better sense of direct control...else you may want to use a system where the camera is within a certain distance from the character, but free to look at the character without rotateing with them...

basicly American McGees Alice is direct control...while Prince of Pershia:Sands of Time is relative control...direct is easyer, relative can be (but isn't exclusively) more robust in the things that can be done...so which is it?


WolRon(Posted 2004) [#4]
Digital Fortress:
In the code you display, since you parented the camera to the player, there is no need to 'position' the camera in your loop like you show.
The camera will automatically follow since it's parented.


wizzlefish(Posted 2004) [#5]
OK - thanks - I'm still new to parenting :)


Agamer(Posted 2004) [#6]
how old is he/she and you seem a bit young to be a parent, sorry I could not resist.

And yes I agree with wolron you don't need to position so he's wright!


po(Posted 2004) [#7]
MSW: I guess what I want is direct control, I will try Digitals way.


Ross C(Posted 2004) [#8]
Hey po. I find this way good for a camera. Adds a nice smooth effect to the camera. Basically what you do is have a pivot parented to the player. Place the pivot at the height/distance away from the player, you want the camera to be. Then, move the camera towards the pivot, based on the distance between the two, whilst always pointing the camera to face the player. Like so :

Graphics3D 800,600
SetBuffer BackBuffer()

Global player = CreateCube()

Global cam_piv = CreatePivot(player) ; parent the pivot to the player
PositionEntity cam_piv,0,2,-8

Global camera = CreateCamera()

Dim cubes(20) ; create some surroundings
For loop = 0 To 20
   cubes(loop) = CreateCube()
   EntityColor cubes(loop),Rand(0,255),Rand(0,255),Rand(0,255)
   PositionEntity cubes(loop),Rnd(-29,29),0,Rnd(-29,29)
Next

While Not KeyHit(1)


   If KeyDown(200) Then MoveEntity player,0,0,0.1
   If KeyDown(208) Then MoveEntity player,0,0,-0.1
   If KeyDown(203) Then TurnEntity player,0,1,0
   If KeyDown(205) Then TurnEntity player,0,-1,0

   UpdateWorld
   updatecam()
   RenderWorld
   Flip
Wend
End

Function updatecam()

   PointEntity camera,cam_piv
   MoveEntity camera,0,0,EntityDistance#(camera,cam_piv)/50
   PointEntity camera,player

End Function



po(Posted 2004) [#9]
Woah, thanks Ross.
It's amazing such a smooth camera can be made with such little code.


Jeppe Nielsen(Posted 2004) [#10]
Try this, you can also specify where the camera should point at, relative to the entity:
Graphics3D 800,600
SetBuffer BackBuffer()

player = CreateCube()

camera = CreateCamera()

Dim cubes(20) ; create some surroundings
For loop = 0 To 20
   cubes(loop) = CreateCube()
   EntityColor cubes(loop),Rand(0,255),Rand(0,255),Rand(0,255)
   PositionEntity cubes(loop),Rnd(-29,29),0,Rnd(-29,29)
Next

While Not KeyHit(1)

   If KeyDown(200) Then MoveEntity player,0,0,0.1
   If KeyDown(208) Then MoveEntity player,0,0,-0.1
   If KeyDown(203) Then TurnEntity player,0,1,0
   If KeyDown(205) Then TurnEntity player,0,-1,0

;   CameraUpdate(camera,player,0,5,-5,0,-4,0)
   CameraUpdate(camera,player,0,5,-5,0,2,0)
 
   RenderWorld
   Flip
Wend
End

Function CameraUpdate(camera,ent,camx#,camy#,camz#,aimx#=0,aimy#=0,aimz#=0,smoothcam#=0.1,roll#=0)
	
	TFormPoint camx#,camy#,camz#,ent,0
	
	dx#=(TFormedX()-EntityX(camera))*smoothcam#
	dy#=(TFormedY()-EntityY(camera))*smoothcam#
	dz#=(TFormedZ()-EntityZ(camera))*smoothcam#
	
	TranslateEntity camera,dx,dy,dz
	
	TFormPoint aimx#,aimy#,aimz#,ent,0
	
	dx# = EntityX(camera)-TFormedX()
	dy# = EntityY(camera)-TFormedY()
	dz# = EntityZ(camera)-TFormedZ()
	dist#=Sqr#((dx#*dx#)+(dz#*dz#))
	pitch#=ATan2(dy#,dist#)
	yaw#=ATan2(dx#,-dz#)
	
	RotateEntity camera,pitch#,yaw#,roll#
	
End Function