PDA

View Full Version : my AI Track as exercise



Lionheart008
06-03-2010, 01:33
http://community.thinbasic.com/index.php?topic=3279.msg24591;topicseen#new

thanks mainly here to eros, michael and petr for new thinbasic 1.8.0.0 update and tbgl section :) really good work!

thanks also for new overview (I think it's a good first start) to collect tbgl basics with various folders and themes with different knowledge for beginners, newbies or advanced users.

perhaps stan or denis (or another newbie with tbgl interests) can see how this tbgl "AI track" is working. I have modified the example from tbgl sample folder with a) second hovercraft object and b) other random behaviour and c) other waypoints and d) different speed. was just an exercise and I like this example! :)


'----------- test for AI hovercraft by lionheart :)

Uses "TBGL"

BEGIN CONST
' -- Scene IDs
%sScene = 1

' -- Entity IDs
%eCamera = 1
%eLight
%eHovercraft
%eHovercraft2
%eWayPointStart
%eWayPointStart2
END CONST

Randomize Timer

GLOBAL FrameRate AS DOUBLE

FUNCTION TBMAIN()
LOCAL hWnd As DWORD

' -- Create and show window
hWnd = TBGL_CreateWindowEx("frankos random Test for hovercraft elemental AI", 880, 620, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

' -- Create scene
TBGL_SceneCreate(%sScene)

' -- Create basic entities
' -- Create camera to look from 15, 15, 15 to 0, 0, 0
TBGL_EntityCreateCamera(%sScene, %eCamera)
TBGL_EntitySetPos(%sScene, %eCamera, 0, 0, 30)
TBGL_EntitySetTargetPos(%sScene, %eCamera, 0, 0, 0)

' -- Create point light
TBGL_EntityCreateLight(%sScene, %eLight)
TBGL_EntitySetPos(%sScene, %eLight, 15, 10, 5)

TBGL_NewList 1
TBGL_UseLighting %FALSE
TBGL_BeginPoly %GL_TRIANGLES
'TBGL_UseBlend %TRUE
TBGL_Vertex -0.08, -0.24
TBGL_Vertex 0.08, -0.24
TBGL_Vertex 0.00, 0.24
TBGL_EndPoly
'TBGL_UseBlend %FALSE
TBGL_UseLighting %TRUE
TBGL_EndList


' -- Create something to look at
TYPE tHoverCraft
nextWayPoint AS LONG
numWayPoint AS LONG
tolerance AS double ' -- Tolerance to reach waypoint
minSpeed AS double
maxSpeed AS double
steerFactor As Double
velocity As Double
END TYPE
dim HoverInfo as tHoverCraft

LOCAL x AS LONG
For x = %eHovercraft + 20 To %eHovercraft + 360
TBGL_EntityCreateDLSlot(%sScene, x, 0, 1)
TBGL_EntitySetColor(%sScene, x, Rnd(10,255), Rnd(10,255), Rnd(10,255))
' -- Assign data to it
with HoverInfo
.nextWayPoint = %eWayPointStart
.numWayPoint = 23
.tolerance = 0.9
.minSpeed = Rnd(60,190)*0.02
.maxSpeed = Rnd(400,450)*0.02
.steerFactor = Rnd(26,46)*0.02
.velocity = Rnd(360*(-0.1),360)
END WITH
TBGL_EntitySetUserData(%sScene, x, HoverInfo)
next

Local i As Long
For i = %eWayPointStart To %eWayPointStart + 23
TBGL_EntityCreateBox(%sScene, i, 0, 0.22, 0.22, 1)
TBGL_EntitySetColor(%sScene, i, 50, 180, 250)
Next

TBGL_NewList 2
TBGL_UseLighting %FALSE
TBGL_BeginPoly %GL_TRIANGLES
'TBGL_UseBlend %TRUE
TBGL_Vertex -0.18, -0.44
TBGL_Vertex 0.18, -0.44
TBGL_Vertex 0.00, 0.44
TBGL_EndPoly
'TBGL_UseBlend %FALSE
TBGL_UseLighting %TRUE
TBGL_EndList

Local y As Long
For y = %eHovercraft2 + 20 To %eHovercraft2 + 180
TBGL_EntityCreateDLSlot(%sScene, y, 0, 1)
TBGL_EntitySetColor(%sScene, y, Rnd(140,255), Rnd(100,255), Rnd(200,255))
' -- Assign data to it
With HoverInfo
.nextWayPoint = %eWayPointStart2
.numWayPoint = Rnd(4,14) '--->14
.tolerance = 0.68
.minSpeed = Rnd(20,80)*0.02
.maxSpeed = Rnd(200,250)*0.02
.steerFactor = Rnd(16,36)*0.04
.velocity = Rnd(160*(-0.1),360)
End With
TBGL_EntitySetUserData(%sScene, y, HoverInfo)
Next

' -- Hardcoded waypoints
TBGL_EntitySetPos(%sScene, %eWayPointStart+0, -2, 2, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+1, 0, 4, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+2, 4, 6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+3, 6, 5, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+4, 10, 0, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+5, 13, 1, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+6, 14, 6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+7, 12, 8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+8, 8, 11, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+9, -6, 10, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+10, -12, 8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+11, -13, 6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+12, -10, 3, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+13, -7, 1, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+14, -8, -3, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+15, -10, -6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+16, -8, -8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+17, -1, -9, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+18, 11, -10, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+19, 12, -8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+20, 9, -5, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+21, 3, -2, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+22, -6, -9, 0)

'------------------------------------------------------------
Local k As Long

For k = %eWayPointStart2 To %eWayPointStart2 + 23
TBGL_EntityCreateBox(%sScene, k, 0, 0.26, 0.26, 1)
TBGL_EntitySetColor(%sScene, k, 50, 50, 250)
Next

TBGL_EntitySetPos(%sScene, %eWayPointStart2+0, -4, 4, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+1, 2, 6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+2, 6, 9, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+3, 8, 8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+4, 11, 7, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+5, 14, 1, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+6, 18, 6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+7, 11, 2, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+8, -9, 12, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+9, -6, 9, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+10, -12, 8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+11, -14, 5, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+12, -11, 4, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+13, -8, 2, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+14, 8, -6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+15, 10, -4, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+16, -6, -6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+17, -2, -8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+18, 6, -11, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+19, 4, -4, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+20, 12, -14, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+21, 4, -2, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+22, -7, -10, 0)


' -- Resets status of all keys
TBGL_ResetKeyState()

Local z, z1 As Long
Local v As Quad
v = 0.1+(Sin(24+GetTickCount/100*24))

'z1 = 0.01*Sin(0.01+Cos(GetTickCount/500)*100))

' -- Main loop
While TBGL_IsWindow(hWnd )
FrameRate = TBGL_GetFrameRate

TBGL_ClearFrame

' NOTE: Pointer offset from track-points
For z = %eHovercraft + 30 To %eHovercraft + 280
HoverCraft_AI(z)
Next
For z1 = %eHovercraft2 + 20 To %eHovercraft2 + 140
HoverCraft_AIX(z*v)
Next
'HoverCraft_AI(%eHovercraftMedium)
'HoverCraft_AI(%eHovercraftLazy)
TBGL_SceneRender(%sScene)

TBGL_DrawFrame

' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
Wend

TBGL_DestroyWindow
END FUNCTION

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sub HoverCraft_AI( entity As Long )
local hoverData AS tHoverCraft ptr

' -- Retrieve data from entity
hoverData = TBGL_EntityGetUserDataPointer(%sScene, entity)

' -- Get angle and distance to next waypoint
Local angle As Double = TBGL_EntityGetAngleXY(%sScene, entity, hoverData.nextWayPoint, %TBGL_Y) ' -- The front part is on Y axis
local distance as double = TBGL_EntityGetDistance(%sScene, entity, hoverData.nextWayPoint)

' -- Move according to parametrization

TBGL_EntityTurn(%sScene, entity, 0, 0, (hoverData.steerFactor*(Min(hoverData.minSpeed+(distance*6), hoverData.maxSpeed*2))*angle)/FrameRate)
TBGL_EntityPush(%sScene, entity, 0, Min(hoverData.minSpeed+(distance*6), hoverData.maxSpeed)/FrameRate, 0)

' -- Check for next waypoints
If distance < hoverData.tolerance Then
If hoverData.nextWayPoint < %eWayPointStart+hoverData.numWayPoint Then
Incr hoverData.nextWayPoint
else
hoverData.nextWayPoint = %eWayPointStart
END IF
END IF

END SUB
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sub HoverCraft_AIX( entity2 As Long )
Local hoverData As tHoverCraft Ptr
Local v As Quad
v = 0.75+(Sin(12+GetTickCount/200*12))

' -- Retrieve data from entity
hoverData = TBGL_EntityGetUserDataPointer(%sScene, entity2)

' -- Get angle and distance to next waypoint
Local angle As Double = TBGL_EntityGetAngleXY(%sScene, entity2, hoverData.nextWayPoint, %TBGL_Y) ' -- The front part is on Y axis
Local distance As Double = TBGL_EntityGetDistance(%sScene, entity2, hoverData.nextWayPoint)
'Local velocity As Double = TBGL_EntityFindNearest(%sScene, entity2, hoverData.nextWayPoint,hoverData.nextWayPoint,Rnd(1,2),2)
Local angle2 As Double = TBGL_EntityGetAngleXY(%sScene, entity2, hoverData.nextWayPoint*Rnd(1,4), %TBGL_X) ' -- The front part is on Y axis

' -- Move according to parametrization
TBGL_EntityTurn(%sScene, entity2, 0, 0, (hoverData.steerFactor*(Min(hoverData.minSpeed+(distance*-0.18), hoverData.maxSpeed*6))*angle*v)/FrameRate)
TBGL_EntityPush(%sScene, entity2, 0, Min(hoverData.minSpeed*-0.01+(distance*7), hoverData.maxSpeed)/FrameRate, 0)

' -- Check for next waypoints
If distance < hoverData.tolerance Then
If hoverData.nextWayPoint < %eWayPointStart2+hoverData.numWayPoint Then
Incr hoverData.nextWayPoint
Else
hoverData.nextWayPoint = %eWayPointStart2
End If
End If

End Sub


one favour: can anybody test this example if it's running fine ? sometimes it's running without problems, next time I cannot start it, don't know why. may be my notebook is having a big cold ;) or it's myself ?

good night, frank

Petr Schreiber
06-03-2010, 13:37
The GPF is caused by fact you have allocated some hovercraft entities (ok), but then you reference their IDs in call:


HoverCraft_AIX(z*v)


using esoteric formula. "Z" is no problem, as it falls in range it should, but "V" is usually zero.

That means you reference entity Z*0 = 0, and as the holy manual of TBGL says :lol:, entityID must not be zero.
This is kind of hack, if you want to pick up random, make sure it is in bounds of generated IDs for entities.

Have a look at great function MinMax, which limits the number to valid range of your choice.

One of the solutions, of course there could be others.


Petr

Lionheart008
06-03-2010, 17:16
thanks petr for hint, I see the dilemma and causing the gpf. fixed both code examples. one example with only random behaviour for triangle swarm and the other with TBGL_EntityFindNearest for velocity (was just an idea).

interesting to see how the swarm reacts for "TBGL_EntityFindNearest". in relationship to random behaviour. I have coloured the second entity2 with darker colour to show the different way and velocity. if you are running this example a certain time, more and more triangles will wander in to lower part of scene. first they are swimming mainly in upper regions, than two, three ones break out and leaving their home ;)

for me this swarm track example is very interesting. I think about robots with their own intelligence or masses crowd in animation films like "lord of the rings" with the evil urucais they were all robots with limited ai handling (go for, left, right, up, down, see enemy, fight and so on.) funny is that some of these monster robots running away to nowhere lands and didn't like any more to fight. I read a very good article about that, but cannot find it. this ai-track example is a good basis for a little game, I know that. I wish to see/build more example with this kind of AI_Track.

thinbasic tbgl code example:

'----------- test for AI hovercraft by lionheart

Uses "TBGL"

BEGIN CONST
' -- Scene IDs
%sScene = 1

' -- Entity IDs
%eCamera = 1
%eLight
%eHovercraft
%eHovercraft2
%eWayPointStart
%eWayPointStart2
END CONST

Randomize Timer

GLOBAL FrameRate AS DOUBLE

FUNCTION TBMAIN()
LOCAL hWnd As DWORD

' -- Create and show window
hWnd = TBGL_CreateWindowEx("Random Test 2b for hovercraft elemental AI_nearest", 880, 620, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

' -- Create scene
TBGL_SceneCreate(%sScene)

' -- Create basic entities
' -- Create camera to look from 15, 15, 15 to 0, 0, 0
TBGL_EntityCreateCamera(%sScene, %eCamera)
TBGL_EntitySetPos(%sScene, %eCamera, 0, 0, 30)
TBGL_EntitySetTargetPos(%sScene, %eCamera, 0, 0, 0)

' -- Create point light
TBGL_EntityCreateLight(%sScene, %eLight)
TBGL_EntitySetPos(%sScene, %eLight, 15, 10, 5)

TBGL_NewList 1
TBGL_UseLighting %FALSE
TBGL_BeginPoly %GL_TRIANGLES
'TBGL_UseBlend %TRUE
TBGL_Vertex -0.10, -0.24
TBGL_Vertex 0.10, -0.24
TBGL_Vertex 0.00, 0.24
TBGL_EndPoly
'TBGL_UseBlend %FALSE
TBGL_UseLighting %TRUE
TBGL_EndList


' -- Create something to look at
TYPE tHoverCraft
nextWayPoint AS LONG
numWayPoint AS LONG
tolerance AS double ' -- Tolerance to reach waypoint
minSpeed AS double
maxSpeed AS double
steerFactor As Double
velocity As Double
END TYPE
dim HoverInfo as tHoverCraft

LOCAL x AS LONG
For x = %eHovercraft + 20 To %eHovercraft + 360
TBGL_EntityCreateDLSlot(%sScene, x, 0, 1)
TBGL_EntitySetColor(%sScene, x, Rnd(10,255), Rnd(10,255), Rnd(10,255))
' -- Assign data to it
with HoverInfo
.nextWayPoint = %eWayPointStart
.numWayPoint = 23
.tolerance = 0.9
.minSpeed = Rnd(60,190)*0.02
.maxSpeed = Rnd(400,450)*0.02
.steerFactor = Rnd(26,46)*0.02
.velocity = Rnd(360*(-0.1),360)
END WITH
TBGL_EntitySetUserData(%sScene, x, HoverInfo)
next

Local i As Long
For i = %eWayPointStart To %eWayPointStart + 23
TBGL_EntityCreateBox(%sScene, i, 0, 0.22, 0.22, 1)
TBGL_EntitySetColor(%sScene, i, 50, 180, 250)
Next

TBGL_NewList 2
TBGL_UseLighting %FALSE
TBGL_BeginPoly %GL_TRIANGLES
'TBGL_COLOR 0,180,250
TBGL_Vertex -0.18, -0.44
TBGL_Vertex 0.18, -0.44
TBGL_Vertex 0.00, 0.44
'TBGL_Vertex -0.18, 0.44
TBGL_EndPoly
'TBGL_UseBlend %FALSE
TBGL_UseLighting %TRUE
TBGL_EndList

' -- Hardcoded waypoints
TBGL_EntitySetPos(%sScene, %eWayPointStart+0, -2, 2, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+1, 0, 4, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+2, 4, 6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+3, 6, 5, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+4, 10, 0, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+5, 13, 1, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+6, 14, 6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+7, 12, 8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+8, 8, 11, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+9, -6, 10, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+10, -12, 8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+11, -13, 6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+12, -10, 3, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+13, -7, 1, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+14, -8, -3, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+15, -10, -6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+16, -8, -8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+17, -1, -9, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+18, 11, -10, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+19, 12, -8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+20, 9, -5, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+21, 3, -2, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+22, -6, -9, 0)

Local y As Long
For y = %eHovercraft2 + 20 To %eHovercraft2 + 80
TBGL_EntityCreateDLSlot(%sScene, y, 0, 1)
TBGL_EntitySetColor(%sScene, y, Rnd(20,55), Rnd(20,55), Rnd(20,55))
' -- Assign data to it
With HoverInfo
.nextWayPoint = %eWayPointStart2
.numWayPoint = Rnd(4,18) '--->14
.tolerance = 0.84
.minSpeed = Rnd(20,60)*0.02
.maxSpeed = Rnd(200,250)*0.02
.steerFactor = Rnd(16,46)*0.02
.velocity = Rnd(160*(-0.1),360)
End With
TBGL_EntitySetUserData(%sScene, y, HoverInfo)
Next

'------------------------------------------------------------
Local k As Long

For k = %eWayPointStart2 To %eWayPointStart2 + 29
TBGL_EntityCreateBox(%sScene, k, 0, 0.26, 0.26, 1)
TBGL_EntitySetColor(%sScene, k, 250, 50, 10)
Next

TBGL_EntitySetPos(%sScene, %eWayPointStart2+0, -4, 4, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+1, 2, 6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+2, 6, 9, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+3, 8, 8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+4, 10, 7, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+5, 13, 1, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+6, 15, 6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+7, 11, 2, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+8, -9, 12, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+9, -6, 9, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+10, -12, 8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+11, -14, 5, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+12, -11, 4, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+13, -8, 2, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+14, 8, -6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+15, 10, -4, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+16, -6, -2, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+17, -2, -8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+18, 6, -10, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+19, 4, -4, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+20, 12, -10, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+21, 4, -2, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+22, -7, -10, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+23, -1, -9, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+24, 11, -10, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+25, 12, -8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+26, 9, -5, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+27, 3, -2, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart2+28, -6, -9, 0)


' -- Resets status of all keys
TBGL_ResetKeyState()

Local z, z1 As Long
Local v As Quad
v = MinMax(0.5+(Sin(24+GetTickCount/100*24)),2,4)

'z1 = 0.01*Sin(0.01+Cos(GetTickCount/500)*100))

' -- Main loop
While TBGL_IsWindow(hWnd )
FrameRate = TBGL_GetFrameRate

TBGL_ClearFrame

' NOTE: Pointer offset from track-points
For z = %eHovercraft + 30 To %eHovercraft + 280
HoverCraft_AI(z)
Next
For z1 = %eHovercraft2 + 20 To %eHovercraft2 + 140
HoverCraft_AIX(v+z)
Next
'HoverCraft_AI(%eHovercraftMedium)
'HoverCraft_AI(%eHovercraftLazy)
TBGL_SceneRender(%sScene)

TBGL_DrawFrame

' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
Wend

TBGL_DestroyWindow
END FUNCTION

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sub HoverCraft_AI( entity As Long )
Local hoverData As tHoverCraft Ptr
Local v As Long

v = MinMax(0.5+(Sin(24+GetTickCount/100*24)),2,4)

' -- Retrieve data from entity
hoverData = TBGL_EntityGetUserDataPointer(%sScene, entity)

' -- Get angle and distance to next waypoint
Local angle As Double = TBGL_EntityGetAngleXY(%sScene, entity, hoverData.nextWayPoint, %TBGL_Y) ' -- The front part is on Y axis
Local distance As Double = TBGL_EntityGetDistance(%sScene, entity, hoverData.nextWayPoint)

' -- Move according to parametrization

TBGL_EntityTurn(%sScene, entity, 0, 0, (hoverData.steerFactor*(Min(hoverData.minSpeed+(distance*4), hoverData.maxSpeed*2))*angle+v)/FrameRate)
TBGL_EntityPush(%sScene, entity, 0, Min(hoverData.minSpeed+(distance*4), hoverData.maxSpeed)/FrameRate, 0)

' -- Check for next waypoints
If distance < hoverData.tolerance Then
If hoverData.nextWayPoint < %eWayPointStart+hoverData.numWayPoint Then
Incr hoverData.nextWayPoint
else
hoverData.nextWayPoint = %eWayPointStart
END IF
END IF

END SUB
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sub HoverCraft_AIX( entity2 As Long )
Local hoverData As tHoverCraft Ptr
Local v As Quad
v = 0.75+(Sin(12+GetTickCount/200*12))

' -- Retrieve data from entity
hoverData = TBGL_EntityGetUserDataPointer(%sScene, entity2)

' -- Get angle and distance to next waypoint
Local angle As Double = TBGL_EntityGetAngleXY(%sScene, entity2, hoverData.nextWayPoint, %TBGL_Y) ' -- The front part is on Y axis
Local distance As Double = TBGL_EntityGetDistance(%sScene, entity2, hoverData.nextWayPoint)
Local velocity As Double = TBGL_EntityFindNearest(%sScene, entity2, hoverData.nextWayPoint,hoverData.nextWayPoint,Rnd(1,2),4)
Local angle2 As Double = TBGL_EntityGetAngleXY(%sScene, entity2, hoverData.nextWayPoint*Rnd(1,4), %TBGL_X) ' -- The front part is on Y axis

' -- Move according to parametrization
TBGL_EntityTurn(%sScene, entity2, 0, 0, (hoverData.steerFactor*(Min(hoverData.minSpeed+(distance*-0.18), hoverData.maxSpeed*6))*angle2*v)/FrameRate)
TBGL_EntityPush(%sScene, entity2, 0, Min(hoverData.minSpeed*-0.01+(distance*7), hoverData.maxSpeed)/FrameRate, 0)

' -- Check for next waypoints
If distance < hoverData.tolerance Then
If hoverData.nextWayPoint < %eWayPointStart2+hoverData.numWayPoint Then
Incr hoverData.nextWayPoint
Else
hoverData.nextWayPoint = %eWayPointStart2
End If
End If

End Sub


new edit: a third version I add here because I have noticed that for second entity2 there is not the right arrange for waypoints (here: 29), so if you like you can test the different. have fun with it.


With HoverInfo
.nextWayPoint = %eWayPointStart2
.numWayPoint = Rnd(4,29) '---> here: 29 waypoints for entity2 ! :)
.tolerance = 0.84
.minSpeed = Rnd(20,60)*0.02
.maxSpeed = Rnd(200,250)*0.02
.steerFactor = Rnd(16,46)*0.02
.velocity = Rnd(160*(-0.1),360)
End With

3) last not least: there is a kind of short pulsing (turning and re-arranging the triangles!) after regarding the scene for some seconds by "GetTickCount" command for entity2 swarm. I like that very mucho !

v = 0.75+(Sin(12+GetTickCount/200*12))


TBGL_EntityTurn(%sScene, entity2, 0, 0, (hoverData.steerFactor*(Min(hoverData.minSpeed+(distance*-0.18), hoverData.maxSpeed*6))*angle*v)/FrameRate)

nice day, frank

Petr Schreiber
06-03-2010, 17:38
Hi Frank,

nice, very hypnotic to watch!

This is interesting topic, the crowd scenes... let me know if you find that article on Lord of the Rings "uruk-hai" artificial inteligence, I would be happy to read it.


Petr

Lionheart008
06-03-2010, 21:54
hi petr.

I will have a special look for that article, because I have throwing away this magazine some years before into the rubbish for old paper for recycling. I will find that special article, I know the magazine and an editor in their office. I can get a copy I am sure. If I have this article you will get it, be sure :)


let me know if you find that article on Lord of the Rings "uruk-hai" artificial inteligence, I would be happy to read it.

I have a very interesting link to this software for masses:

www.massivesoftware.com

as you can imagine this big software is not for free ;(

best regards, frank lionhead

Petr Schreiber
06-03-2010, 21:59
Thanks for the info,

I will checkout the website, sounds interesting.


Petr