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


TurnEntity, RotateEntity

have

TurnMesh, RotateMesh counterparts.

TFormPoint may help in this... Im not too sure...

This might help:
http://www.blitzbasic.com/codearcs/codearcs.php?code=978

Thanks, TFormPoint does the job.

Function TransformMesh(mesh,entity)
msurface=GetSurface(mesh,1)
esurface=GetSurface(entity,1)
numverts=CountVertices(msurface)
For i=0 To numverts-1
TFormPoint VertexX(esurface,i),VertexY(esurface,i),VertexZ(esurface,i),entity,0
VertexCoords msurface,i,TFormedX(),TFormedY(),TFormedZ()
TFormNormal VertexNX(esurface,i),VertexNY(esurface,i),VertexNZ(esurface,i),entity,0
VertexNormal msurface,i,TFormedX(),TFormedY(),TFormedZ()
Next
End Function

You could do something like this ... but vertex manipulation is slow.

Note that each time you rotate a mesh the yaw, pitch and roll are always zeroised. Perhaps if you rotatemesh each axis separately this will work for you .. I doubt it though.

Oops .. too slow but same idea!!


Function UpdateGame()

	TurnEntity pivotBlock,0,.1,.1	
	
	MESHrotate( test , pivotBlock )

	;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

Function MESHrotate( Mesh, Proxy ) 

	; note assumes mesh and proxy have same no of vertices
	Ms = GetSurface( Mesh, 1 )
	Ps = GetSurface( Proxy , 1 )
	
	For v = 0 To CountVertices( Ps )-1
		TFormPoint VertexX( Ps, v ), VertexY( Ps, v ) , VertexZ( Ps, v ) , Proxy , 0
		VertexCoords Ms , v , TFormedX(), TFormedY(), TFormedZ()
	Next	
	
End Function