rvb Swarm

rvb animation snippet example

Swarming in rhino! You can select the amount of objects in the scene and how many iterations you want the simulation to run.

Option explicit

'Script by archiologics · www.archiologics.com

' Modified by Adolfo Nadal

'original author: ivo stotz

Call Main()

Sub Main()

Dim ArrBots, i, idBot, Size, Center, arrPoints, strPath, iter

iter      = 1500

strPath   = rhino.GetObject("select the path", 4)

If Not rhino.IsCurve(strPath) Then Exit Sub

arrPoints = rhino.DivideCurve(strPath, iter+1)

'Rhino.DeleteObject strPath

size      = 6

idBot     = "Nothing" 

 

Call rhino.EnableRedraw(False)

arrBots  = MakeBots(50, array(0,0,0), array(260,260,260), size)

Call rhino.EnableRedraw(True)

Center   = BotCentroid(arrBots)

 

For i = 0 To iter

'Rhino.getreal

Call rhino.EnableRedraw(False)

'Center   = BotCentroid(arrBots)

If rhino.IsPoint(strPath) Then rhino.DeleteObject(strPath)

center   = arrPoints(i)

strPath  = rhino.AddPoint(arrPoints(i))

Call rhino.ObjectColor(strPath, rgb(255,0,0))

arrBots  = MoveBots(arrBots, Center, size)

Call rhino.EnableRedraw(True)

Next

End Sub

 

Function MoveBots(arrBots, Point, size)

Dim Vec, l, tmpBots, P1, P2, i

tmpBots = arrBots

For i = 0 To ubound(arrBots)

vec = Direction(arrBots, arrBots(i)(1),arrBots(i)(2), Point)

P1  = MovePtVec(arrBots(i)(1), vec, 1)

P2  = MovePtVec(arrBots(i)(1), vec, 2)

TmpBots(i) = array(drawBot(p1, p2, size), P1, P2)

rhino.DeleteObject arrBots(i)(0)

Next

MoveBots = TmpBots

End Function

 

Function Direction(arrBots, P1, P2, point)

Dim Vcntr, Vbot, Vswarm, Valpha, V0, Vdir, Center

Center    = BotCentroid(arrBots)

Vcntr     = MultiplyVec(UnitVec(Vector(P1, Center)), 0.2)

V0        = multiplyvec(unitvec(Vector(p1,point)),0.6)

Valpha    = MultiplyVec(UnitVec(Vector(arrBots(1)(1), arrBots(1)(2))), 0.03)'array(0,0,0)'

Vbot      = Vector(P1, P2)

Vswarm    = MultiplyVec(SwarmDirection(arrBots),  0.01)

Vdir      = addvec(addvec(addvec(addVec(vcntr, vBot), vswarm), Valpha), v0)

Do While LengthVec(Vdir) > 12  

Vdir = multiplyVec(Vdir, 0.9)

Loop

Direction = Vdir

End Function

 

Function BotCentroid(arrBots)

Dim x, y, z, i, Bound

bound = ubound(arrBots)

x = 0

y = 0

For i = 0 To Bound

x = x + arrBots(i)(1)(0)

y = y + arrBots(i)(1)(1)

z = z + arrBots(i)(1)(2)

Next 

BotCentroid = array(x/(bound+1), y/(bound+1), z/(bound+1))

End Function

 

Function MakeBots(nb, Pmin, Pmax, size)

Dim Id, P1, P2, i, ArrBots()

ReDim ArrBots(nb-1)

Randomize

For i = 0 To nb-1

P1 = Array((pmax(0)-pmin(0))*rnd, (pmax(1)-pmin(1))*rnd, (pmax(2)-pmin(2))*rnd)

P2 = Array((pmax(0)-pmin(0))*rnd, (pmax(1)-pmin(1))*rnd, (pmax(2)-pmin(2))*rnd)

P2 = MovePtVec(P1, unitvec(vector(P1,P2)), size)

id = drawBot(P1, P2, size)

ArrBots(i) = array(id, P1, P2)

Next

MakeBots = ArrBots

End Function

 

Function SwarmDirection(arrBots)

Dim TempVec, Vbot, i

TempVec = array(0,0,0) 

For i = 0 To ubound(arrBots)

Vbot = Vector(arrBots(i)(1),arrBots(i)(2))

TempVec = addVec(tempVec, Vbot) 

Next

SwarmDirection = unitVec(TempVec)

End Function

 

Function drawBot(sPt, ePt, size)

Dim strBot

strBot  = Rhino.AddSrfPt(Array(array(-1*size,0,0),array(1*size,0,0),array(0,1.3*size,0),array(-1*size,0,0)))

drawBot = rhino.OrientObject(strBot, Array(Array(0,0,0), Array(0,1,0)), Array(sPt, ePt))

End Function

 

'__________________________________________________________________________________

'   NAME          MovePtVec

'   DESCRIPTION   Creates a 3D-point strarting from a 3D-point while

'                 adding n times along the given vector

'   ARGUMENTS     Point       - an array of three 3D-coordinates of a Point (x,y,z)

'                 vec         - an array of three 3D-Points defining a vector

'                 d           - the factor

'   RETURN        ReturnPoint - an array of three 3D-coordinates of a Point

'__________________________________________________________________________________

Function MovePtVec(Point, vec, d)

Dim ReturnPoint(2)

ReturnPoint(0) = Point(0) + (d*vec(0))

ReturnPoint(1) = Point(1) + (d*vec(1))

ReturnPoint(2) = Point(2) + (d*vec(2))

MovePtVec = ReturnPoint

End Function

 

'__________________________________________________________________________________

'   NAME          MultiplyVec

'   DESCRIPTION

'   ARGUMENTS

'                 vec         - an array of three 3D-Points defining a vector

'__________________________________________________________________________________

Function MultiplyVec(vec, d)

Dim ReturnVec(2)

ReturnVec(0) = (d*vec(0))

ReturnVec(1) = (d*vec(1))

ReturnVec(2) = (d*vec(2))

MultiplyVec = ReturnVec

End Function

 

'__________________________________________________________________________________

'   NAME          OrthoVec

'   DESCRIPTION   creates a unitarian vector perpendicular to vec

'   ARGUMENTS

'                 vec         - an array of three 3D-Points defining a vector

'__________________________________________________________________________________

Function OrthoVec(vec, normVec)

OrthoVec = ProductVec(UnitVec(vec), UnitVec(normVec))

End Function

 

'__________________________________________________________________________________

'   NAME          SkalarVec

'   ARGUMENTS

'                 vec         - an array of three 3D-Points defining a vector

'__________________________________________________________________________________

Function SkalarVec(vec1, vec2)

SkalarVec = CosVec(vec2, UnitVec(vec1))

End Function

 

'__________________________________________________________________________________

'   NAME          CosVec

'   DESCRIPTION   Computes the cosinus of the angle between two vectors

'   ARGUMENTS     vec1    - an array of three 3D-Points defining vector 1

'                 vec2    - an array of three 3D-Points defining vector 2

'   RETURN        CosVec  - a double containing the cosine value

'__________________________________________________________________________________

Function  CosVec(vec1, vec2)

CosVec = (vec1(0)*vec2(0)) + (vec1(1)*vec2(1)) + (vec1(2)*vec2(2))

End Function

 

'__________________________________________________________________________________

'   NAME          UnitVec

'   DESCRIPTION   Creates a unit-vector of a given vector

'   ARGUMENTS     vec     - an array of three 3D-Points defining the entry vector

'   RETURN        TempVec - an array of three 3D-Points defining the unit-vector

'__________________________________________________________________________________

Function UnitVec(vec)

Dim TempVec

Dim fact

TempVec = vec

fact = Sqr((vec(0)*vec(0))+(vec(1)*vec(1))+(vec(2)*vec(2)))

TempVec(0) = TempVec(0)/fact

TempVec(1) = TempVec(1)/fact

TempVec(2) = TempVec(2)/fact

UnitVec = TempVec

End Function

 

'__________________________________________________________________________________

'   NAME          LengthVec

'   DESCRIPTION   Calculates the length of a given vector

'   ARGUMENTS     vec       - an array of three 3D-Points defining the entry vector

'   RETURN        LengthVec - double containing the length of the vector

'__________________________________________________________________________________

Function LengthVec(vec)

LengthVec = Sqr((vec(0)*vec(0))+(vec(1)*vec(1))+(vec(2)*vec(2)))

End Function

 

'__________________________________________________________________________________

'   NAME          ProductVec

'   DESCRIPTION   Computes the vector product of two entry vectors

'   ARGUMENTS     vec1    - array of three 3D-Points defining the entry vector 1

'                 vec2    - array of three 3D-Points defining the entry vector 2

'   RETURN        TempVec - array of three 3D-Points defining the product vector

'__________________________________________________________________________________

Function ProductVec(vec1, vec2)

Dim TempVec(2)

tempVec(0) = (vec1(1)*vec2(2)) - (vec1(2)*vec2(1))

tempVec(1) = (vec1(2)*vec2(0)) - (vec1(0)*vec2(2))

tempVec(2) = (vec1(0)*vec2(1)) - (vec1(1)*vec2(0))

ProductVec = TempVec

End Function

 

'__________________________________________________________________________________

'   NAME          AddVec

'   DESCRIPTION   Computes addition of two vectors

'   ARGUMENTS     vec1    - array of three 3D-Points defining the entry vector 1

'                 vec2    - array of three 3D-Points defining the entry vector 2

'   RETURN        TempVec - array of three 3D-Points defining the product vector

'__________________________________________________________________________________

Function AddVec(vec1, vec2)

Dim TempVec(2)

tempVec(0) = (vec1(0)+vec2(0))

tempVec(1) = (vec1(1)+vec2(1))

tempVec(2) = (vec1(2)+vec2(2))

AddVec = TempVec

End Function

 

'__________________________________________________________________________________

'   NAME          Vector

'   DESCRIPTION   Computes the vector which strarts from point 1 and ends at point 2

'   ARGUMENTS     Point1  - an array of three 3D-coordinates of point 1 (x,y,z)

'                 Point2  - an array of three 3D-coordinates of point 2 (x,y,z)

'   RETURN        vec     - array of three 3D-Points defining the resulting vector

'__________________________________________________________________________________

Function Vector(Point1, Point2)

Dim vec(2)

vec(0) = Point2(0) - Point1(0)

vec(1) = Point2(1) - Point1(1)

vec(2) = Point2(2) - Point1(2)

Vector = vec

End Function

Search Site




archi·o·logics survey

I am interested in: