So i took a code i had written for a 2D space, and used a peasy cam, and have since brought it to the 3d environment. The only problem is i cannot get the ball to move around the space towards the attractors. any help?
import peasy.*;
PeasyCam cam;
float [] xpos = new float[7000];
float [] ypos = new float [7000];
float [] zpos = new float [7000];
Ball[] ballies = new Ball[2];
attractor[] attractor = new attractor[25];
void setup() {
size(800, 800, P3D);
smooth();
cam = new PeasyCam(this, 100);
cam.setMinimumDistance(100);
cam.setMaximumDistance(200);
for (int i=0; i<ballies.length; i++) {
ballies[i] = new Ball(random(width), random(height), random(300), random(0,2),random(0,2),25,50);
}
for (int p=0; p < attractor.length; p++) {
attractor[p] = new attractor( width/8 + random(width), height/8 + random(height), random(300));
smooth();
}
}
void draw() {
background(0);
stroke(250,250,250);
strokeWeight(2);
point(xpos[0], ypos[0], zpos[0]);
for (int g = 1; g <5000; g++){
point(xpos[g], ypos[g], zpos[g]);
}
for (int h = 0; h < 100; h++){//draw x axis
stroke(255,h*5,0);
point(h,0,0);
}
for (int h = 0; h < 100; h++){// draw y axis
stroke(h*5,255,0);
point(0,h,0);
}
for (int h = 0; h < 100; h++){// draw z axis
strokeWeight(3);
stroke(0,255,h*5);
point(0,0,h);
}
for (int p=0; p < attractor.length; p++) {
attractor[p].update();
}
for (int i=0; i<ballies.length; i++) {
ballies[i].update();
}
}
class Ball {
// ball fields
float x;
float y;
float z;
float speedX;
float speedY;
float speedZ;
int normSize;
float angA;
float angV = 0;
boolean dead = false;
PVector a;
PVector v, p, standardV, offsetV;
// ball constructor
Ball(float _x, float _y, float _z, float _speedX, float _speedY,float _speedZ, int _normSize) {
p = new PVector(_x, _y, _z);
v = new PVector(_speedX, _speedY, _speedZ);
standardV = new PVector(_speedX, _speedY, _speedZ);
normSize = _normSize;
}
void update() {
if (!dead) {
a = new PVector();
for (int i = 0; i < attractor.length; i++) {
a.add(attractor[i].getAccel(p.x,p.y,p.z));
}
angA = a.heading2D();
angV += angA/.25;
v = new PVector( standardV.x, standardV.y, standardV.z);
a.normalize();
a.mult(v.mag());
a.mult(0.9);
v.add(a);
checkCollisions();
p.add(v);
angV *= .9;
}
drawBall();
}
void drawBall() {
pushMatrix();
translate(x,y,z);
sphere(2);
popMatrix();
}
void checkCollisions() {
if(p.x + v.x > width || p.x + v.x < 0) {
v.x *= -1;
standardV.x *= -1;
}
if(p.y + v.y > height || p.y + v.y < 0) {
v.y *= -1;
}
if(p.z + v.z > height || p.z + v.z < 0) {
v.z *= -1;
}
}
}
/////////////////////////////////////////////////////
class attractor {
float x,y,z;
attractor(float _x, float _y, float _z ) {
x = _x;
y = _y;
z = _z;
}
void update() {
render();
}
void render() {
stroke(255,0,0);
pushMatrix();
translate(x,y,z);
box(5);
popMatrix();
}
PVector getAccel(float px, float py, float pz) {
PVector a = new PVector(0,0,0);
float d2 = sq(dist(px,py,pz,x,y,z));
if (d2 > 10) {
a.x = G * (x-px) / d2;
a.y = G * (y-py) / d2;
a.z = G * (z-pz) / d2;
}
return a;
}
}
1