Page 1 of 1

Search for Ki program to create a fish swarm

Posted: Mon Dec 21, 2009 11:44 pm
by mpz
Hi,

i ask in the German forum, but nobody could help me. I play a little bit with Pseudo-fish Meshs code, but the fishs makes only easy moves. Now i need a code or idea for a code to simulate swarm ki. you know a big fisk will eat the little one and they move as swarm away. Does anybody has a code or link to a code or somethink else to makes this or any ideas?

I have 120 fishes, a fish has a position, Vector, speed and need "only" an intelligence

Greetings :D
Michael


Windows program for testing
http://em.q-soft.ch/files/get/-wXiPrUIPX/fishes.exe


MP3D example Code, sorry it is not possible for me to translate it to Ogre...

Code: Select all

;-
;- ProgrammStart
;////////////////////////////////////////////////////////////////
;//
;// Project Title: MP Fishes!
;// File Title: MP_Fishes.pb
;// Created On: 19.12.2009
;// Updated On: 
;// Author: Michael Paulwitz
;// OS:Windows
;// 
;// pseudo-fish meshs 
;// 
;////////////////////////////////////////////////////////////////

;Funny variables

Structure TFish
  
  Entity.i
  Yaw.f
  Speed.f
  
EndStructure

;Create a fish!
Procedure CreateFish()
	mesh = MP_CreateMesh()
	
	v0 = MP_AddVertex(mesh, 1, 0, 0)
	v1 = MP_AddVertex(mesh, 0, -0.5, 0)
	v2 = MP_AddVertex(mesh, 0, 0.5, 0)
	v3 = MP_AddVertex(mesh, -1, 0, -0.5)
	v4 = MP_AddVertex(mesh, -1, 0, 0.5)
	v5 = MP_AddVertex(mesh, -2, 0.5, 0)
	v6 = MP_AddVertex(mesh, MP_RandomFloat(-2.5, -1), MP_RandomFloat(1, 1.2), 0)
	v7 = MP_AddVertex(mesh, -2, -0.5, 0)
	v8 = MP_AddVertex(mesh, -3, -0.8, 0)
	v9 = MP_AddVertex(mesh, -3.5, -0.5, 0)
	v10 = MP_AddVertex(mesh, -3.5, 1.5, 0)
	v11 = MP_AddVertex(mesh, -1, MP_RandomFloat(0.5, -1), 0)
	
	;Head
	MP_AddTriangle (mesh, v3, v2, v0)
	MP_AddTriangle (mesh, v0, v2, v4)
	MP_AddTriangle (mesh, v1, v3, v0)
	MP_AddTriangle (mesh, v0, v4, v1)
	MP_AddTriangle (mesh, v3, v5, v2)
	MP_AddTriangle (mesh, v2, v5, v4)

	;Fin
	MP_AddTriangle (mesh, v5, v6, v2)
	MP_AddTriangle (mesh, v2, v6, v5)
	
	;Body
	MP_AddTriangle (mesh, v3, v7, v5)
	MP_AddTriangle (mesh, v5, v7, v4)
	MP_AddTriangle (mesh, v3, v1, v7)
	MP_AddTriangle (mesh, v7, v1, v4)
	
	;Back-fin
	MP_AddTriangle (mesh, v7, v10, v5)
	MP_AddTriangle (mesh, v5, v10, v7)
	MP_AddTriangle (mesh, v7, v9, v10)
	MP_AddTriangle (mesh, v10,v9, v7)
	MP_AddTriangle (mesh, v8, v9, v7)
	MP_AddTriangle (mesh, v7, v9, v8)
	
	;Under-fin
	MP_AddTriangle (mesh, v11, v7, v1)
	MP_AddTriangle (mesh, v1, v7, v11)

  MP_EntitySetNormals (Mesh)
	
	;Color fish!
	For i = 0 To 11
	    MP_VertexSetColor(mesh, i ,RGB(MP_RandomInt(0, 255),MP_RandomInt(0, 255),MP_RandomInt(0, 255)))
	Next
	
	ProcedureReturn mesh
EndProcedure


;Animate a fish!
Procedure AnimateFish(mesh, an.f)

	MP_VertexSetZ(mesh, 10, Sin(an*0.017453)*0.5)
	MP_VertexSetZ(mesh, 9, Sin(an*0.017453)*0.5)
	MP_VertexSetZ(mesh, 8, Sin(an*0.017453)*0.5)
	MP_VertexSetZ(mesh, 6, Cos(an*0.017453)*-0.2)
	MP_VertexSetZ(mesh, 5, Cos(an*0.017453)*-0.2)
	MP_VertexSetZ(mesh, 7, Cos(an*0.017453)*-0.2)
	MP_VertexSetZ(mesh, 11, Cos(an*0.017453)*-0.2)
		
	MP_VertexSetZ(mesh, 3, -0.5+Cos(an*0.017453)*-0.1)
	MP_VertexSetZ(mesh, 4, 0.5+Cos(an*0.017453)*-0.1)
	MP_VertexSetZ(mesh, 2, Sin(an*0.017453)*-0.1)
	MP_VertexSetZ(mesh, 1, Sin(an*0.017453)*-0.1)

EndProcedure

Dim fishes.TFish(119)

MP_Graphics3D (800,600,0,2) ; Erstelle ein WindowsFenster #Window = 0
SetWindowTitle(0, "Fishes! with Purbasic and MP3D Engine") 

camera=MP_CreateCamera() ; Kamera erstellen
MP_CameraSetPerspective(camera, 90) 
MP_PositionEntity(camera,0,50,-50)
MP_RotateEntity (camera, 45, 0, 0)

light=MP_CreateLight(1)

MP_AmbientSetLight (RGB(64,64,255))
MP_Fog (RGB(64,64,255),10,100)

;Create 120 fishes!
For i = 0 To 119
  fishes(i)\Entity = CreateFish()
	fishes(i)\Yaw = MP_RandomFloat(-1, 1)
	fishes(i)\Speed = MP_RandomFloat(0.1, 0.5)
	MP_PositionEntity (fishes(i)\Entity, MP_RandomInt(-50, 50), MP_RandomInt(-50, 50), MP_RandomInt(-50, 50))
	MP_RotateEntity (fishes(i)\Entity, 0, MP_RandomInt(0, 360), MP_RandomInt(-45, 45))
Next

MP_V2M_OpenMem(?theTune1) ; LoadSong and go
MP_V2M_Play(0)


While Not MP_KeyDown(#PB_Key_Escape) And Not MP_WindowEvent() = #PB_Event_CloseWindow; Esc or close

    If Not MP_V2M_IsPlaying()     
       MP_V2M_Play(0)
    EndIf

    For i = 0 To 119
		  AnimateFish(fishes(i)\Entity, an + 90*i)
		  MP_MoveEntity( fishes(i)\Entity, fishes(i)\Speed, 0, 0)
		  MP_TurnEntity( fishes(i)\Entity, 0,0,fishes(i)\Yaw)
	  Next
	
	  an = an + MP_RandomFloat(10, 20)
	  an = an % 360

    MP_RenderWorld () ; Hier gehts los
    MP_Flip () ; 

Wend

DataSection

  theTune1:
  IncludeBinary "synthetic panorama_new.v2m"

EndDataSection
  

Re: Search for Ki program to create a fish swarm

Posted: Tue Dec 22, 2009 2:24 am
by IdeasVacuum
Hi mpz

The fish essentially swim along a curved or S shaped or wiggly path right? So, if the path is represented by a spline curve, you need to transform each fish along it's own path, i.e. define n number of points along the spline and transform the fish from point to point to point.

Re: Search for Ki program to create a fish swarm

Posted: Tue Dec 22, 2009 1:56 pm
by mpz
Hi Ideas Vacuum,

the fishes swin in a curve now. The idea with spline curve is nice. I have see some times before a code for that here in the forum. Then i need some points for a path and algorythmus for the distance between the fishes in the swarm.


Greetings Michael

Re: Search for Ki program to create a fish swarm

Posted: Wed Dec 23, 2009 12:15 am
by IdeasVacuum
You could cheat.....

Use a CAD program to define splines and array points on them. Collect the point coordinates and store them as Data in your app. The advantage is that you should get smooth, accurate movement with minimal code.

Re: Search for Ki program to create a fish swarm

Posted: Wed Dec 23, 2009 5:19 am
by Kaeru Gaman
if you want to create halfways realistic movement, you'll need interactive Sub-AI.

each fish controls his movement on his own.

he follows the fish in front of him, this is the main influence.

he keeps distance to the fishes around him:
when the distance gets smaller than a limit value, he steers away from that neighbor fish,
when the distance gets bigger than a limit value, he steers towards that neighbor fish.

the first fish can follow an arbitrary path, like a spline.
or make it not a single first fish and let them all search for food.

and additional every fish should have a flee distance towards prey enemies, if you want to add enemies.
you can leave them out to get started easier.

this model should be realizable with only a handfull of pseudo-neurons.

Re: Search for Ki program to create a fish swarm

Posted: Sat Jan 02, 2010 12:32 pm
by Yewklid
The classic work on swarming behaviour is Boids by Craig Reynolds. You can find a lot of helpful stuff on his website.