home  forum  showcase  royalty-free  sign in  register   
 
   Forum forum >> BlitzCoder >> PointTowardEntity(+Normalized)

Recent discussions

Sound Effects Events by megaphilx on Thu 6 Jan 2011 at 21:49:11
Latest updates by Heirauqs on Wed 5 Jan 2011 at 07:29:10
Bugs list by megaphilx on Wed 5 Jan 2011 at 03:46:36
What does PHP stand for? by sgvisual on Tue 23 Nov 2010 at 23:37:58
MegaPhilX To-Do List by megaphilx on Sat 13 Nov 2010 at 00:19:46
On-Topic Break Time by gosse on Fri 12 Nov 2010 at 17:58:47
gab to do list by Heirauqs on Fri 10 Sep 2010 at 07:37:11
gosse to do list by gosse on Fri 10 Sep 2010 at 06:27:45
Master Task List and Status by Heirauqs on Thu 9 Sep 2010 at 17:38:19
Borderlands Tweaks by gosse on Wed 28 Oct 2009 at 02:24:25

Back to BlitzCoderPage 1

 PointTowardEntity(+Normalized) on Sun 10 Aug 2008 at 01:17:50
 

gosse
  Administrator
indieground.com
Posts: 89



Joined: Apr 2006
Location: Montreal, QC, CA

 

(reposted from Nuloen forum - Feb 15, 2006)
Here's 2 functions I coded for someone over CW.com in order to be able to gradually rotate (point) an entity toward another one. The normalized version is truly great!
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

-gosse

Back to BlitzCoderPage 1

1 guest online. 26 visitors in the last 24 hours.