What's wrong with this code?

Blitz3D Forums/Blitz3D Beginners Area/What's wrong with this code?

This code allows the camera to be rotated around some object.
The object should then turn to point in the same direction as the camera via the shortest route.

I've tweaked with the variables a lot; sometimes the object rotates in the wrong direction, sometimes it doesn't stop, sometimes it doesn't move at all.

I think it has something to do with the fact that, by default, my object has yaw = 0 and my cameraPivot has yaw = 180 initially. This is needed for the correct orientation of the viewer initially.

Can anyone find the problem?

[Code]
; Ensure that the mouse never leaves the screen bounds.
If ( MouseX() > 1200 ) Or ( MouseX() < 50 ) Or ( MouseY() > 1000 ) Or ( MouseY() < 50 ) Then
MoveMouse 640, 512
EndIf

; Turn cameraPivot, thus making camera spin around it.
TurnEntity cameraPivot, MouseYSpeed() / 1.5, -MouseXSpeed(), 0

; Always keep cameraPivot's roll 0 wrt the horizontal plane.
RotateEntity cameraPivot, EntityPitch(cameraPivot), EntityYaw(cameraPivot), 0


If EntityYaw(player\model) > 0
If EntityYaw(cameraPivot) < EntityYaw(player\model)
TurnEntity player\model, 0, 1, 0
ElseIf EntityYaw(cameraPivot) > EntityYaw(player\model)
TurnEntity player\model, 0, -1, 0
EndIf

Else
If EntityYaw(cameraPivot) < 360 + EntityYaw(player\model)
TurnEntity player\model, 0, 1, 0
ElseIf EntityYaw(cameraPivot) > 360 + EntityYaw(player\model)
TurnEntity player\model, 0, -1, 0
EndIf

EndIf


[/Code]

Bear in mind that entityyaw() returns -180 to 180 not 0 - 360.

Something like this should work ..

turnentity player\model, 0, ( entityyaw( camerapivot, 1 ) - entityyaw( player\model ) ) * .1 ,0 


or ....

global Pivot = createpivot() ;reuse this 


tformpoint 0, 0, 10, camerapivot, 0
positionentity Pivot , tformedx(), tformedy(), tformedz()
turnentity player\model, 0 , deltayaw( player\model, camerapivot ) * .1, 0


Stevie

you mean, you want to something like this ?
Graphics3D 800,600,0,2
SetBuffer BackBuffer()

GW=GraphicsWidth()
GH=GraphicsHeight()

Type Truc
	Field model
End Type

player.truc=New truc
	player\model=CreateCube()


cameraPivot=CreatePivot()
camera	=	CreateCamera(cameraPivot)
			CameraClsColor camera,120,180,255
			CameraRange camera,.1,100
			MoveEntity camera,0,0,-10

; just to view the camera rotation
	Axe3D=CAM_Create3DAxes(camera)

; to view the face rotating, we need a light.
	light=	CreateLight(1)
			MoveEntity light,100,100,-100
			PointEntity light,CameraPivot


Repeat
	; Ensure that the mouse never leaves the screen bounds.
		If ( MouseX() > GW-50 ) Or ( MouseX() < 50 ) Or ( MouseY() > GH-50 ) Or ( MouseY() < 50 ) Then
			MoveMouse GW/2, GH/2
		EndIf

		; Turn cameraPivot, thus making camera spin around it.
		TurnEntity cameraPivot, MouseYSpeed() / 1.5, -MouseXSpeed(), 0

		; Always keep cameraPivot's roll 0 wrt the horizontal plane.
		RotateEntity cameraPivot, EntityPitch(cameraPivot), EntityYaw(cameraPivot), 0

	; try new code
		LIB_PointEntity(player\model,camera,.01)

	; disable the original code.
		Goto suite
		If EntityYaw(player\model) > 0
			If EntityYaw(cameraPivot) < EntityYaw(player\model)
				TurnEntity player\model, 0, 1, 0
			ElseIf EntityYaw(cameraPivot) > EntityYaw(player\model)
				TurnEntity player\model, 0, -1, 0
			EndIf
		Else
			If EntityYaw(cameraPivot) < 360 + EntityYaw(player\model)
				TurnEntity player\model, 0, 1, 0
			ElseIf EntityYaw(cameraPivot) > 360 + EntityYaw(player\model)
				TurnEntity player\model, 0, -1, 0
			EndIf
		EndIf
		.suite

	; turn the 3D axe to 0,0,0 coords
		RotateEntity Axe3D,0,0,0,1
	RenderWorld
	Flip
	If KeyHit(1) Exit
Forever

ClearWorld 1,1,1
EndGraphics
End



Function LIB_PointEntity(source%,dest%,rate#=1)
	Dx#=EntityX(dest,1)-EntityX(source,1)
	Dy#=EntityY(dest,1)-EntityY(source,1)
	Dz#=EntityZ(dest,1)-EntityZ(source,1)
	AlignToVector source,dx#,dy#,dz#,3,rate
End Function


;===============================================================

Function CAM_Create3DAxes(Cam)
	rotule=CreateMesh(cam)
		repX=CreateCube()
			ScaleMesh RepX,.1,.002,.002:PositionMesh RepX,.12,0,0
			b=CreateBrush(255,0,0):BrushFX b,1:PaintMesh repx,b:FreeBrush b
		repY=CreateCube()
			ScaleMesh RepY,.002,.1,.002:PositionMesh RepY,0,.12,0
			b=CreateBrush(0,255,0):BrushFX b,1:PaintMesh repy,b:FreeBrush b
		repZ=CreateCube()
			ScaleMesh RepZ,.002,.002,.1:PositionMesh RepZ,0,0,.12:b=CreateBrush(0,0,255)
			BrushFX b,1:PaintMesh repz,b:FreeBrush b
		AddMesh RepX,Rotule:FreeEntity RepX
		AddMesh RepY,Rotule:FreeEntity RepY
		AddMesh RepZ,Rotule:FreeEntity RepZ
	EntityOrder Rotule,-00950
	MoveEntity Rotule,-.5,-.3,1
	Return rotule
End Function