Space Ship - Controls

Blitz3D Forums/Blitz3D Beginners Area/Space Ship - Controls

nrasool(Posted 2006) [#1]
Hi there, Just playing around with Blitz3d and controling a space ship around. I seen a previous example from Viper, see http://www.blitzbasic.com/Community/posts.php?topic=43314

But would love to know about turning the ship around

Viper written in his code the following for left and right:

If KeyDown(203);z rotation(roll)
		If zrotation < 70 Then
			zrotation = zrotation + rollspeed#
			If zrotation < 0 Then
				zrotation = zrotation / 1.1
			End If
		End If
	Else 
		If KeyDown(205)
			If zrotation > -70
				zrotation = zrotation - rollspeed#
				If zrotation > 0 Then
					zrotation = zrotation / 1.1
				End If
			End If
		Else 
			If zrotation <> 0 Then
				zrotation = zrotation / 1.1
			End If
		End If
	EndIf


I was thinking about putting a new variable within MoveShip(xrotation,zrotation), so it would be

MoveShip(xaxis,xrotation,zrotation)

Therefore if i had the code written as:
	If KeyDown(203) Then
		If player\zrotation < 70 Then
			player\zrotation = player\zrotation + rollspeed#
			If player\zrotation < 0 Then
				player\zrotation = player\zrotation / 1.1
			End If
		End If
		player\xaxis=player\xaxis+0.01
	EndIf 
It would make the model go left. But alas it is not working.

Please could someone help

Kind Regards


RiverRatt(Posted 2006) [#2]
You should post your entire code. It's hard to tell whats wrong with fragments. The problem could be anywhere. Also What's not working? Are you getting a debuging notice/message? Are you declaring xrotation,zrotation as floats? Becouse if not they will just remain 0 or null when you try to add a floating value (ie 1.1).


nrasool(Posted 2006) [#3]
Hi RiverRatt, thanks for replying. The easiest thing to do is to copy and paste the following code:

Graphics3D 1024,768,16,1
SetBuffer BackBuffer()

Const climbspeed# = 1, rollspeed# = 2

Global xrotation#,zrotation#,speed#
Global camera% = CreateCamera()

Global plane% = CreatePlane()
Global planetex% = LoadTexture("planettextures/ground4.bmp");Replace with your own texture
ScaleTexture planetex,10,10
EntityTexture plane,planetex

Global ship = LoadMesh("fighter5.3ds");replace with your own mesh
PositionEntity ship,0,100,0 

While Not KeyDown(1)
	speed# = 1
	FlightControls()
	MoveShip(xrotation,zrotation)
	MoveCamera()
	RenderWorld
	Flip
Wend
End

Function MoveShip(xrotate#,zrotate#)
	roll#=rollspeed*Sin(zrotate#)		
	pitch#=climbspeed*Sin(xrotate)
	
	TurnEntity ship,pitch#,0,roll#
	MoveEntity ship,0,0,speed#
End Function

Function MoveCamera()
	;I believe there may be a better way to write the following code using TFORMPOINT, but 
	;this works really well.
	EntityParent(camera,ship,True)
	dx#=EntityX(camera)
	dy#=EntityY(camera)
	dz#=EntityZ(camera)
	r#=EntityRoll(camera)
	p#=EntityPitch(camera)
	y#=EntityYaw(camera)
	;The following 4 lines cause the camera to follow just behind the ship(30 behind, 2 up).
	;Pitch roll and yaw are also just behind the ship giving your camera a super smooth action.
	If r# => 180 Then r# = r# - 360
	If r# =< -180 Then r# = r# + 360
	TurnEntity camera,-p#,-y#,-r#*(.06)
	TranslateEntity camera,-dx*.1,(-dy*.1)+2,(-dz)-30
	EntityParent(camera,0)
End Function

Function FlightControls()
	;The xrotation and yrotation values are passed to the MoveShip function which
	;use the sine wave to give you smooth movement. It is very important these
	;values never exceed 90. But 70 is sufficient.
	If KeyDown(200);x rotation(pitch)
		If xrotation < 70 Then
			xrotation = xrotation + climbspeed#
			If xrotation < 0 Then
				xrotation = xrotation / 1.1
				;If you use Delta timing then the above line should
				;read  "xrotation = xrotation / 1.1 ^ FL\SpeedFactor"
				;note the (^) not (*)
			End If
		End If
	Else 
		If KeyDown(208)
			If xrotation > -70
				xrotation = xrotation -climbspeed#
				If xrotation > 0 Then
					xrotation = xrotation / 1.1
				End If
			End If
		Else 
			If xrotation <> 0 Then
				xrotation = xrotation / 1.1
			End If
		End If
	EndIf
	
	If KeyDown(203);z rotation(roll)
		If zrotation < 70 Then
			zrotation = zrotation + rollspeed#
			If zrotation < 0 Then
				zrotation = zrotation / 1.1
			End If
		End If
	Else 
		If KeyDown(205)
			If zrotation > -70
				zrotation = zrotation - rollspeed#
				If zrotation > 0 Then
					zrotation = zrotation / 1.1
				End If
			End If
		Else 
			If zrotation <> 0 Then
				zrotation = zrotation / 1.1
			End If
		End If
	EndIf
End Function


This is Viperfish code, now when you run that, bearing in mind to add your own texture and ship, if you click on left cursor, you will see the ship rolls to the left. What I want to achieve is to make the ship turn left, basically bank left. (Hope that makes some sort of sense?)

I have checked the sample xfighter.bb code which Matty had programmed in Blitz3d, but couldn't see a way of adapting that to this code

Any help would be greatly appreciated :)


nrasool(Posted 2006) [#4]
*Bump*

Sorry to bump this, but if anyone could help it would be greatly appreciated


RiverRatt(Posted 2006) [#5]
I played with it a little bit. I 99 persent sure this is not what you want, but it might give you something to experiment with. I simply added in yrotation for a yaw vallue. all my changes are marked with a :+++. I might play with it som more later. I think it might help me with a problem I am having too.

Graphics3D 1024,768,16,1
SetBuffer BackBuffer()

Const climbspeed# = 1, rollspeed# = 2 , yawspeed# = .1

Global xrotation#,zrotation#,speed#,yrotation#
Global camera% = CreateCamera()

Global plane% = CreatePlane()
Global planetex% = LoadTexture("rock_01.png");Replace with your own texture
ScaleTexture planetex,10,10
EntityTexture plane,planetex

Global ship = LoadMesh("Data\Rat_Hunter.b3d");replace with your own mesh
RotateMesh ship,0,180,0 ;Riverratt added +++
PositionEntity ship,0,100,0

While Not KeyDown(1)
speed# = 1
FlightControls()
MoveShip(xrotation,zrotation,yrotation)
MoveCamera()
RenderWorld
Flip
Wend
End

Function MoveShip(xrotate#,zrotate#,yrotate#)
roll#=rollspeed*Sin(zrotate#)
pitch#=climbspeed*Sin(xrotate)
;yaw#=turnspeed*Sin(yrotate#)+(roll#*2)-(2*pitch#);+++
yaw#=(yawspeed*Sin(yrotate#)+roll#)-pitch#

TurnEntity ship,pitch#,yaw#,roll#
MoveEntity ship,0,0,speed#

End Function

Function MoveCamera()
;I believe there may be a better way to write the following code using TFORMPOINT, but
;this works really well.
EntityParent(camera,ship,True)
dx#=EntityX(camera)
dy#=EntityY(camera)
dz#=EntityZ(camera)
r#=EntityRoll(camera)
p#=EntityPitch(camera)
y#=EntityYaw(camera)
;The following 4 lines cause the camera to follow just behind the ship(30 behind, 2 up).
;Pitch roll and yaw are also just behind the ship giving your camera a super smooth action.
If r# => 180 Then r# = r# - 360
If r# =< -180 Then r# = r# + 360
TurnEntity camera,-p#,-y#,-r#*(.06)
TranslateEntity camera,-dx*.1,(-dy*.1)+2,(-dz)-30
EntityParent(camera,0)
End Function

Function FlightControls()
;The xrotation and yrotation values are passed to the MoveShip function which
;use the sine wave to give you smooth movement. It is very important these
;values never exceed 90. But 70 is sufficient.
If KeyDown(200);x rotation(pitch)
If xrotation < 70 Then
xrotation = xrotation + climbspeed#
If xrotation < 0 Then
xrotation = xrotation / 1.1
;If you use Delta timing then the above line should
;read "xrotation = xrotation / 1.1 ^ FL\SpeedFactor"
;note the (^) not (*)
End If
End If
Else
If KeyDown(208)
If xrotation > -70
xrotation = xrotation -climbspeed#
If xrotation > 0 Then
xrotation = xrotation / 1.1
End If
End If
Else
If xrotation <> 0 Then
xrotation = xrotation / 1.1
End If
End If
EndIf

If KeyDown(203);z rotation(roll)
If zrotation < 70 Then
zrotation = zrotation + rollspeed#
yrotation = yrotation + yawspeed# ;+++
If yrotation < 0 Then
yrotation = yrotation / 1.1
End If

If zrotation < 0 Then
zrotation = zrotation / 1.1
End If
End If
Else
If KeyDown(205)
If zrotation > -70
zrotation = zrotation - rollspeed#
yrotation = yrotation - yawspeed# ;+++

If yrotation > 0 Then
yrotation = yrotation / 1.1
End If

If zrotation > 0 Then
zrotation = zrotation / 1.1
End If
End If
Else
If zrotation <> 0 Then
zrotation = zrotation / 1.1
End If
End If
EndIf
End Function


nrasool(Posted 2006) [#6]
Hi RiverRatt, Thanks for the code, fraid you are right, I just want the ship to turn left. I have played with the code a bit, and see the following

If KeyDown(203);z rotation(roll)
If zrotation < 70 Then
zrotation = zrotation + rollspeed#
yrotation = yrotation + yawspeed# ;+++
TurnEntity ship,-1,0,0 ;+++ Here turning the ship left
If yrotation < 0 Then
yrotation = yrotation / 1.1
End If


Tried the above, and it does a bit, except when you press left, it goes left, however you have to press down then to turn it all the way to the left

Will still have a play around as well :)


RiverRatt(Posted 2006) [#7]
To avoid that I think you have to seperate the code for turning left by adding another keypress conditional ie
if key down control and keydown left/right arrow then change yawrotation to turn left. Or you could just get rid of the roll code. The problem with the code I posted is that the ship is turning by its local oreintation not global. You might also try to use rotateentity in in place of turnentity. That way the ship is directed by global orientation, Rotateentity ship,anglex,angley,anglez each frame.
So to turn the ship left you would say
If keydown(leftkey)angley=angley+1
Rotateentity ship,anglex,angley,anglez


nrasool(Posted 2006) [#8]
Ahhhh good point RiverRatt, I will try that and see, that sounds like the right solution :)