can objects orbit attractors in 3D?
in
Programming Questions
•
2 years ago
I have been looking at a lot of gravity fields and attractors, but I have yet to see them move in the x,y, and z axis.
http://www.openprocessing.org/visuals/?visualID=9151 : this does an awesome job in 2d with 3d representation, and what im looking to do is something similar, but the planets will move in the z direction as well as having fields / attractors scattered about the z axis.
attached is the code I personally have been working with and creating. My major problem is not knowing how to classify the vectors in 3D, i guess. I have the attractors scattered in 3D, but the balls keep coming in on 0,0,0 for some reason, and I cannot get any movement to them.
I have been struggling with this for a few days and so far have gotten nowhere. if anyone more knowledgeable than me help I will be more than thankful
import peasy.*;
PeasyCam cam;
float [] xpos = new float[800];
float [] ypos = new float [800];
float [] zpos = new float [800];
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(10), random(20),random(80,2),random(90,2),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 <800; 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 *= 12;
}
drawBall();
}
void drawBall() {
pushMatrix();
translate(x,y,z);
sphere(1);
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