Tokamak space ship

Miscellaneous Forums/Blitz Showcase/Tokamak space ship

I played around with turbulence today, and the results are actually surprisingly nice.
I also added a basic chase camera from the code archives. (SuperCam, by PsychicParrot).
It doesn't work perfectly for this program, but it doesn't really matter since it's just a horribly coded test.

;3d space flight engine v 3
;now that I have proper physics, I think it may work out
;I'm aiming to fix the problem involving it just not 'feeling' right, which is very hard to fix with conventional control systems

;includes
;the dust clouds effect
Include "dustclouds.bb"

Const TurbulencePower=2 ;You can change this

;the fog type
Type snow
	Field entity
	Field x#,y#,z#
	Field vx#,vy#,vz#
End Type

;the type instance for the centrepoint() function
Type point
   Field x#,y#,z# 
   Field lx#,ly#,lz#
   Field rx#,ry#,rz#
End Type

;set up graphics
Graphics3D 1024,768,0,2

;double buffering
SetBuffer BackBuffer()

SeedRnd MilliSecs()


;set up tokamak simulator

TOKSIM_SetRigidBodiesCount 2
TOKSIM_SetAnimatedBodiesCount 0
TOKSIM_SetRigidParticleCount 0
TOKSIM_SetControllersCount 0
TOKSIM_SetGeometriesCount 16
TOKSIM_SetOverlappedPairsCount 16

TOKSIM_CreateSimulator(0,0,0)

;the camera
;Global cam1 = CreateCamera()

Global campivot=CreatePivot() ; create pivot for camera
Global cam1=CreateCamera()  ; create camera (!!!)
CameraViewport cam1,0,0,GraphicsWidth(),GraphicsHeight()
PositionEntity cam1,0,0,-10

;load the ship(s)
;NOTE: CHANGE THIS TO AN ARRAY OR SOMETHING!!
Global Ship01=LoadMesh("media\models\ship01\Fighter1.3ds")
MoveEntity ship01,-0.65,0.6,0.35
Global CamTarget=CreatePivot(ship01)
Global Ship01_2=CopyEntity(ship01)
MoveEntity ship01_2,-0.65,0.6,0.35

;playing with klaas's fog effect
CameraClsColor cam1,5,5,35
CameraFogColor cam1,5,5,35
CameraFogMode cam1,1
CameraFogRange cam1,-10,200

;cameracone for the topview
cone = CreateCone(12,1,cam1)
EntityColor(cone,255,0,0)
TurnEntity cone,-90,0,0

Global x_speed#,y_speed#,z_speed#

;------ load the snowflake
extractdata("snow.jpg")
Global snow = LoadSprite("snow.jpg",2)
ScaleSprite(snow,0.4,0.4)
ScaleSprite(snow,16,16)
EntityColor(snow,10,10,10)
EntityAlpha snow,0.2
DeleteFile("snow.jpg")
HideEntity(snow)
SpriteViewMode(snow,4)

;topview camera
topview = CreateCamera(ml_cam_pivot)
CameraViewport topview,10,10,320,240
CameraProjMode(topview,2)
CameraZoom(topview,0.01)
MoveEntity(topview,0,10,10)
TurnEntity(topview,90,0,0)
HideEntity topview

;----- the dimension of the weather box
Global weather_x# = 40
Global weather_y# = 35
Global weather_z# = 50
Global weather_nearfade = 30
createSnow(40)

;the lights
AmbientLight 100,100,100

;render tweening timer / constants / initial variables
Const FPS=60
period=1000/FPS
time=MilliSecs()-period


;APOE Physics Editor 0.1; output for Fighter1.3ds
ship01rb=TOKRB_Create()
TOKRB_SetPosition ship01rb,0,5,0 ;For this version of APOE, you must set positions manually
TOKRB_SetRotation ship01rb,0,0,0 ;For this version of APOE, you must set positions manually
TOKRB_SetLinearDamping ship01rb,0.05
TOKRB_SetAngularDamping ship01rb,0.07
TOKRB_CollideConnected ship01rb,0
TOKRB_SetMass ship01rb,10.0
TOKRB_SetBoxInertiaTensor ship01rb,5.0,5.0,5.0,10.0

Geom_4=TOKRB_AddBox(ship01rb,0.346159,2.11598,3.468)
TOKGEOM_SetPositionAndRotation Geom_4,6.14357,0.0191566,-2.392,0.0,0.0,0.0
;TOKGEOM_SetMaterialIndex Geom_4,3
Geom_6=TOKRB_AddCylinder(ship01rb,0.430949,5.25123)
TOKGEOM_SetPositionAndRotation Geom_6,6.10232,0.0140017,-1.35361,0.0,0.0,0.0
;TOKGEOM_SetMaterialIndex Geom_6,2
Geom_3=TOKRB_AddBox(ship01rb,3.41733,3.28844,6.02556)
TOKGEOM_SetPositionAndRotation Geom_3,-0.0511686,0.0645195,-1.793,0.0,0.0,0.0
;TOKGEOM_SetMaterialIndex Geom_3,3
Geom_5=TOKRB_AddBox(ship01rb,0.346159,2.11598,3.468)
TOKGEOM_SetPositionAndRotation Geom_5,-6.22443,0.0194947,-2.392,0.0,0.0,0.0
;TOKGEOM_SetMaterialIndex Geom_5,3
Geom_7=TOKRB_AddCylinder(ship01rb,0.448,5.25123)
TOKGEOM_SetPositionAndRotation Geom_7,-6.25,0.0140017,-1.35361,0.0,0.0,0.0
;TOKGEOM_SetMaterialIndex Geom_7,2
cHull = LoadConvex("media\models\ship01\RB_CHull_1.bin")
Geom_2=TOKRB_AddConvex(ship01rb,cHull,BankSize(cHull))
TOKGEOM_SetPositionAndRotation Geom_2,-3.95875,0.0196976,-2.448,0.0,0.0,0.0
;TOKGEOM_SetMaterialIndex Geom_2,3
Geom_8=TOKRB_AddBox(ship01rb,3.06407,2.98516,7.30887)
TOKGEOM_SetPositionAndRotation Geom_8,0.0137982,0.667202,4.72569,0.0,0.0,0.0
;TOKGEOM_SetMaterialIndex Geom_8,1
cHull = LoadConvex("media\models\ship01\RB_CHull_0.bin")
Geom_1=TOKRB_AddConvex(ship01rb,cHull,BankSize(cHull))
TOKGEOM_SetPositionAndRotation Geom_1,3.8196,0.0196976,-2.448,0.0,0.0,0.0
;TOKGEOM_SetMaterialIndex Geom_1,3

TOKRB_SetCollisionID ship01rb,1
TOKRB_UpdateBoundingInfo ship01rb

;a ship to smash into.... Lazy me....
ship01_2rb=TOKRB_Create()
	TOKRB_SetPosition ship01_2rb,10.0,8.0,10.0
	TOKRB_SetRotation ship01_2rb,10.0,5.0,40.0
	TOKRB_SetLinearDamping ship01_2rb,0.05
	TOKRB_SetAngularDamping ship01_2rb,0.07
	TOKRB_CollideConnected ship01_2rb,0
	TOKRB_SetMass ship01_2rb,10.0
	TOKRB_SetBoxInertiaTensor ship01_2rb,5.0,5.0,5.0,10.0

Geom_4=TOKRB_AddBox(ship01_2rb,0.346159,2.11598,3.468)
TOKGEOM_SetPositionAndRotation Geom_4,6.14357,0.0191566,-2.392,0.0,0.0,0.0
;TOKGEOM_SetMaterialIndex Geom_4,3
Geom_6=TOKRB_AddCylinder(ship01_2rb,0.430949,5.25123)
TOKGEOM_SetPositionAndRotation Geom_6,6.10232,0.0140017,-1.35361,0.0,0.0,0.0
;TOKGEOM_SetMaterialIndex Geom_6,2
Geom_3=TOKRB_AddBox(ship01_2rb,3.41733,3.28844,6.02556)
TOKGEOM_SetPositionAndRotation Geom_3,-0.0511686,0.0645195,-1.793,0.0,0.0,0.0
;TOKGEOM_SetMaterialIndex Geom_3,3
Geom_5=TOKRB_AddBox(ship01_2rb,0.346159,2.11598,3.468)
TOKGEOM_SetPositionAndRotation Geom_5,-6.22443,0.0194947,-2.392,0.0,0.0,0.0
;TOKGEOM_SetMaterialIndex Geom_5,3
Geom_7=TOKRB_AddCylinder(ship01_2rb,0.448,5.25123)
TOKGEOM_SetPositionAndRotation Geom_7,-6.25,0.0140017,-1.35361,0.0,0.0,0.0
;TOKGEOM_SetMaterialIndex Geom_7,2
cHull = LoadConvex("media\models\ship01\RB_CHull_1.bin")
Geom_2=TOKRB_AddConvex(ship01_2rb,cHull,BankSize(cHull))
TOKGEOM_SetPositionAndRotation Geom_2,-3.95875,0.0196976,-2.448,0.0,0.0,0.0
;TOKGEOM_SetMaterialIndex Geom_2,3
Geom_8=TOKRB_AddBox(ship01_2rb,3.06407,2.98516,7.30887)
TOKGEOM_SetPositionAndRotation Geom_8,0.0137982,0.667202,4.72569,0.0,0.0,0.0
;TOKGEOM_SetMaterialIndex Geom_8,1
cHull = LoadConvex("media\models\ship01\RB_CHull_0.bin")
Geom_1=TOKRB_AddConvex(ship01_2rb,cHull,BankSize(cHull))
TOKGEOM_SetPositionAndRotation Geom_1,3.8196,0.0196976,-2.448,0.0,0.0,0.0				

TOKRB_SetCollisionID ship01_2rb,1
TOKRB_UpdateBoundingInfo ship01_2rb

;collision reponse for the player's ship with the dummy ship
;TOKSIM_SetCollisionResponse 1,1,3

;um, let's hope I just set up the scaling wrong here...
;TO DO: I THINK THE FRONT OF THE SHIP'S RIGID BODY SETUP IS WRONG
;TRY USING A DIFFERENT COLLISION SYSTEM ALONG WITH TOKRB_ApplyImpulse2
			
;the acceleration variables
;NOTE: PUT THRUST INTO THE SHIP01 ARRAY/Type
thrust = 0

;arrays for engine's positions (see top of "rotation stuff" section)
;the port wing engine
Dim PortEngine#(2)

;the starboard wing engine
Dim StarboardEngine#(2)

;the top back engine
Dim BackTopEngine#(2)

;the bottom back engine
Dim BackBottomEngine#(2)

;main loop
While Not KeyDown(1)

	;temporary fps counter
	Start = MilliSecs()

	Repeat
		elapsed=MilliSecs()-time
	Until elapsed
	ticks=elapsed/period	;how many 'frames' have elapsed	
	tween#=Float(elapsed Mod period)/Float(period)	;fractional remainder
	For k=1 To ticks
		time=time+period
		If k=ticks Then CaptureWorld

		;;;;;;;;;;;;;;CONTENT HERE;;;;;;;;;;;;;;;;;;;;
		
		;;;;;;;;;;;;;;MOVEMENT RELATED STUFF STILL TO DO;;;;;;;;;;;;;;;;
		;shaking the ship at random
		;ability to cut flow from certain engines
				
		;;;acceleration stuff;;;

		; if the + key is pressed
	
		If KeyDown(78)
	
		;if not exeeding max speed  NOTE: WHEN YOU SET UP AN ARRAY/TYPE INSTANCE FOR THE SHIP, MAKE MAX SPEED A VARIABLE
			If thrust<1000
		
			;speed up		
			thrust = thrust + 2
		
			EndIf
		EndIf
	
		;if the - key is pressed
	
		If KeyDown(74)
	
			If thrust>-800 ; max speed check
		
				;slow down
				thrust = thrust - 2
			EndIf	
		EndIf
	
		;if the "0" key on the number line (not num pad) is pressed
		If KeyDown(11)
			thrust = 0 ;stop engines
		EndIf
		
		;if the backspace key is pressed
		If KeyDown(14)
			thrust = 1000 ;imediately go to maximum speed
		EndIf
	
		;actually move	
		
		;first TformVector on the ship entity to get the right movement direction
			TFormVector 0,0,Thrust,ship01,0
			TOKRB_SetForce ship01rb,TFormedX#(),TFormedY#(),TFormedZ#()
		
		;end acceleration stuff
		
		;;;;;;;rotation stuff;;;;;;;
		
		;creating the variables to store postitions of the engines on the ship
		;try to reduce the number of thrusters on this ship; it will increase speed a lot.
		;I may have mucked up my left and right orientation at some point
		;so this would be a good place to look for the problem		
		TFormPoint 3.8,0,0,ship01,0 ;port engine
			PortEngine#(0)=TFormedX#() ;storing the x position
			PortEngine#(1)=TFormedY#() ;storing the y position
			PortEngine#(2)=TFormedZ#() ;storing the z position
		
		TFormPoint -3.8,0,0,ship01,0 ;starboard engine
			StarboardEngine#(0)=TFormedX#() ;storing the x position
			StarboardEngine#(1)=TFormedY#() ;storing the y position
			StarboardEngine#(2)=TFormedZ#() ;storing the z position
		

		TFormPoint 0,2,0,ship01,0 ;top back engine
			BackTopEngine#(0)=TFormedX#()
			BackTopEngine#(1)=TFormedY#()
			BackTopEngine#(2)=TFormedZ#()
		
		TFormPoint 0,-2,0,ship01,0 ;bottom back engine
			BackBottomEngine#(0)=TFormedX#()
			BackBottomEngine#(1)=TFormedY#()
			BackBottomEngine#(2)=TFormedZ#()
		
		
		;yaw control
			
		;left turn with left arrow
		If KeyDown(203)
				
			;apply reverse force to engine on port wing
			TFormVector 0,0,1,ship01,0	
			TOKRB_ApplyImpulse2(ship01rb,TFormedX#(),TFormedY#(),TFormedZ#(),PortEngine#(0),PortEngine#(1),PortEngine#(2))
						
			;apply forward force to engine on starboard wing
			TFormVector 0,0,-1,ship01,0
			TOKRB_ApplyImpulse2(ship01rb,TFormedX#(),TFormedY#(),TFormedZ#(),StarboardEngine#(0),StarboardEngine#(1),StarboardEngine#(2))			
						
		EndIf
	
		;right turn with right arrow
		If KeyDown(205)		
			;forward force to left wing	
			TFormVector 0,0,-1,ship01,0	
			TOKRB_ApplyImpulse2(ship01rb,TFormedX#(),TFormedY#(),TFormedZ#(),PortEngine#(0),PortEngine#(1),PortEngine#(2))
						
			;apply reverse force to engine on starboard wing
			TFormVector 0,0,1,ship01,0
			TOKRB_ApplyImpulse2(ship01rb,TFormedX#(),TFormedY#(),TFormedZ#(),StarboardEngine#(0),StarboardEngine#(1),StarboardEngine#(2))			
		
		EndIf
		
		;pitch control
		
		;downwards with up arrow
		If KeyDown(200)
			;forward force to top back engine			
			TFormVector 0,0,1,ship01,0	
			TOKRB_ApplyImpulse2(ship01rb,TFormedX#(),TFormedY#(),TFormedZ#(),BackTopEngine#(0),BackTopEngine#(1),BackTopEngine#(2))
			;reverse force to bottom back engine						
			TFormVector 0,0,-1,ship01,0
			TOKRB_ApplyImpulse2(ship01rb,TFormedX#(),TFormedY#(),TFormedZ#(),BackBottomEngine#(0),BackBottomEngine#(1),BackBottomEngine#(2))
		EndIf
		
		;upwards with down arrow
		If KeyDown(208)
			;reverse force to top back engine		
			TFormVector 0,0,-1,ship01,0	
			TOKRB_ApplyImpulse2(ship01rb,TFormedX#(),TFormedY#(),TFormedZ#(),BackTopEngine#(0),BackTopEngine#(1),BackTopEngine#(2))
			;forward force to bottom back engine						
			TFormVector 0,0,1,ship01,0
			TOKRB_ApplyImpulse2(ship01rb,TFormedX#(),TFormedY#(),TFormedZ#(),BackBottomEngine#(0),BackBottomEngine#(1),BackBottomEngine#(2))
		EndIf
		
		;rotate counterclockwise with "w" key
		If KeyDown(17)
			;downwards force to port engine
			TFormVector 0,1,0,ship01,0	
			TOKRB_ApplyImpulse2(ship01rb,TFormedX#(),TFormedY#(),TFormedZ#(),PortEngine#(0),PortEngine#(1),PortEngine#(2))			
			;upwards force to starboard engine
			TFormVector 0,-1,0,ship01,0
			TOKRB_ApplyImpulse2(ship01rb,TFormedX#(),TFormedY#(),TFormedZ#(),StarboardEngine#(0),StarboardEngine#(1),StarboardEngine#(2))		
		EndIf
		
		;rotate clockwise with "e" key
		If KeyDown(18)
			;upwards force to port engine
			TFormVector 0,-1,0,ship01,0	
			TOKRB_ApplyImpulse2(ship01rb,TFormedX#(),TFormedY#(),TFormedZ#(),PortEngine#(0),PortEngine#(1),PortEngine#(2))			
			;downwards force to starboard engine
			TFormVector 0,1,0,ship01,0
			TOKRB_ApplyImpulse2(ship01rb,TFormedX#(),TFormedY#(),TFormedZ#(),StarboardEngine#(0),StarboardEngine#(1),StarboardEngine#(2))		
		EndIf
		
		;Turbulence... an interesting effect
		TFormPoint Rnd(-MeshWidth(ship01)/2,MeshWidth(ship01)/2),Rnd(-MeshHeight(ship01)/2,MeshHeight(ship01)/2),Rnd(-MeshDepth(ship01)/2,MeshDepth(ship01)/2),ship01,0
			BlastX#=TFormedX() : BlastY#=TFormedY() : BlastZ#=TFormedZ()
		TOKRB_ApplyImpulse2 ship01rb,Rnd(-TurbulencePower,TurbulencePower),Rnd(-TurbulencePower,TurbulencePower),Rnd(-TurbulencePower,TurbulencePower),BlastX,BlastY,BlastZ


		;Camera Movement
		
		;Chase
		SuperCam(cam1,Ship01,0.5,8,3,3,0)


		doSnow()
		
		
		;;;;;;;;;;;;;;;;END CONTENT;;;;;;;;;;;;;;;;;;;;
	
		;update the simulator
			TOKSIM_Advance(1.5/FPS,1)
		
		;matching objects to their rigid bodies
		;match ship01 to the tokamak rigid body
		PositionEntity ship01,TOKRB_GetX#(ship01rb),TOKRB_GetY#(ship01rb),TOKRB_GetZ#(ship01rb)
		RotateEntity ship01,TOKRB_GetPitch#(ship01rb),TOKRB_GetYaw#(ship01rb),TOKRB_GetRoll#(ship01rb),False
		
		;match the dummy ship to it's tokamak rigid body
		PositionEntity ship01_2,TOKRB_GetX#(ship01_2rb),TOKRB_GetY#(ship01_2rb),TOKRB_GetZ#(ship01_2rb)
		RotateEntity ship01_2,TOKRB_GetPitch#(ship01_2rb),TOKRB_GetYaw#(ship01_2rb),TOKRB_GetRoll#(ship01_2rb),False		


			
		;finish render tweening
			UpdateWorld
		Next
	
	RenderWorld tween#
	
	;another part of the very temporary fps counter
	CurFPS# = 1000.0 / (MilliSecs() - Start)
	
	;the temporary HUD
	;by the way, this seems to be the best place to put one
	
	Text 0,10,"Thrust: " + thrust/10 ; display thrust
	
	;TODO: A SHIP SPEED CHECKER	

	Text 0,30,"Physics Time:"+TOKSIM_GetPhysicsTime()*1000.0+ " milliseconds" ;physics time

	Text 0,50,"Render Time:"+Str(elapsed)+ " milliseconds" ;render time
	
	;Text 0,70,"Fps: " + CurFps# ;display fps ;Removed FPS counter
	
	;Text 0,90,collision$ ;display if the ships are collided or not
		
	;more double buffering
	Flip
	
Wend

TOKSIM_DestroySimulator()
End

;;;;;;;;;The functions;;;;;;;;;;;

;the function to update cube maps

Function UpdateCubeMap( tex,camera ) 

tex_sz=TextureWidth(tex) 

;do left view 
SetCubeFace tex,0 

; copy contents of backbuffer to cube map 
CopyRect 0,0,tex_sz,tex_sz,0,0,BackBuffer(),TextureBuffer(tex) 

;do forward view 
SetCubeFace tex,1 
RotateEntity camera,0,0,0 
RenderWorld 
CopyRect 0,0,tex_sz,tex_sz,0,0,BackBuffer(),TextureBuffer(tex) 

;do right view 
SetCubeFace tex,2 
RotateEntity camera,0,-90,0 
RenderWorld 
CopyRect 0,0,tex_sz,tex_sz,0,0,BackBuffer(),TextureBuffer(tex) 

;do backward view 
SetCubeFace tex,3 
RotateEntity camera,0,180,0 
RenderWorld 
CopyRect 0,0,tex_sz,tex_sz,0,0,BackBuffer(),TextureBuffer(tex) 

;do up view 
SetCubeFace tex,4 
RotateEntity camera,-90,0,0 
RenderWorld 
CopyRect 0,0,tex_sz,tex_sz,0,0,BackBuffer(),TextureBuffer(tex) 

;do down view 
SetCubeFace tex,5 
RotateEntity camera,90,0,0 
RenderWorld 
CopyRect 0,0,tex_sz,tex_sz,0,0,BackBuffer(),TextureBuffer(tex)
End Function

Function LoadConvex(file$)
 Convex = CreateBank(FileSize(file$))
 filein = ReadFile(file$)
 For i=0 To FileSize(file$)-1
  PokeByte Convex,i,ReadByte(filein)
 Next
 CloseFile filein
 Return Convex
End Function


; SuperCam!
;
; by PsychicParrot 2003
;
; 
; Usage : SuperCam(cam,ent,cspeed#,dist#,hite#,xrot#,tilt#)
; 
;

Function SuperCam(cam,ent,cspeed#,dist#,hite#,xrot#,tilt#)
	TFormPoint 0,hite#,-dist#,ent,0

	cx#=(TFormedX()-EntityX(cam))*cspeed#
	cy#=(TFormedY()-EntityY(cam))*cspeed#
	cz#=(TFormedZ()-EntityZ(cam))*cspeed#

	TranslateEntity cam,cx,cy,cz
	PointEntity cam,ent
	RotateEntity cam,xrot#,EntityYaw(cam),tilt
End Function



(Oh and this thread should probably be moved to the B3d section...)