Collision Ellipse and Rect

edited June 2015 in Questions about Code

Right guys, my problem is that, the collision on the top and bottom of the rect is not working, the way that i tried its comented

float ballX, ballY, xVel, yVel, ballDiam=30;
boolean up = true;
boolean right = true;
boolean down = true;
boolean left = true;

int rx = 50;
int ry = 50;
int rtamanho = 200;

void setup(){
  size(640,480);
  strokeWeight(2);
}

void draw(){
  background(150);
  rect(rx,ry,rtamanho,rtamanho);
  colisao();
  if (mousePressed){
    // if the mouse is held down, the ball location
    // should be the same as the mouse location.
    ballX=mouseX;
    ballY=mouseY;
  }
  else{
    // otherwise it should be traveling
    // according to its velocity.
    ballX += xVel;
    ballY += yVel;
  }
  // draw the ball:
  ellipse(ballX, ballY, ballDiam, ballDiam);
  // here is the bounce part:  if the ball gets close to
  // any edge of the window, reverse its X or Y velocity.
  // in this case, "too close" is "past the ball's radius."
  if ((ballX<ballDiam/2) || (ballX>width-ballDiam/2)) xVel = -xVel;
  if ((ballY<ballDiam/2) || (ballY>height-ballDiam/2)) yVel = -yVel;

}


  void colisao(){
  if ((ballY + ballDiam/2 > ry)&&(ballY-ballDiam/2 < ry + rtamanho)) {
    //verifica se a bolinha está na mesma região horizontal do quadrado.
    if ((ballX+ ballDiam/2 > rx) && (ballX - ballDiam/2 < rx + rtamanho)) {
      //verifica se a bolinha colidiu com o quadrado. 
      if (ballX > rx + rtamanho/2) {
        //verifica se a colisão foi pela direita ou esquerda.
        xVel = -xVel;
      } else {
        xVel = -xVel;
        //se a colisão foi feita pela esquerda, ele vai para a direita. se foi feita pela direita, vai para a esquerda.
      }
    }
  }


  /* if ((ballX + ballDiam/2 > rx) && (ballX - ballDiam/2 < rx + rtamanho)) {
    if ((ballY + ballDiam/2 > ry) && (ballY - ballDiam/2 < ry + rtamanho)) { 
      if (ballY > ry + rtamanho/2) { 
        yVel = -yVel;
      } else { 
        yVel = -yVel;
        //esse códigi apresenta a mesma lógica do código acima, a única diferença é que, agora, está levando em consideração a parte de cima e a parte de baixo
        //e não as laterais. 
      }
    }
  } */
}








void mouseReleased(){
  // when the mouse is released, "throw" the ball
  // by setting its x and y velocities.
  xVel = mouseX-pmouseX;
  yVel = mouseY-pmouseY;
}

I really need some help on making that collision, someone know how i can do that?

That's the first part of 4 thing that i need to do.

1 - Ball Moving with collision on rect 2 - Ball Moving and after hit the rect, the rect captures the ball and the ball stop moving 3 - Ball moving anf after hit the rect it keeps moving but cant leave the rect. 4 - Ball and Rect moving, and after the ball hit the rect, it stay trapped insede the rect moving and the rect keep moving too with the initial direction

Answers

  • Have you worked with objects before?

    If so you can have your ellipse and rect as objects and have them communicate with eachother. This makes it easier to control and modify each aspect of your program.

    I would suggest doing something like this:

    class Particle { Particle(){} void draw(){ ellipse(); } void move(){ location.add(velocity); } /* Other boundary function /* }

    void draw(){ if(!particle.CheckBoundary()){ /* If we are not in the rect boundary KEEP moving */ particle.move(); }else{ /* move inside the rectangle then have the ellipse collide with the rectangle boundary */ InsideRectBoundary(); } }

    here is plenty of collision detection code online that you can use. Have a look at this sketch http://www.openprocessing.org/sketch/20795 for collision detection. To trap inside a box use a formula like this.

    if(location.x < 0 || location.x > width) velocity.x *= -1; if(location.y < 0 || location.y > height) velocity.y *= -1;

    This is just so whenever the ball hits any edge it will invert the corresponding velocity. Here is a quick example, not using objects but gives a rough idea. http://www.openprocessing.org/sketch/110796

    Hope this helps

  • I never worked with object before, but it seems more easy..

  • I tried to do something like that. I got that, but whats wrong ?? T-T

    float ballX, ballY, xVel, yVel, ballDiam=30;
    boolean up = true;
    boolean right = true;
    boolean down = true;
    boolean left = true;
    
    int rx = 50;
    int ry = 50;
    int rtamanhow = 200;
    int rtamanhoh = 200;
    
    void setup(){
      size(640,480);
      strokeWeight(2);
    }
    
    void draw(){
      background(150);
      boolean collisionDetected = isCollidingCircleRectangle(ballX, ballY,
          ballDiam, rx, ry, rtamanhow, rtamanhoh);
      rect(rx,ry,rtamanhow,rtamanhoh);
      if (mousePressed){
        // if the mouse is held down, the ball location
        // should be the same as the mouse location.
        ballX=mouseX;
        ballY=mouseY;
      }
      else{
        // otherwise it should be traveling
        // according to its velocity.
        ballX += xVel;
        ballY += yVel;
      }
      // draw the ball:
      ellipse(ballX, ballY, ballDiam, ballDiam);
      // here is the bounce part:  if the ball gets close to
      // any edge of the window, reverse its X or Y velocity.
      // in this case, "too close" is "past the ball's radius."
      if ((ballX<ballDiam/2) || (ballX>width-ballDiam/2)) xVel = -xVel;
      if ((ballY<ballDiam/2) || (ballY>height-ballDiam/2)) yVel = -yVel;
    
      if (collisionDetected == true){
        yVel = -yVel;
      }
    
    
    }
    
    
    void mouseReleased(){
      // when the mouse is released, "throw" the ball
      // by setting its x and y velocities.
      xVel = mouseX-pmouseX;
      yVel = mouseY-pmouseY;
    }
    
     boolean isCollidingCircleRectangle(
              float ballX,
              float ballY,
              float ballDiam,
              float rx,
              float ry,
              float rtamanhow,
              float rtamanhoh){
    
                float circleDistanceX = abs(ballX - rx - rtamanhow/2);
        float circleDistanceY = abs(ballY - ry - rtamanhoh/2);
    
        if (ballX > (rtamanhow/2 + ballDiam)) { return false; }
        if (ballY > (rtamanhoh/2 + ballDiam)) { return false; }
    
        if (circleDistanceX <= (rtamanhow/2)) { return true; }
        if (circleDistanceY <= (rtamanhoh/2)) { return true; }
    
        float cornerDistance_sq = pow(circleDistanceX - rtamanhow/2, 2) +
                             pow(circleDistanceY - rtamanhoh/2, 2);
    
        return (cornerDistance_sq <= pow(ballDiam,2));
    }
    
Sign In or Register to comment.