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 & HelpOpenGL and 3D Libraries › Pong-3D - how determine collision
Page Index Toggle Pages: 1
Pong-3D - how determine collision (Read 1157 times)
Pong-3D - how determine collision
Jan 23rd, 2010, 3:15pm
 


Hello,

I am interested in a sphere flying through a 3D-Space hitting a wall.

Think of the sport squash.

When the sphere is inside a box, build by vertices, you can create a 3D-pong-game.

But how is the formula sphere hits vertices/3D-Wall

Thanks!

Chrisir




import processing.opengl.*;

// fonts =====================================================

PFont font;

// vars ====================================================

int Speed = 3;

int Line_X = 100;
int Line_Y = 80;
int Line_Z = -20;

// Creating an array of objects.
Mover[] movers = new Mover[1];


// main functions ========================================

void setup() {
 // automatic init
 size(screen.width, screen.height, P3D );
 fill(204);
 noSmooth();
 // The font must be located in the sketch's
 // "data" directory to load successfully
 font = loadFont("Eureka-90.vlw");
 // font
 textFont(font);
 movers[0] = new Mover();

} // function

void draw() {
 // automatically loops
 background ( 0,0,54 );
 lights();

 // camera
 camera( 200, 200, 330, // eyeX, eyeY, eyeZ      
 100, 100,-100,   // centerX, centerY, centerZ        
 0.0, 1.0, 0.0 );              // upX, upY, upZ  

 stroke(80);

 // Big Game Box
 BigGameBox ();
 //  BoxParameters ( 0, 0, -1440,
 //  width, height, 45,
 //  0, 0, 0 );
 //  fill(99);
 // SphereParameters(100,100,-100,33);

 int i = 0;
 movers[i].update();
 movers[i].checkEdges();
 movers[i].display();


} // function

// minor functions ========================================
// CheckeredFloor and ShowCoordinates

void CheckeredFloor() {

 noStroke();

 for (int i = 0; i < 40; i = i+1) {
   for (int j = 0; j < 40; j = j+1) {

     // % is modulo, meaning rest of division
     if (i%2 == 0) {
       if (j%2 == 0) {
         fill (255,0,0);
       }
       else
       {
         fill ( 103 );
       }
     }  
     else {
       if (j%2 == 0) {
         fill ( 103 );
       }
       else
       {
         fill (255,0,0);
       }
     } // if

     pushMatrix();

     translate ( 40*i-500,360,40*j-640 );
     box (40,7,40);

     popMatrix();

   } // for
 } // for

} // function

void ShowCoordinates () {
 // Show Coordinates x, y and z

 // X
 stroke (255,0,0);
 line (0,0,0, 100,0,0 ) ;
 fill(255,0,0);
 SphereParameters (100,0,0,13);
 text ("X",110,60,0);

 // Y
 stroke    (0,255,0);
 line (0,0,0, 0,100,0 ) ;    
 fill(0,255,0);
 SphereParameters (0,100,0,13);    
 text ("Y",0,160,0);

 // Z
 stroke (0,0,255);
 line (0,0,0, 0,0,-300 ) ;  
 fill(0,0,255);
 SphereParameters (0,0,-300,33);    
 text ("-Z",30,50,-300);

} // function

// ===================================================
// functions for 3D Shapes

void SphereParameters ( float x, float y, float z,
float w ) {
 // Position and size of a sphere
 noStroke();
 pushMatrix();
 translate ( x,y,z );
 sphere ( w );
 popMatrix();

} // function

void BoxParameters ( float x,float y,float z,
float w, float h, float d,  
float RotateX, float RotateY, float RotateZ ) {
 // Position and size and rotation of a box
 noStroke();
 fill (254,254,1);
 pushMatrix();
 translate ( x,y,z );
 rotateX ( radians(RotateX) );
 rotateY ( radians(RotateY) );
 rotateZ ( radians(RotateZ) );  
 box ( w,h,d );
 popMatrix();

} // function

void drawLine ( float x1, float y1, float z1,
float x2, float y2, float z2, float weight, color strokeColour)
// drawLine programmed by James Carruthers
// see http://processing.org/discourse/yabb2/num_1262458611.html#4
// It is a 3D-replacement for the Line from x1,y1,z1 to xy,y2,z2 with
// weight and strokeColour.
{
 PVector p1 = new PVector(x1, y1, z1);
 PVector p2 = new PVector(x2, y2, z2);
 PVector v1 = new PVector(x2-x1, y2-y1, z2-z1);
 float rho = sqrt(pow(v1.x,2)+pow(v1.y,2)+pow(v1.z,2));
 float phi = acos(v1.z/rho);
 float the = atan2(v1.y,v1.x);
 v1.mult(0.5);

 pushMatrix();
 translate(x1,y1,z1);
 translate(v1.x, v1.y, v1.z);
 rotateZ(the);
 rotateY(phi);
 noStroke();
 fill(strokeColour);
 box(weight,weight,p1.dist(p2)*1.2);
 popMatrix();

} // function

// ------------------------------------------------------------

void BigGameBox () {

 fill(122);
 // left wall  
 beginShape ();
 vertex (0,0,0);
 vertex (0,0,-300);  
 vertex (0,300,-300);    
 vertex (0,300,0 );    
 endShape(CLOSE);

 fill(22);
 // Wall in the back
 beginShape ();
 vertex (0,0,-300);  
 vertex (0,300,-300);    
 vertex (300,300,-300 );    
 vertex (300,0,-300);    
 endShape(CLOSE);

 fill(222);
 // Right wall
 beginShape ();
 vertex (300,300,-300 );    
 vertex (300,0,-300);    
 vertex (300,0,0);      
 vertex (300,300,0);        
 endShape(CLOSE);

 fill(254);
 // floor
 beginShape ();
 vertex (0,300,0 );    
 vertex (300,300,0);    
 vertex (300,300,-300);      
 vertex (0,300,-300);        
 endShape(CLOSE);

 fill(2);
 // ceiling
 beginShape ();
 vertex (0,0,0 );    
 vertex (300,0,0);    
 vertex (300,0,-300);      
 vertex (0,0,-300);        
 endShape(CLOSE);

}

// ------------------------------------------------------------

class Mover {

 PVector location;
 PVector velocity;
 PVector acceleration;
 float topspeed;

 Mover() {
   location = new PVector (100,100,-200); //  (random(width),random(height));
   velocity = new PVector(33,33,-10);
   topspeed = 4;
 }

 void update() {

   // Our algorithm for calculating acceleration:

etc. max characters 6000 reached....  Cry

Re: Pong-3D - how determine collision
Reply #1 - Jan 23rd, 2010, 10:29pm
 
Hi,
lots of articles out there on this subject -- try terms like "sphere box collision," "sphere plane collision," or "plane-sphere intersection."
Good luck!
Re: Pong-3D - how determine collision
Reply #2 - Jan 24th, 2010, 11:00am
 


Thank you!

Page Index Toggle Pages: 1