Rotate a Mesh like an Entity?

Blitz3D Forums/Blitz3D Programming/Rotate a Mesh like an Entity?

Hi, I need to rotate a mesh in the same way RotateEntity works, but I can't get it to work.

The code bellow shows the problem, if you rotate just the Yaw it's fine, but add the other angles and it goes out of sync with the rotated entity.

I know the pitch/roll values jump in Bltz after 90/180 but the mesh goes out before then.

Anyone else made a function to do this?

Thanks,

Graphics3D 800,600,32,2
SetBuffer BackBuffer()

Global gameFPS=60
Global oldP#=0,oldY#=0,oldR#=0
Global newP#=0,newY#=0,newR#=0

;------------
.SetupCamera
;----------
;
Global camPiv=CreatePivot()
MoveEntity camPiv,2,0,0
RotateEntity camPiv,0,-45,0
Global Camera=CreateCamera(camPiv)
CameraClsColor Camera,0,0,0
CameraRange Camera,1,500
MoveEntity Camera,0,15,-15
RotateEntity Camera,20,0,0
CameraViewport Camera, 0, 0, GraphicsWidth (), GraphicsHeight ()

;------------
.SetupLights
;----------
;
Global Light=CreateLight()
PositionEntity Light,0,100,0
RotateEntity Light,45,-90,0
LightColor Light,200,200,200

;-----------
.SetupWorld
;---------
;
Plane=CreateCube()
ScaleMesh Plane,100,.001,100
EntityAlpha Plane,0.6

PlaneTexture=CreateTexture(128,128,9)

ClsColor 0,0,0
Cls
Color 255,255,255

Rect 0,0,64,64,1
Rect 64,64,64,64,1

CopyRect 0,0,128,128,0,0,BackBuffer(),TextureBuffer(PlaneTexture)

ScaleTexture PlaneTexture,.08,.08
EntityTexture Plane,PlaneTexture,0,0

Global pivotBlock=CreateCube()
ScaleMesh pivotBlock,1.5,0.9,4.5
PositionEntity pivotBlock,0,5,0
EntityAlpha pivotBlock,0.5
EntityColor pivotBlock,0,0,255

Global test=CreateCube()
ScaleMesh test,1.5,0.9,4.5
PositionEntity test,0,5,0

;---------
.MainLoop
;-------
;
framePeriod = 1000 / gameFPS
frameTime = MilliSecs () - framePeriod

Repeat

	; --------------------------------------------------------------
	; Frame limiting
	; --------------------------------------------------------------

	Repeat
		frameElapsed = MilliSecs () - frameTime
	Until frameElapsed
	
	frameTicks = frameElapsed / framePeriod	
	frameTween# = Float (frameElapsed Mod framePeriod) / Float (framePeriod)

	; --------------------------------------------------------------
	; Update game and world state
	; --------------------------------------------------------------

	For frameLimit = 1 To frameTicks
	
		If frameLimit = frameTicks Then CaptureWorld
		frameTime = frameTime + framePeriod
		
		UpdateGame()
		
		UpdateWorld

	Next

	; --------------------------------------------------------------
	; Draw 3D world
	; --------------------------------------------------------------

	RenderWorld frameTween

	Flip
	
Until KeyHit(1)

End

;----------
.Functions
;--------
;
Function UpdateGame()

	TurnEntity pivotBlock,0,.1,.1	

	newP#=EntityPitch(pivotBlock)
	newY#=EntityYaw(pivotBlock)
	newR#=EntityRoll(pivotBlock)

	DebugLog "Pitch:"+newP+" Yaw:"+newY+" Roll:"+newR
	
	RotateMesh test,newP-oldP,newY-oldY,newR-oldR
	oldP=newP : oldY=newY : oldR=newR

	Select MouseZSpeed() 
		Case -1 
		MoveEntity Camera,0,0,-2
		Case 1 
		MoveEntity Camera,0,0,2
	End Select

End Function