Flickering object

Blitz3D Forums/Blitz3D Programming/Flickering object

Help please. I'm getting an odd flickering thing here with my ship oject, what am I doing wrong?
;3d space flight engine v 3
;now that I have proper physics, I think it may work out
;I'm aiming to fix the gameplay problem involving it just not 'feeling' right

;set up graphics
Graphics3D 1024,768,32

;double buffering
SetBuffer BackBuffer()

;set up tokamak simulator

TOKSIM_SetRigidBodiesCount 1
TOKSIM_SetAnimatedBodiesCount 0
TOKSIM_SetRigidParticleCount 0
TOKSIM_SetControllersCount 0
TOKSIM_SetGeometriesCount 14

TOKSIM_CreateSimulator(0,0,0)

;load the ship
;NOTE: CHANGE THIS TO AN ARRAY!!
Global ship01=LoadMesh("ship1.b3d")

;the camera
Global cam1 = CreateCamera()
CameraViewport cam1,0,0,GraphicsWidth(),GraphicsHeight()
PositionEntity cam1,0,3,-10

;the lights
AmbientLight 100,100,100

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

;the ship rigid body
ship01rb=TOKRB_Create()
	TOKRB_SetPosition ship01rb,0.0,0.0,0.0
	TOKRB_SetRotation ship01rb,90.0,180.0,0.0
	TOKRB_SetBoxInertiaTensor ship01rb,5.0,5.0,5.0,1.0
	TOKRB_SetMass ship01rb,100.0
	TOKRB_SetLinearDamping ship01rb,0.1
	TOKRB_SetAngularDamping ship01rb,0.1
	TOKRB_CollideConnected ship01rb,0

	ship01rb_1 = TOKRB_AddBox(ship01rb,-2.20411,-2.88501,2.09501)
		TOKGEOM_SetPositionAndRotation g,-3.78355,-0.312894,-0.471711,0.0,0.0,0.0
		TOKGEOM_SetMaterialIndex ship01rb_1,0

	ship01rb_2 = TOKRB_AddBox(ship01rb,2.0,-0.913641,-1.19867)
		TOKGEOM_SetPositionAndRotation g,-3.78568,-2.07925,-0.294399,0.0,0.0,0.0
		TOKGEOM_SetMaterialIndex ship01rb_2,0

	ship01rb_3 = TOKRB_AddBox(ship01rb,-2.0,-3.0,0.804565)
		TOKGEOM_SetPositionAndRotation g,-3.85608,-3.90423,-0.268204,0.0,0.0,0.0
		TOKGEOM_SetMaterialIndex ship01rb_3,0

	ship01rb_4 = TOKRB_AddBox(ship01rb,1.86821,-0.93018,-0.757756)
		TOKGEOM_SetPositionAndRotation g,-3.94073,-5.83604,-0.289075,0.0,0.0,0.0
		TOKGEOM_SetMaterialIndex ship01rb_4,0

	ship01rb_5 = TOKRB_AddBox(ship01rb,1.44042,-0.808995,-0.571616)
		TOKGEOM_SetPositionAndRotation g,-3.83165,-6.66908,-0.3939,0.0,0.0,0.0
		TOKGEOM_SetMaterialIndex ship01rb_5,0

	ship01rb_6 = TOKRB_AddBox(ship01rb,-1.36885,-1.11293,-0.847562)
		TOKGEOM_SetPositionAndRotation g,-2.3013,-0.285581,-0.419939,0.0,0.0,0.0
		TOKGEOM_SetMaterialIndex ship01rb_6,0

	ship01rb_7 = TOKRB_AddBox(ship01rb,1.5764,-0.906782,-0.5684)
		TOKGEOM_SetPositionAndRotation g,-0.947437,-0.327261,-0.415343,0.0,0.0,0.0
		TOKGEOM_SetMaterialIndex ship01rb_7,0

	ship01rb_8 = TOKRB_AddBox(ship01rb,1.49912,-1.17104,-0.826421)
		TOKGEOM_SetPositionAndRotation g,-5.59488,-0.325605,-0.399286,0.0,0.0,0.0
		TOKGEOM_SetMaterialIndex ship01rb_8,0

	ship01rb_9 = TOKRB_AddBox(ship01rb,1.20174,-0.92761,0.578545)
		TOKGEOM_SetPositionAndRotation g,-6.92694,-0.300521,-0.372304,0.0,0.0,0.0
		TOKGEOM_SetMaterialIndex ship01rb_9,0
		
	ship01rb_10 = TOKRB_AddBox(ship01rb,0.379707,-2.13287,1.27555)
		TOKGEOM_SetPositionAndRotation g,-0.0445214,-0.347584,-0.413206,0.0,0.0,0.0
		TOKGEOM_SetMaterialIndex ship01rb_10,0
		
	ship01rb_11 = TOKRB_AddBox(ship01rb,-0.320397,-2.21165,-1.36992)
		TOKGEOM_SetPositionAndRotation g,-7.58982,-0.350264,-0.375476,0.0,0.0,0.0
		TOKGEOM_SetMaterialIndex ship01rb_11,0

	ship01rb_12 = TOKRB_AddBox(ship01rb,0.246424,-1.22218,0.220696)
		TOKGEOM_SetPositionAndRotation g,-0.0723057,-1.96473,-0.402167,0.0,0.0,0.0
		TOKGEOM_SetMaterialIndex ship01rb_12,0

	ship01rb_13 = TOKRB_AddBox(ship01rb,-0.237158,-1.5,0.245809)
		TOKGEOM_SetPositionAndRotation g,-7.61479,-1.83985,-0.40037,0.0,0.0,0.0
		TOKGEOM_SetMaterialIndex g,0

	ship01rb_14 = TOKRB_AddCylinder(ship01rb,1.73311,2.2236)
		TOKGEOM_SetPositionAndRotation g,-3.80075,-4.39053,-0.900166,0.0,0.0,0.0
		TOKGEOM_SetMaterialIndex g,0
		
TOKRB_UpdateBoundingInfo ship01rb
		
;the acceleration variable NOTE: SWITCH THRUST INTO THE SHIP01 ARRAY/TYPE
thrust# = 0
		

;main loop
While Not KeyDown(1)

;damned render tweening. Make this code more readable at some point. The indenting's all messed up.

	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;;;;;;;;;;;;;;;;;;;;
		
		;;;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 THIS A VARIABLE
			If thrust#<2000
		
			;speed up		
			thrust# = thrust# + 10
		
			EndIf
		EndIf
	
		;if the - key is pressed
	
		If KeyDown(74)
	
			If thrust#>-2000 ; max speed check
		
				;slow down
				thrust# = thrust# - 10
			EndIf	
		EndIf
	
		If KeyDown(11)
			thrust# = 0
		EndIf
	
		;actually move	
		
		TOKRB_SetForce ship01rb,0,0,thrust#
		
		;end acceleration stuff
	
		;rotation
	
		;left turn with "a" key
		If KeyDown(30)	
			TOKRB_ApplyImpulse2 ship01rb,0,0,0.5,TOKRB_GetX(ship01rb)+1,TOKRB_GetY(ship01rb),TOKRB_GetZ(ship01rb)
			TOKRB_ApplyImpulse2 ship01rb,0,0,-0.5,TOKRB_GetX(ship01rb)-1,TOKRB_GetY(ship01rb),TOKRB_GetZ(ship01rb)
		EndIf
	
		;right turn with "d" key
		If KeyDown(32)
			TOKRB_ApplyImpulse2 ship01rb,0,0,0.5,TOKRB_GetX(ship01rb)-1,TOKRB_GetY(ship01rb),TOKRB_GetZ(ship01rb)
			TOKRB_ApplyImpulse2 ship01rb,0,0,-0.5,TOKRB_GetX(ship01rb)+1,TOKRB_GetY(ship01rb),TOKRB_GetZ(ship01rb)
		EndIf
		
		;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

		;camera movement
;			PositionEntity cam1,EntityX(ship01),EntityY(ship01)+3,EntityZ(ship01)-10
;			PointEntity cam1,ship01
		
		;;;;;;;;;;;;;;;;END CONTENT;;;;;;;;;;;;;;;;;;;;
	
		;update the simulator
			TOKSIM_Advance(1.5/FPS,1)
			
		;finish render tweening
			UpdateWorld
		Next
	
	RenderWorld tween#
		
	;more double buffering
	Flip
		
Wend

TOKSIM_DestroySimulator()
End

Uh, sorry about any vulgar language, It will go through very extensive editing if this works out and I release it with source. I got a bit angry at my indenting :D
You will need the 3d model at http://www.freewebs.com/bass-line/projects/3dmodels/ship1.b3d to see the ship, and the tokamak physics wrapper v. 0.4 or higher. Just download from the link on the blitzbasic.com main page.
Thanks in advance!

Try adding the following line after you load the ship.
RotateMesh ship01,90,180,0

and change the TOKRB_SetRotation line to...
TOKRB_SetRotation ship01rb,0.0,0.0,0.0
or simply remove it.

I thought I had the rotations all worked out but there still seems to be some nasty little bugs in the code.

Yay it works! Thanks. Now to focus on getting my friction stuff down to sensible amounts...