Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Ok so this is for a small uni project. My lecturer provided me with a framework for a simple brickbreaker game. I am currently trying to overcome to problem of detecting a collision between the two game objects. One object is always the ball and the other objects can either be the bricks or the bat.

public Collision hitBy( GameObject obj )
{
    //obj is the bat or the bricks
    //the current object is the ball

    // if ball hits top of object
    if(topX + width >= obj.topX && topX <= obj.topX + obj.width &&
       topY + height >= obj.topY - 2 && topY + height <= obj.topY){
        return Collision.HITY;
    }

    //if ball hits left hand side
    else if(topY + height >= obj.topY && topY <= obj.topY + obj.height &&
            topX + width >= obj.topX -2 && topX + width <= obj.topX){
        return Collision.HITX;
    }

    else return Collision.NO_HIT;
}

So far I have a method that is used to detect this collision. The the current obj is a ball and the obj passed into the method is the the bricks. At the moment I have only added statement to check for left and top collisions but do not want to continue as I have a few problems.

The ball reacts perfectly if it hits the top of the bricks or bat but when it hits the ball often does not change directing. It seems that it is happening toward the top of the left hand edge but I cannot figure out why.

I would like to know if there is another way of approaching this or if people know where I'm going wrong.

Lastly the collision.HITX calls another method later on the changes the x direction likewise with y.

share|improve this question
1  
Check out this post it may give you a little insight on this matter stackoverflow.com/questions/401847/… – Savlon Mar 18 at 2:29
What game engine (framework) do you use ? Libgdx has a very comfortable tool, ContactListener, that elaborates the collisionvery well. – Ion Farima Mar 18 at 9:04

2 Answers

It doesn't change the direction because you didn't handle it.. you want something like ball bouncing

take a look at this example :

NOTE: I know you asked it for android/java but I did it in c# it was an assignement in WinForms :) but just look maybe it'll give some idea

 public void moveBall(Rectangle r)
 {
       x = ballLocation.X + vX;   // x and y are the new coordinate of the ball
       y = ballLocation.Y + vY;

       if (x + diameter > r.Width) // when the ball moves right and hits the right              
                                   // border of the form
       {
           // vX is the ball velocity on X axis
           // set new velocity on x axis but convert it to negative
           // so the ball will move left
           vX = new Random().Next(minVelocity, maxVelocity);
           vX = -vX;
       }
       else if (x < 0) // when the ball move left and hits the left border
       {
           // set new velocity (Note that I didn't convert it to negative 
           // because i'm getting a positive value)
           vX = new Random().Next(minVelocity, maxVelocity);
       }

       if (y + diameter > r.Height) // when the ball hits the bottom border of the  
                                    //form
       {
           // vY is the ball velocity on Y axis
           // set new velocity and convert it to negative 
           // so the ball will move up
           vY = new Random().Next(minVelocity, maxVelocity);
           vY = -vY;
       }
       else if (y < 0) // when the ball hits the upper border of the form
       {
           // set new velocity on Y axis
           // so the ball will move down
           vY = new Random().Next(minVelocity, maxVelocity);
       }
        // set the ball location to the new values
        ballLocation = new Rectangle(x, y, diameter, diameter);
    }   

Note : in all screens no matter if it's phone or pc or tv screen, the upper left corner is (0,0,RGB) means -> pixel which construct from point and one of the colors :RED ,GREEN,BLUE. So if you want to move object to :

Right:

add value to x axis

Left :

subtract value to x axis

Down:

add value to y axis

Up:

subtract value to y axis

share|improve this answer

So from what I understand your coordinate system's origin is at the top left of the screen.

I think you need to get rid of your magic number in your IF statements first (I also split it up into two IF statements, easier to read):

// if ball hits top of object
if(topX + width >= obj.topX && topX <= obj.topX + obj.width) {
    if(topY + height >= obj.topY && topY + height <= obj.topY + obj.height) {
        return Collision.HITY;
    }
}

Now that might have fixed it partially, the next thing I would do is reset the position of the ball when it collides (if you're not doing so already somewhere else):

// if ball hits top of object
if(topX + width >= obj.topX && topX <= obj.topX + obj.width) {
    if(topY + height >= obj.topY && topY + height <= obj.topY + obj.height) {
        setY(obj.topY - height);
        return Collision.HITY;
    }
}

This way during the next frame update it won't think that ball and obj are colliding again. It may be executing this code multiple times before you can even see it on the screen. This is the same way I would do it for the left, right, and bottom collision.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.