-4

Hello I am making a game in java that requires collision from a ball into a rectangle.

pos.x = x value of ball

pos.y = y value of ball

vel.x = x velocity of ball

vel.y = y velocity of ball

sh = height of ball

sw = width of ball

W = width of rect

H = height of rect

So far I have :

  boolean insideRect = pos.x >= X && pos.x <= X+W && pos.y >= Y && pos.y <= Y+H;
  
  if(insideRect){
  
    if(pos.y + sh/2 >= H){
       vel.y = (-1*vel.y)*gravity;
       vel.x *= .6;
       if(pos.y < Y+H/2){
         pos.y = Y;
       } else{
         pos.y = Y+H;
       }
    }
    if(pos.x + sw/2 > W){
       vel.x = -1*vel.x;
    }       
  }

However this just says if it hit the left side of the rect or right, and if it hit the top or bottom.

So an output if you printed under the if statements would be : (left, up), (right, down), (left, down), (right, up)

So the problem is that for this to work I can only have one, either left, right, up, or down.

If I have two, then it thinks that it both hit the ceiling and the right wall, and has two visual outputs do to it.

How can I work around this?

  • What is the takeAway variable? Could you please rename your sh and sw variables to look like what they represent? That would make your code a lot easier to read. How does this code lead to any print statements? – sunrise Dec 01 '20 at 23:40
  • I would suggest that you start with some elementary physics of balls moving in 2D space. – duffymo Dec 02 '20 at 01:18
  • see [Can't flip direction of ball without messing up gravity](https://stackoverflow.com/a/53637567/2521214) – Spektre Dec 02 '20 at 07:12
  • Does this answer your question? [Circle-Rectangle collision detection (intersection)](https://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection) – ChatterOne Dec 02 '20 at 07:55

1 Answers1

0
/**
     * checks to see if the obstacles and balls collide.
     * Utilizes bounding boxes for obstacles and coordinates for ball:
     * + = Obstacle
     *            ballTop
     *             : :
     *          :      :
     *ballLeft :        : ballRight
     *          :      :
     *            : :
     *         ballBottom
     *
     *          ____
     *          |+++|
     *          |+++|
     *          |+++|
     *          ----
     * @param ball
     * @param rect
     * @return boolean
     */

    public boolean intersects(Ball ball, Obstacle rect){
        double r = ball.getRadius();

        double rectTop = rect.y;
        double rectLeft = rect.x;
        double rectRight = rect.x + rect.width;
        double rectBottom = rect.y + rect.height;

        double ballTop = ball.y - r;
        double ballLeft = ball.x - r;
        double ballRight = ball.x + r;
        double ballBottom = ball.y + r;


        //NO COLLISIONS PRESENT
        if(ballRight < rectLeft){
            return false;
        }
        if(ballTop > rectBottom){
            return false;
        }
        if(ballBottom < rectTop){
            return false;
        }
        if(ballLeft > rectRight){
            return false;
        }

        //HAS TO BE A COLLISION
        return true;
    }

//In Logic File
private void collideBallObstacles(Ball ball,Obstacle rect){
        if(rect.intersects(ball, rect)){
            ball.velX *= -1;
            ball.velY *= -1;
            if (ball == player){
                flashGreen();
                obstaclesevaded--;
            }
            obstacles.remove(rect);
        }
    }

You will also have to render this, and change the variables or create ones that fit with this code.

Just curious-what would the name of this game happen to be?