With the help of my good friend Wade, I have started converting the Boid Pseudocode to work in Java. This is nothing like as easy as it sounds though.
Currently the program uses two classes, one to generate the individual boids and the other to calculate vectors based on the global rules.
BOID MAIN CODE
/*
RGB Cube Mod
Here are some edits to get a framework
for understanding RGB Cube and starting
on the next step.
*/
import toxi.geom.*;
import processing.opengl.*;
import javax.media.opengl.*;
float xmag, ymag = 0; //starting variables to define X and Y rotation
float newXmag, newYmag = 0; //Same but used to define new variables as the mouse moves
float i = 1;
Vec3D startPos;
Vec3D vel;
Vec3D pos;
BoidArray bA;
PGraphicsOpenGL pgl;
GL gl;
void setup() {
size(800, 400, OPENGL );
hint( ENABLE_OPENGL_4X_SMOOTH );
colorMode(RGB, 1);
pgl = (PGraphicsOpenGL) g;
gl = pgl.gl;
gl.setSwapInterval(1);
stroke(1, 1, 1);
bA = new BoidArray(2)
}
void draw() {
background(0.5, 0.5, 0.45);
directionalLight(204, 204, 204, 0, 0, -1);
noStroke();
translate(width/2, height/2, -30);
s.drawBoid();
moveBoid();
// for () {
// boid[i].drawBoid();
// s.moveBoid();
}
BOID ARRAY CLASS
Class BoidArray {
Boid[] Arr;
BoidArray(int size1) {
Arr = new Boid[size1];
populate(Arr);
}
populate(Boid[] R) {
Boid s = new Boid(0,0,0);
Bois v = new Boid(1,1,1);
R(0) = s;
R(1) = v;
}
moveBoids() {
// declare v1 etc
Vec3d v1;
Vec3d v2;
Vec3d v3;
for (int j=0,j-lessthan-Arr.size;j++) {
v1 = rule1(j);
v2 = rule2(j);
v3 = rule3(j);
Arr[j].moveBoid(v1,v2,v3);
}
}
// Arr[i].pos is b.position
// Arr[j].pos is bj.position
// total = pcj
rule1(j) {
Vec3D total = new Vec3d(0,0,0)
// Each Boid
for (int i=0;i-lessthan-Arr.size;i++) {
// This is the if != part
if (i != j) {
total = total.add(Arr[i].pos);
}
}
float x = total.x;
float y = total.y;
float z = total.z;
x = x / (Arr.size - 1)
y = y / (Arr.size - 1)
z = z / (Arr.size - 1)
x = ( x - Arr[j].x ) / 100
y = ( y - Arr[j].y ) / 100
z = ( z - Arr[j].z ) / 100
return new Vec3d(x,y,z)
}
// Arr[i].pos is b.position
// Arr[j].pos is bj.position
// c = c
rule2(j) {
Vec3D c = new Vec3d(0,0,0)
// Each Boid
for (int i=0;i-lessthan-Arr.size;i++) {
// This is the if != part
if (i != j) {
if ( (Arr[i].pos - Arr[j].pos) -lessthan- 100 ) {
c = c.sub(Arr[i].pos.sub(Arr[j].pos));
}
}
return c;
}
// pvjTotal = pvj
// Arr[j].vel = b.velocity
// And the returned value is calculated from the x,y,z values individually
// N = Arr.size
rule3(j) {
Vec3D pvjTotal = new Vec3d(0,0,0)
// Each Boid
for (int i=0;i-lessthan-Arr.size;i++) {
// This is the if != part
// This ONLY calculates the pvj, it does not do the +b.velocity
if (i != j) {
pvjTotal = pvjTotal.add(Arr[i].vel);
}
}
// This is where you do first pvj + b.velocity ( Remember b.velocity != bj.velocity)
// Then you do the pvj = pvj/N-1
// and finally pvj - bjvelocity / 8 return
float x = total.x;
float y = total.y;
float z = total.z;
x = x / (Arr.size - 1)
y = y / (Arr.size - 1)
z = z / (Arr.size - 1)
x = ( x - Arr[j].x ) / 100
y = ( y - Arr[j].y ) / 100
z = ( z - Arr[j].z ) / 100
return c;
}
}
BOID INDIVIDUAL CLASS
//the Boid class is intended to hold and change
class Boid{
Vec3D pos;
Vec3D vel;
Boid(float xpos,float xvel,ypos, yvel,z) {
pos = new Vec3D(xpos, 0, 0);
vel = new Vec3D(xvel, 0, 0);
}
drawBoid() {
sphere(2);
}
moveBoid(v1,v2,v3) {
vel = vel.add(v1.add(v2.add(v3))));
pos = pos.add(vel);
}
toString() {
println(pos.x + " This is pos.x - " + pos.y)
}
}
Labels:
Input Device,
Semester 2
Subscribe to:
Post Comments (Atom)
0 comments: