Question

So I'm working on porting Boids to Brightscript, based on the pseudocode here.

I'm trying to understand the data structures involved, for example is Velocity a single value, or is it a 3D value? (i.e. velocity={x,y,z})

It seems as if the pseudocode seems to mix this up where sometimes it has an equation that incudes both vectors and single-value items:

v1 = rule1(b)
v2 = rule2(b)
v3 = rule3(b)

b.velocity = b.velocity + v1 + v2 + v3

If Velocity is a tripartite value then this would make sense, but I'm not sure.

So, my first question: based on the pseudocode above, is this the correct data structure for a single boid?

boid={position:{px:0,py:0,pz:0},velocity:{x:0,y:0,z:0},vector:{x:0,y:0,z:0},pc:{x:0,y:0,z:0},pv:{x:0,y:0,z:0})

where pc=perceived center, and pv= perceived velocity.

I've implemented a vector_add, vector_sub, vector_div, and vector boolean functions.

The reason I'm starting from this pseudocode is I've not been able to find anything else that is as readable, but it still leaves me with lots of questions as the data structures are not explicitly defined for each variable.

(edit) Here's a good example of what I'm talking about:

IF |b.position - bJ.position| < 100 THEN

if b.position - b[j].position are both 3D coordinates, how can they be considered "less than 100" unless they are < {100,100,100}?

No correct solution

OTHER TIPS

Velocity

If you look at their pseudocode for vector addition and subtraction, they are explicitly performing these operations on three-dimensional vectors, e.g.

PROCEDURE Vector_Add(Vector v1, Vector v2)
    Vector v
    v.x = v1.x + v2.x
    v.y = v1.y + v2.y
    v.z = v1.z + v2.z
    RETURN v
END PROCEDURE

In the Auxiliary Functions section, it also says:

... all the additions and subtractions in the above pseudocode are vector operations

So we can assume b.velocity = b.velocity + v1 + v2 + v3 + ... is vector addition for 3D vectors.

Vector Magnitude

The subtraction of the two vectors is called the difference between vectors, and produces a new vector. In your case, let diffB = b.position - bJ.position.

Now |b.position - bJ.position| is equivalent to |diffB|, and is taking the magnitude of the difference vector diffB, not the absolute value of a "single value" (called a scalar.) Magnitude is also called vector length, or norm.

What may be confusing is that the magnitude of a vector is denoted by the same notation as absolute value. So diffB is a difference vector, and |diffB| is the magnitude of that vector. Magnitude is defined for a vector v in Euclidean space by |v| = sqrt(x1^2 + ... + xn^2).

So, for your 3D vector diffB:

|diffB| = sqrt(x1^2 + x2^2 + x3^2) = sqrt(x^2 + y^2 + z^2)

Since the result of square root is a scalar, it can clearly satisfy < 100.


So yes, I believe velocity is a 3D vector velocity = {x1, x2, x3}, and while I haven't thoroughly reviewed the boid pseudocode, your data structure appears correct.

What is ment here by the expression:

|b.position - bJ.position|

Is actually a scalar from the difference of the two vectors.

This scalar is a single value and can thus be < 100.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top