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 › Quick question regarding frameRate()
Page Index Toggle Pages: 1
Quick question regarding frameRate() (Read 590 times)
Quick question regarding frameRate()
Sep 30th, 2009, 11:37pm
 
Hello guys it's me again  Cheesy

I've sorted out my code for collision detection between a travelling ball and a stationary square, and it works! What I would like to happen is for the ball to bounce off the square when it hits it.

The trouble is, at some random instances, the ball fails collision detection and goes right through the square. I find this strange because this may happen on the 4th collision with the previous 3 collisions working as planned. My friend whom I turned to for help said this could be because of the framerate. However this still happens even though framerate was increased. I reckon this could be because the ball travels too fast (i.e. coordinates change too fast) for the processor to do its checks properly.

Would any kind soul know why collision detection fails at some times, and if frameRate could be the answer to it? Thanks much  Smiley

Tim.
Re: Quick question regarding frameRate()
Reply #1 - Oct 1st, 2009, 12:37am
 
hmm i wouldnt say that framerate is the problem.
Do you have some code to show, so we can take a closer look ?
Re: Quick question regarding frameRate()
Reply #2 - Oct 1st, 2009, 1:33am
 
Hello Cedric, yup. My entire sketch is quite long so I hae just given a snippet of the code that is in question. The code will not work as it is so I'll briefly explain what the two functions move() and collisionCheck() does. Both functions are part of a ball class and they are called in the draw().

move() checks if the balls are moving horizontally or vertically, and changes bounce (a boolean variable) and subsequently it's direction of motion.

collisionCheck() checks if any collision is taking place, i.e. left,right,top, or bottom of the rectangle.

If these snippets are not making sense I'd be happy to paste the entire code.

--------
//this array stores the values of the x and y-coordinates of rectangles //that are drawn
int[] drawRect_X = new int[20];
int[] drawRect_Y = new int[20];

//these values belong to the ball class
int x_pos, y_pos, direction;
float ball_speed;
boolean bounce,hit_left,hit_right,hit_top,hit_bottom

void draw()
{
   for(i=0;i<ball.length;i++){
   ball[i].move();
   ball[i].collisionCheck();
   {
};

//the bottom two functions belong to a ball class

void move(){
   
  ball_speed = random(0.1,0.9);
   
  // getting the ball to move horizontally
  if (direction == 1){
     
    if (bounce == false)  
     x_pos += ball_speed;
     else
     x_pos -= ball_speed;

    if (x_pos +5 > width || (bounce==false && hit_left == true)){
     bounce = true;
     hit_left=false;
    };
   
    if (x_pos -5 < 0 || (bounce==true && hit_right==true)){
       bounce = false;
       hit_right = false;
    };  
 };

// getting the ball to move vertically
   if (direction == 2){
     
     if (bounce == false)  
       y_pos += ball_speed;
     else
       y_pos -= ball_speed;

     if (y_pos +5 > height || (bounce == false && hit_top == true))
     {
       hit_top = false;
       bounce = true;
     }
     if (y_pos -5 < 0  || (bounce == true && hit_bottom==true)){
       bounce = false;
       hit_bottom = false;
     };
    };
 };  
 


 void collisionCheck(){

 for (int l=0; l<drawRect_X.length; l++)
 {  
    //collision checking for bottom of square
    if (int(y_pos) == (drawRect_Y[l]+25)
    && x_pos-5 > (drawRect_X[l]-25)
    && x_pos+5 < (drawRect_X[l]+25)){
       hit_bottom = true;
      };
     
    //collision checking for top of square
    if (int(y_pos) == (drawRect_Y[l]-25)
        && x_pos-5 > (drawRect_X[l]-25)
        && x_pos+5 < (drawRect_X[l]+25)){
      hit_top = true;
    };
   
    //collision checking for left of square
    if (int(x_pos) == (drawRect_X[l]-25)
        && y_pos-5 > (drawRect_Y[l]-25)
        && y_pos+5 < (drawRect_Y[l]+25)){
      hit_left = true;
    };
   
     //collision checking for right of square
     if (int(x_pos) == (drawRect_X[l]+25)
        && y_pos-5 > (drawRect_Y[l]-25)
        && y_pos+5 < (drawRect_Y[l]+25)){
      hit_right = true;
    };
};
Re: Quick question regarding frameRate()
Reply #3 - Oct 1st, 2009, 1:46am
 
From a quick glance I'd say the problem lies in your collision detection, specifically where you test for equality:

if(int(y_pos) == (drawRext_Y[l]+25)...

If the vertical or horizontal speed of the ball is greater than one then it's perfectly reasonable that this test will fail to return true.  e.g. if drawRext_Y[l]+25 = 200 and y_pos = 199 and the vertical speed = 2, y_pos will go from 199 straight to 201, will not be equal to 200 so the collision won't occur...  The solution is to do what you do in the rest of the condition and test for greater/less than; or alternatively look into other methods of collision detection.

Page Index Toggle Pages: 1