We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › 3D Rotation and hitTest on a sphere
Page Index Toggle Pages: 1
3D Rotation and hitTest on a sphere (Read 797 times)
3D Rotation and hitTest on a sphere
Mar 2nd, 2010, 6:02am
 
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));
}
}

Re: 3D Rotation and hitTest on a sphere
Reply #1 - Mar 2nd, 2010, 6:51am
 
this has conversion from angles to xyz positions (which is basically what you're doing in 2d)
http://en.wikipedia.org/wiki/Spherical_coordinate_system

and 3d distance is just ((x1 - x2)^2 + (y1 - y2)^2 + (z1 - z2)^2)
Re: 3D Rotation and hitTest on a sphere
Reply #2 - Mar 2nd, 2010, 7:05am
 
ah, thanks.
but seemed using PVector and just normalising the vectors is the easiest way to go. will post the code once i've sorted out the hitTest.

cheers
g
Page Index Toggle Pages: 1