| Code
; Environment Settings
Graphics3D 800, 600, 0, 2
SetBuffer BackBuffer()
AmbientLight 255, 255, 255
SeedRnd MilliSecs()
AppTitle "Point Toward Entity"
; We need this pivot for the function
Global pvtPoint = CreatePivot()
; Create the scene
cam = CreateCamera()
MoveEntity cam, 0, 20, 0
TurnEntity cam, 90, 0, 0
CameraProjMode cam, 2
CameraZoom cam, 0.1
c = CreateCone()
p = CreatePlane()
s = CreateSphere()
EntityColor p, 60, 110, 160
EntityPickMode p, 2
EntityColor c, 0, 255, 0
RotateMesh c, 90, 0, 0
EntityColor s, 255, 0, 0
; Main Loop
While Not KeyDown(1)
; Pick and position the sphere
CameraPick cam, MouseX(), MouseY()
PositionEntity s, PickedX(), PickedY(), PickedZ()
; Select mode
If KeyHit(57) Then
mode = (mode + 1) Mod 3
End If
; Points the cone gradually towards the sphere
Select mode
Case 0
PointEntity c, s
Case 1
PointTowardEntity c, s, 0.01
Case 2
PointTowardEntityNormalized c, s, 1
End Select
; Render
Cls
RenderWorld
Text 0, 0, "Mode: " + mode + " (press space to change)"
Select mode
Case 0
Text 0, 15, "Point Entity"
Case 1
Text 0, 15, "Point Toward Entity"
Case 2
Text 0, 15, "Point Toward Entity Normalized"
End Select
Text 0, 30, "Turning Speed: " + (EntityYaw(c) - ly#)
ly# = EntityYaw(c)
Flip
Wend
Function PointTowardEntity(src, pt, r#)
Local dp#, dy#, dr#
MoveEntity pvtPoint, EntityX(src), EntityY(src), EntityZ(src)
PointEntity pvtPoint, pt
dp = EntityPitch(pvtPoint) - EntityPitch(src)
dy = EntityYaw(pvtPoint) - EntityYaw(src)
dr = EntityRoll(pvtPoint) - EntityRoll(src)
If dp > 180 Then
dp = dp - 360
ElseIf dp < -180 Then
dp = dp + 360
End If
If dy > 180 Then
dy = dy - 360
ElseIf dy < -180 Then
dy = dy + 360
End If
If dr > 180 Then
dr = dr - 360
ElseIf dr < -180 Then
dr = dr + 360
End If
TurnEntity src, dp*r, dy*r, dr*r
End Function
Function PointTowardEntityNormalized(src, pt, ma#)
Local dp#, dy#, dr#, l#
MoveEntity pvtPoint, EntityX(src), EntityY(src), EntityZ(src)
PointEntity pvtPoint, pt
dp = EntityPitch(pvtPoint) - EntityPitch(src)
dy = EntityYaw(pvtPoint) - EntityYaw(src)
dr = EntityRoll(pvtPoint) - EntityRoll(src)
If dp > 180 Then
dp = dp - 360
ElseIf dp < -180 Then
dp = dp + 360
End If
If dy > 180 Then
dy = dy - 360
ElseIf dy < -180 Then
dy = dy + 360
End If
If dr > 180 Then
dr = dr - 360
ElseIf dr < -180 Then
dr = dr + 360
End If
l = Sqr(dp*dp+dy*dy+dr*dr)
dp = dp / l * ma
dy = dy / l * ma
dr = dr / l * ma
TurnEntity src, dp, dy, dr
End Function
|