Weird issw with TurnEntity (Possibly Float issue)

Blitz3D Forums/Blitz3D Programming/Weird issw with TurnEntity (Possibly Float issue)

_PJ_(Posted 2009) [#1]
The following should be run-able as is. Check out the Yaw values, though, I dunno why I can't seem to fix them to stay to 90 degree increments. :S

;Dungeon 

 
Graphics3D 800,600,32,2

SetBuffer BackBuffer()

Global screenbk=CreateImage(GraphicsWidth(),GraphicsHeight())
Global underw_a
;*****************************INITIALISE******************************

SeedRnd MilliSecs()

AmbientLightR=128
AmbientLightG= 128
AmbientLightB= 128

MasterFogRangeNear=-12
MasterFogRangeFar=8

Global cam=CreateCamera()

EntityType cam,2,0

CameraViewport cam,0,0,800,600

ScaleEntity cam,0.5,1,0.5

CameraFogMode cam,1
CameraFogColor cam,AmbientLightR /8,AmbientLightG /8,AmbientLightB /8
CameraFogRange cam,MasterFogRangeNear,MasterFogRangeFar
CameraRange cam,0.01,MasterFogRangeFar+2

ViewFar=MasterFogRangeFar
ViewNear=MasterFogRangeNear

camlight=CreateLight(2)
EntityParent camlight,cam,1

LightRange camlight,4
EntityRadius cam,0.3

Global fpcounter#
Global timer

Global TORCH=0
Global poison=0
Global tempPoison#=0
Global tpadd#=0.01
;****************************BUILD ARRAYS *****************************

Dim vpos#(1,1)
Dim Map(200)

;***************************CREATE QUICK RANDOM MAP**********************


Fl=CreatePlane()
MoveEntity fl,0,-1,0
;ftx=LoadTexture("Floor.bmp")
;EntityTexture Fl,ftx
;FreeTexture ftx

	For f= 1 To 200

		Map(f)=CreateWall()
					
		ScaleMesh Map(f),1,4,1
		
				Morph(Map(f))
						
		PositionEntity Map(f),Rand(-15,15),0,Rand(-15,15)
				
		Next
	
	
;******************************* SET COLLISIONS ******************

Collisions 2,3,3,1

;*************************** MAIN LOOP ****************************

Global fps#=0

timer=MilliSecs()

While Not KeyDown(1)

;*************************CONTROLS*****************************

PositionEntity cam,Int(EntityX(cam)),0,Int(EntityZ(cam)),1
RotateEntity cam,0,Floor(EntityYaw(cam)),0,1



If KeyDown(200) Then walkforward(cam)
If KeyDown(208) Then walkbackward(cam)

If KeyDown(203) Then turnleft(cam)
If KeyDown(205) Then turnright(cam)

If KeyDown(25)

			Delay 150
			
			poison = (1-poison)
			
			If poison=1
				AmbientLightR=AmbientLightR-64
				AmbientLightB=AmbientLightB-64
			End If	
					
			If poison=0 
				CameraZoom cam,1
				AmbientLightR=AmbientLightR+64
				AmbientLightB=AmbientLightB+64
			EndIf

		AmbientLight AmbientLightR,AmbientLightG,AmbientLightB

EndIf


If KeyDown(20) 
			
			Delay 150
			
			TORCH=(Not TORCH)
			
			If TORCH
				AmbientLightR=AmbientLightR+64
				AmbientLightG=AmbientLightG+64
				AmbientLightB=AmbientLightB+32
			
			FogRangeFar=MasterFogRangeFar+2
			CameraRange cam,0.01,FogRangeFar+2
			CameraFogRange cam,FogRangeNear,Rand(FogRangeFar,FogRangeFar+4)

			LightColor camlight,Rand(32)+16+AmbientLightR,Rand(32)+16+AmbientLightG,AmbientLightB+16

			ShowEntity camlight
	
			ViewFar=(ViewFar*2)
	
			EndIf	
			
			If Not TORCH			
				AmbientLightR=AmbientLightR-64
				AmbientLightG=AmbientLightG-64	
				AmbientLightB=AmbientLightB-32
					
				CameraFogRange cam,FogRangeNear,MasterFogRangeFar
				CameraRange cam,0.01,MasterFogRangeFar-2	
					
				HideEntity camlight					
			EndIf
				
		AmbientLight AmbientLightR,AmbientLightG,AmbientLightB
		CameraFogColor cam,AmbientLightR/8,AmbientLightG/8,AmbientLightB/8
	
CameraFogColor cam,AmbientLightR /8,AmbientLightG /8,AmbientLightB /8
CameraFogRange cam,FogRangeNear,FogRangeFar
CameraRange cam,0.01,FogRangeFar+2
	
			
EndIf

If TORCH
EndIf

;*************************TORCHLIGHT*******************************

		render()


Wend
End

;*******************************************FUNCTIONS************************




Function walkforward(entity)

	If poison=1
		CameraClsMode cam,0,0
	End If

For f= 1 To 5 

MoveEntity entity,0,0.05,0.1


render()

Next

For f= 1 To 5 

MoveEntity entity,0,-0.05,0.1


render()

Next

	If poison=1	Then CameraClsMode cam,1,1

End Function




Function walkbackward(entity)

	If poison=1
		CameraClsMode cam,0,0
	End If

For f= 1 To 5 

MoveEntity entity,0,0.1,-0.1


render()

Next

For f= 1 To 5 

MoveEntity entity,0,-0.1,-0.1

render()

Next

	If poison=1
		CameraClsMode cam,1,1
	End If

End Function


Function turnleft(entity)
yw=Int(Floor(EntityYaw(entity)))

For f=0 To 20

TurnEntity entity,0,4.5,0,True
	render()
Next

RotateEntity entity,0,yw+90,0,True
;q=EntityYaw(entity,True) Mod 90
;If q<>0 Then TurnEntity entity,0,-q,0,True

End Function


Function turnright(entity)
yw=Int(Floor(EntityYaw(entity)))
For f=0 To 20
TurnEntity entity,0,-4.5,0,True
	render()
Next

;RotateEntity entity,0,yw-90,0,True

;q=EntityYaw(entity,True) Mod 90
;If q<>0 Then TurnEntity entity,0,q,0,True
End Function







Function CreateWall()

;*****************************CREATE SEGMENTED CUBE FROM 6 SIDES **************

	mesh=CreateMesh()
	
	segs=3
	
	For scnt=0 To 3
		surf=CreateSurface( mesh )
		stx#=-.5
		sty#=stx
		stp#=Float(1)/Float(segs)
		y#=sty
		For a=0 To segs
			x#=stx
			v#=a/Float(segs)
			For b=0 To segs
				u#=b/Float(segs)
				AddVertex(surf,x,y,0.5,u,v)
				x=x+stp
			Next
			y=y+stp
		Next
		For a=0 To segs-1
			For b=0 To segs-1
				v0=a*(segs+1)+b:v1=v0+1
				v2=(a+1)*(segs+1)+b+1:v3=v2-1
				AddTriangle( surf,v0,v1,v2 )
				AddTriangle( surf,v0,v2,v3 )
			Next
		Next
		RotateMesh mesh,0,90,0
	Next

	UpdateNormals mesh
	
	EntityType Mesh,3,1
	
	EntityBox mesh,-0.5,0,-0.5,1,4,1

;wtx=LoadTexture("Wall.bmp")
;EntityTexture Mesh,wtx
;FreeTexture wtx

Return mesh
End Function


;**************************MORPH VERTS*****************************

Function Morph(mesh)

surftotal=CountSurfaces(mesh)

For xx= 1 To surftotal
as1=GetSurface(mesh,xx)

; record the locations of the verts
Dim vpos#(CountVertices(as1)-1,3)
For n=0 To CountVertices(as1)-1
	vpos(n,0)=VertexX(as1,n)
	vpos(n,1)=VertexY(as1,n)
	vpos(n,2)=VertexZ(as1,n)
	vpos(n,3)=0
Next

For n=0 To CountVertices(as1)-1

; change these to make it more or less messy
xm#=Rnd(-.1,.1)
ym#=Rnd(-.1,.1)
zm#=Rnd(-.1,.1)

	For nn=0 To CountVertices(as1)-1
	
	; if the vert has not been monkeyed with monkey away
	If vpos(nn,3)=0
		If vpos(n,0)=vpos(nn,0) And vpos(n,1)=vpos(nn,1) And vpos(n,2)=vpos(nn,2)
			VertexCoords as1,nn,vpos(nn,0)+xm,vpos(nn,1)+ym,vpos(nn,2)+zm
			vpos(nn,3)=1
			EndIf
		EndIf
		
	Next
Next

Next

Return mesh

End Function


Function render()

;*************************MAINTAIN FACING*******************

RotateEntity cam,0,EntityYaw(cam),0,True

	If poison=1
		tempPoison#=tempPoison#+tpadd#
		If TempPoison#>1 Then tpadd#=(0-0.01)
		If TempPoison#<0.8 Then tpadd#=0.01
			CameraZoom cam,tempPoison#
	EndIf

UpdateWorld
RenderWorld

Text 600,50,EntityX(cam,True)+" , "+EntityZ(cam,True),1,1
Text 600,70,EntityYaw(cam,True)

Text 0,100,"POLYS: "+TrisRendered(),0,1

Text 0,200,"FPS: "+fps#,0,1

If poison Text 0,250,"POISON AFFLICTION APPLIED",0,1
If TORCH Text 0,280,"TORCHLIGHT ACTIVE",0,1

fpcounter#=fpcounter#+1

	If (MilliSecs()-timer)>=1000
		timer=MilliSecs()
		fps#=fpcounter#
		fpcounter#=0
	EndIf

If poison=1
	CaptureScreen()
	WobbleView()
End If
	Flip
End Function


; COLLISIONS INDECES

;1 = PLAYER
;2 = CAMERA
;3 = WALL

















Function capturescreen()
 CopyRect 0,0,GraphicsWidth(),GraphicsHeight(),0,0,BackBuffer(),ImageBuffer(screenbk)
End Function

Function WobbleView()
 gw#=GraphicsWidth()
 gh#=GraphicsHeight()
 underw_a=(underw_a+4)
 steph#=gh/32
 mu8#=gh/60
 If underw_a>359 Then underw_a=0
  For iif#=0 To gh-4  Step .001
   wsin#=(Sin((underw_a+iif)Mod 360.0)*mu8#)
   CopyRect 0,  iif,         gw,steph+4, 0,iif+wsin#, ImageBuffer(screenbk),BackBuffer()
   iif=iif+steph
  Next
End Function



Sledge(Posted 2009) [#2]
Dude, try it this way:

All it's doing is giving the player an orientation modifier, which is an order to rotate by so many degrees. Then what happens is that the orientation modifer is chipped away each frame/loop as the entity is rotated, until it gets back to zero and is ready to take another order. You shouldn't have too much trouble integrating it into your own code.

EDIT: Oh, here's a slightly extended version with movement in. You know how it is when you get tapping away :)


Ross C(Posted 2009) [#3]
Try using rotateentity instead of turnentity. Personally, i've never seen the need for turnentity. As pointed out above, it carries floating point inaccuracies, which will add up.


_PJ_(Posted 2009) [#4]
The above stuff was modified so that I could give an example of the issue.
If you look, there's a coupla uses of rotateentity but these too didn't fix the inaccuracy :(

I'l work with your suggestion and see how I get on :)

The idea is that movement, though restricted to "steps" is displayed so the movement is a little smoother on-screen and also helps with the "POISON" SCREEN EFFECTS.