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 & HelpSyntax Questions › Bouncing off Objects (Tangent)
Page Index Toggle Pages: 1
Bouncing off Objects (Tangent) (Read 149 times)
Bouncing off Objects (Tangent)
Feb 28th, 2009, 2:33am
 
I've been trying to get this floating ball to bounce off of colored ellipse objects (or Bumpers) tangent to where it hits the Bumper. Right now I use a range of x and y values to determine where each Bumper is -kinda- and the ball bounces backward where it came from.

The Bumpers are static objects stored in a class.

Here's some code...



class Ball {
 float xpos;        
 float ypos;        
 float xspeed;      
 float yspeed;    
 int size = 30;
 
 Ball() {
   xpos = 480;
   ypos = 0;
   xspeed = 2;
   yspeed = 4;
 }

 void display () {
 stroke(0);
 fill(255,228,10);
 ellipse(xpos+size/2, ypos+size/2, size, size);
 }
 
 void move() {
 float xdirection = 1;
 float ydirection = 1;

 xpos = xpos + ( xspeed * xdirection );
 ypos = ypos + ( yspeed * ydirection );
 

 if (xpos > 850-size || xpos < 100) {       //walls
   xspeed *= -1;
 }
 if (ypos > 800-size || ypos < 0) {
   yspeed *= -1;
 }

 
 //Bumper Collision
 //Luigi
   if(xpos > 210 && xpos < 260 && ypos > 345 && ypos < 415) {
     yspeed *= -1;
     xspeed *= -1;
     score =  score + 100;
   }
   
   //Mario
   if(xpos > 422 && xpos < 472 && ypos > 190 && ypos < 250) {
     xspeed *= -1;
     yspeed *= -1;
     score = score + 225;
   }
   
   //Toad
   if(xpos > 650 && xpos < 725 && ypos > 500 && ypos < 570) {
     xspeed *= -1;
     yspeed *= -1;
     score = score + 75;
   }
   
   
   //Peach
   if(xpos > 210 && xpos < 280 && ypos > 500 && ypos < 570) {
     xspeed *= -1;
     yspeed *= -1;
     score = score + 75;
   }
   
   //Yoshi
   if(xpos > 510 && xpos < 580 && ypos > 500 && ypos < 570) {
     xspeed *= -1;
     yspeed *= -1;
     score = score + 100;
   }
 
 }
 }



Page Index Toggle Pages: 1