Airplane movement

Blitz3D Forums/Blitz3D Beginners Area/Airplane movement

ingenium(Posted 2006) [#1]
I want to move an airplane (I can't move it as a car). What's the best way? Have I to manipulate vectors?


puki(Posted 2006) [#2]
In your Blitz3D 3D samples directory - look for the "mak" directory and you should have a folder called xfighter.

Should you not have it, it contains this function to fly a bi-plane:

Function UpdatePlayer( p.Player )

	Local x_dir,y_dir,z_dir

	Select p\ctrl_mode
	Case 1:
		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
		
	Case 2:
		x_dir=JoyXDir()
		y_dir=JoyYDir()
		If JoyDown(1) z_dir=1
		If JoyDown(2) z_dir=-1
		
		If KeyHit(63) p\cam_mode=1
		If KeyHit(64) p\cam_mode=2
		If KeyHit(65) p\cam_mode=3
		If KeyHit(66) p\cam_mode=4
		
	End Select
	
	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 + (1.5-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
	
	If p\camera
		Select p\cam_mode
		Case 1:
			EntityParent p\camera,p\entity
			RotateEntity p\camera,0,p\yaw,0,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
		Case 2:
			EntityParent p\camera,0
			PositionEntity p\camera,EntityX(p\entity),EntityY(p\entity),EntityZ(p\entity)
			TranslateEntity p\camera,0,1,-5
			PointEntity p\camera,p\entity,0
		Case 3:
			EntityParent p\camera,p\entity
			PositionEntity p\camera,0,.25,0
			RotateEntity p\camera,0,0,0
		Case 4:
			EntityParent p\camera,0
			PointEntity p\camera,p\entity,0
		End Select
	EndIf
	
End Function



Ross C(Posted 2006) [#3]
If you want to start simple, have a speed variable, and add 1 to it if the speed up key is pressed, or subtract 1 if the slowdown/reverse key is pressed. Make use of the move entity and rotateentity commands. They will calculate all the vector stuff for you :o)

TIP:

Use RotateEntity instead of TurnEntity. It rotates a better way.

RotateEntity plane , EntityPitch(plane) + XROT , EntityYaw(plane) + YROT , EntityRoll(plane) + ZROT


XROt, YROT and ZROT are the amount you wish to turn on each axis :o)


Buggy(Posted 2006) [#4]
Why is RotateEntity better, Ross? Just wondering, as I use TurnEntity for all of my things.


Ross C(Posted 2006) [#5]
Well, if you turn entity along the x axis then the y, it's more of a global rotation.


Pongo(Posted 2006) [#6]
Rotate entity isn't any better, just different.

RotateEntity will rotate to an exact rotation.
TurnEntity will rotate based on the current orientation.

In the example above, you could use turnentity instead.

TurnEntity plane,XROT,YROT,ZROT


Edit: yea on the global rotation thing. It can be pretty usefull when you need to rotate on an exact axis and your object is in a strange orientation.

I have a different approach that I use, which comes from my animation background. I use muliple pivots and then parent them together. Then I can rotate each pivot on a single axis only. This is a bit convoluted, but it prevents gimbal lock situations and works very well.


Ross C(Posted 2006) [#7]
It rotates the entity differently. Turnentity is fine if you only want to turn your entity along the Y axis. It's pretty much useless for multiaxis turning i say. Try the examples below.

ROTATEENTITY

Graphics3D 800,600
SetBuffer BackBuffer()


Global camera = CreateCamera()
PositionEntity camera,0,2,-10

Global light = CreateLight()

Global cube = CreateCube()
ScaleEntity cube,1,1,5


While Not KeyHit(1)


	If KeyDown(200) Then RotateEntity cube,EntityPitch(cube)+1,EntityYaw(cube),EntityRoll(cube)
	If KeyDown(208) Then RotateEntity cube,EntityPitch(cube)-1,EntityYaw(cube),EntityRoll(cube)
	If KeyDown(203) Then RotateEntity cube,EntityPitch(cube),EntityYaw(cube)+1,EntityRoll(cube)
	If KeyDown(205) Then RotateEntity cube,EntityPitch(cube),EntityYaw(cube)-1,EntityRoll(cube)
	
	UpdateWorld
	RenderWorld
	Flip
Wend
End


TURNENTITY

Graphics3D 800,600
SetBuffer BackBuffer()


Global camera = CreateCamera()
PositionEntity camera,0,2,-10

Global light = CreateLight()

Global cube = CreateCube()
ScaleEntity cube,1,1,5


While Not KeyHit(1)


	If KeyDown(200) Then TurnEntity cube,1,0,0
	If KeyDown(208) Then TurnEntity cube,-1,0,0
	If KeyDown(203) Then TurnEntity cube,0,1,0
	If KeyDown(205) Then TurnEntity cube,0,-1,0
	
	UpdateWorld
	RenderWorld
	Flip
Wend
End



Pongo(Posted 2006) [#8]
This is kinda funny,... we are saying pretty much the same exact thing, but our opinions of each are the exact opposite!

In those examples above, I like the turnentity example much better because the rotations are always correct in relation to the local object axis. (if you were sitting on the object the controls would always be consistent) If it were an airplane, yaw pitch and roll would always be yaw, pitch and roll.

In the rotateentity example though, the orientation gets a bit messy and things change. For example it starts out with control for pitch and yaw, but if you rotate it to a vertical position, the yaw becomes roll instead.

I gotta see if I can find my heli code somewhere, I had done a bunch of stuff for a basic heli trainer a while back.


Stevie G(Posted 2006) [#9]
I agree that you need to be very careful of your use of rotateentity for the gimbal lock reasons Pongo mentioned. Turnentity or aligntovector use quarternions internally so avoid this issue.

Using a pivot for separate axis is also a good idea as it gives you more control but it really depends on how realistic you want the movement.

Stevie


ingenium(Posted 2006) [#10]
thnx to all


Ross C(Posted 2006) [#11]
Yeah, i suppose the use of each is different, but i find me doing most of my rotations globally. I think if you were controlling a plane and wanted to adjust the Y axis rotation, and the x axis had already been rotated upon, it would give you the desured turning effect for a plane. But i fully take your other points :o)