Pointentity Problem

Blitz3D Forums/Blitz3D Programming/Pointentity Problem

A 'YawToEntity' function. Seems to work well enough. Another possible use for this is in turning bots to face a waypoint entity in a 3D (or 2D in 3D) game.


Global timer = CreateTimer( 25 )

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

Global cam = CreateCamera()
CameraZoom cam, 1.6
MoveEntity cam, 0.0, 60.0, 0.0
TurnEntity cam, 90.0, 0.0, 0.0

Global light = CreateLight()

Global cone = CreateCone()
RotateMesh cone, 90.0, 0.0, 0.0
UpdateNormals cone
PositionEntity cone, 0.0, 0.0, -20.0

Global cube = CreateCube()
UpdateNormals cube
PositionEntity cube, 40.0, 0.0, 20.0

While Not KeyHit( 1 )

MoveEntity cube, -0.22, 0.0, 0.0

YawToEntity( cone, cube, 0.5 )
;PointEntity( cone, cube )
;MoveEntity cone, 0.0, 0.0, 0.3

UpdateWorld
RenderWorld

Text 10, 10, DeltaYaw( cone, cube )

Flip

WaitTimer( timer )

Wend

End


Function YawToEntity( src_entity, dest_entity, rate# )
; Turns 'src_entity' to point at 'dest_entity' at the rotation rate specified by 'rate#'.

	Local target_yaw# = DeltaYaw( src_entity, dest_entity )
	
	; If the required correction amount is less than the correction amount to be applied...
	If Abs( target_yaw# ) < rate#
		; Point 'src_entity' directly at 'dest_entity' to prevent jittering.
		TurnEntity src_entity, 0.0, target_yaw#, 0.0
	Else
		; Turn 'src_entity' gradually towards 'dest_entity'.
		TurnEntity src_entity, 0.0, rate# * Sgn( target_yaw# ), 0.0
	EndIf
End Function