Camera Following object to strictly

Blitz3D Forums/Blitz3D Beginners Area/Camera Following object to strictly

MaximusPrimal(Posted 2008) [#1]
Hi,

I am new to Blitz and have been playing for the last 2 days trying to get something roughly cobbled together from example code which comes with it, so Ican have something to build an idea upon.

I basicaly have a spaceship flying around a randomly created asteriod field where the camera is always directly behind the ship regardless of which of the three axis it moves in. This code was taken from the xpilot demo and re-worked a bit to make it do what I wanted (and follow the ship in the y axis movement which the xpilot demo didn't do).

All fine so far. But when the ship moves in the y (up/down) axis I want there to be a little lag so the camera follows and turns about 1 or 2 seconds behind the ship (which currenttly happens when the ship banks in the z axis and then turns). But I have no real idea who to introduce that time lag.

The code I use at the moment for controling the ship & camera is :


	Local x_dir,y_dir,z_dir

		If KeyDown(203) x_dir=-1
		If KeyDown(205) x_dir=1
		
		If KeyDown(200) y_dir=-1
		If KeyDown(208) y_dir=1
		
		If KeyDown(30) z_dir=1
		If KeyDown(44) z_dir=-1
		
		If KeyHit(59) p\cam_mode=1
		If KeyHit(60) p\cam_mode=2
		If KeyHit(61) p\cam_mode=3
		If KeyHit(62) p\cam_mode=4
		
	
	
	If x_dir<0
		p\yaw_speed=p\yaw_speed + (4-p\yaw_speed)*.04
	Else If x_dir>0
		p\yaw_speed=p\yaw_speed + (-4-p\yaw_speed)*.04
	Else
		p\yaw_speed=p\yaw_speed + (-p\yaw_speed)*.02
	EndIf
		
	If y_dir<0
		p\pitch_speed=p\pitch_speed + (2-p\pitch_speed)*.2
	Else If y_dir>0
		p\pitch_speed=p\pitch_speed + (-2-p\pitch_speed)*.2
	Else
		p\pitch_speed=p\pitch_speed + (-p\pitch_speed)*.1
	EndIf
		
	p\yaw=p\yaw+p\yaw_speed
	If p\yaw<-180 Then p\yaw=p\yaw+360
	If p\yaw>=180 Then p\yaw=p\yaw-360
	
	p\pitch=p\pitch+p\pitch_speed
	If p\pitch<-180 Then p\pitch=p\pitch+360
	If p\pitch>=180 Then p\pitch=p\pitch-360
		
	p\roll=p\yaw_speed*30
	RotateEntity p\entity,p\pitch,p\yaw,p\roll
	
	;see if y/p/r funcs are working...
	t_p#=EntityPitch( p\entity )
	t_y#=EntityYaw( p\entity )
	t_r#=EntityRoll( p\entity )
	RotateEntity p\entity,t_p,t_y,t_r
	
	If p\ignition
		If z_dir>0			;faster?
			p\thrust=p\thrust + (20-p\thrust)*.04	;1.5
		Else If z_dir<0		;slower?
			p\thrust=p\thrust + (-p\thrust)*.04
		EndIf
		MoveEntity p\entity,0,0,p\thrust
	Else If z_dir>0
		p\ignition=True
	EndIf
	
			EntityParent p\camera,p\entity
			RotateEntity p\camera,p\pitch,p\yaw,p\roll,True
			PositionEntity p\camera,EntityX(p\entity),EntityY(p\entity),EntityZ(p\entity),True
			MoveEntity p\camera,0,1,-5
			PointEntity p\camera,p\entity,p\roll/2


As I said this is mostly from the xpilot demo. Can someone tell where I should be looking for the time-lag part. I do not expect the answer, just an idea of where to look and what for so I can try and find the solution as I did with getting the camera to rotate on the x axis (up/down) with the ship so it always behind it.

I hope that made some sense of what I am trying to acheive and understand.

Thanks

Max


mtnhome3d(Posted 2008) [#2]
try this
;***********************************
Function camcontrol(cam)
dx#=(TFormedX()-EntityX(cam))*.08
dy#=(TFormedY()-EntityY(cam))*.01
dz#=(TFormedZ()-EntityZ(cam))*.08

TranslateEntity cam,dx,dy,dz

End Function
;***********************************

and when you update your spaceship
TFormPoint 0,30,-70,p\entity,0
pointentity cam,p\entity

only change the first three numbers to best fit you needs


Moraldi(Posted 2008) [#3]
Take a look at the samples of my lib:
http://www.moraldigames.com/Products.html#Products_MeshFactory
You can have a camera following an object with just a line of code and change many parameters of how to do this.
You can change also how much strictly this will be happen.
This may fit to your needs.

Take a look in this example too
http://www.moraldigames.com/Temp/Test.zip
Drive the spaceship with the arrow keys.
Press [SPACE] to toggle the auto camera mode ON/OFF. When auto camera mode is off you can rotate the view using the keys in NUM PAD (press NUM5 to reset view)
Move forward/backward [A]/[Z]
Move up/down [S]/[X]


jfk EO-11110(Posted 2008) [#4]
Additionaly here's some theory. Instead of having a camera fixed to a certain position relative to the ship, you should use pivot. The camera then has to try to move to the pivots position as good as it gets. There is a very simple algorithm to do that:

cam_x=cam_x + ((piv_x - cam_x)*0.25)

So you see the distance between camera and pivot is divided by 4 (*.25) and added to the current camera position. This means, the camera needs 4 Frames until it reaches the pivots position. Continous motion of the pivot will then result in a smooth lag. You can easily define the lag this way. Of course you do the same with Y and Z, then position the camera at the calculated (aka smoothed or tweened) position every frame.


Moraldi(Posted 2008) [#5]
Actually this is what I am using in my spaceship example above.
Academically jfk EO-1110's camera will NEVER reach to pivot's position because will always move 0.25*REMAINING distance... :)


jfk EO-11110(Posted 2008) [#6]
You're right. This may even be an issue with render-on-demand optimations. So when the distance reaches a certain minimum you may simply put the camera to the pivots exact position.