Detect collision between corners of objects

13

I'm writing a game based on the breakout , but I can not think of a way to detect the collision between the corner of the ball area and the paddle, to be able to reverse the horizontal direction of the ball.

In my class Ball , I have a method that takes care of the movement of the ball, and when it detects a collision, it only reverses the vertical direction (y-axis).

import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Ball {

    private int x = 0;
    private int y = 15;
    private final int DIAMETER = 30;
    private int xSpeed = 1;
    private int ySpeed = 1;

    private Board board;

    public Ball(Board board) {
        this.board = board;
        y = board.paddle.getTopY() - DIAMETER;
        x = board.getPreferredSize().width / 2 - DIAMETER / 2;
    }

    public void move() {

        if (x > board.getWidth() - DIAMETER || x < 0) {
            xSpeed = -xSpeed;
        }

        if (y < 15) {
            ySpeed = -ySpeed;
        }

        if (y > board.getHeight() - DIAMETER) {
            board.gameOver();
        }

        //entre nesta if quando uma colisão é detectada
        if (collision()) {
            y = board.paddle.getTopY() - DIAMETER;
        }

        x += xSpeed;
        y += ySpeed;
    }

    public void setSpeed(int speed) {
        this.xSpeed = speed;
        this.ySpeed = speed;
    }

    public void paint(Graphics2D g2) {
        g2.fillOval(x, y, DIAMETER, DIAMETER);
    }

    public boolean collision() {
        //detecta colisão entre a area da bola e o paddle
        return board.paddle.getBounds().intersects(this.getBounds());
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, DIAMETER, DIAMETER);
    }
}

The result so far:

Onlywhentheballareacollideswiththepaddle'scorner,itcontinuesthetrajectoryofthex-axis,andtosimulatemorerealphysics,IwouldliketobeabletodetectwhenthecollisionoccursattheendsoftheobjectsandinvertthetrajectoryoftheXaxisoftheball.HowdoIdetectthis?

Sincethereare4differentbigclasses,soasnottomessupthequestion,Iputafullyexecutableandcompleteexampleofthecodeinthe gist .

    
asked by anonymous 09.05.2017 / 01:29

1 answer

12

In the case of this game, what you want to do is very simple.

When your collision happens, the ball is necessarily touching the top of the stick ( paddle ). Therefore, the value of the y axis does not matter. You need to compare the value of the axis x relatively .

By relative, I mean that you need to check how much the value in% of the ball is displaced from the value in% with% of the ball. Do this (pseudo-code):

centro_bastao = bastao.x + (bastao.width / 2);
posRelativa = Math.abs(bola.x - centro_bastao) / (bastao.width / 2);

The value of x will be between x and posRelativa so that 0 means that the ball is exactly in the center and 1 the ball is exactly at one end. If you want to know which tip (ie, the side), take the 0 of the code above and check the signal (negative will be on the left and positive on the right). Then make the decision according to the value of this variable.

  

Q: Ideally you should move the ball using a vector   velocity (ie, a vector with the direction and magnitude of the   movement speed). Thus, it would be easier to "reflect" the vector   simply by rotating it to the value of 1 and then   inverting it (0 does not rotate, and the inverse is exactly the force to be   applied back up). Fail from the scope of this site to give a class   I would suggest reading on the Internet about it. It is important to   meet to develop games. This my other answer has   something in that sense and can be useful as well.

    
09.05.2017 / 02:12