hey all
i'm trying to rotate objects on the surface of a sphere and test to see if they hit each other.
I've worked out the simple 2D rotation, but can't figure out how to rotate on the Z axis as well (shouldn't have napped through high school maths). tried to read up on it, but been going round in circles for hours.
and secondly how would i test for the collision on z-axis as well.
Something tells me i should be using Vectors
any help appreciated.
here's the code so far:
Code:
class Circs{
float x, y, z, angle, speed;
float sphereSize;
int me;
Circs(int _me, float _angle) {
angle = _angle;
me = _me;
sphereSize= random(15, 40);
speed = random(-1, 1);
if (speed > -0.1 && speed < 0.1) speed = 0.4;
}
void draw(){
update();
fill(255);
pushMatrix();
translate(x, y, z);
sphere(sphereSize);
popMatrix();
hitTest();
}
void update(){
angle += radians(speed);
x=cos(angle)*(radius/2+sphereSize/2);
y=sin(angle)*(radius/2+sphereSize/2);
}
void hitTest(){
for (int i=0; i<circs.length; i++){
if (i != me){
float dist = distance(circs[i].x, circs[i].y, x, y) / (circs[i].sphereSize-2 + sphereSize-2);
if(dist < 1) {
speed*=-1;
circs[i].speed*=-1;
update();
}
}
}
}
float distance(float x1, float y1, float x2, float y2) {
return sqrt(sq(x1-x2) + sq(y1-y2));
}
}