2

I don't understand what I actually have to do for the collision of ball with other objects. I am at the state where ball and paddle are just moving but collision is not yet defined. What should I do if the ball collides with paddle?

I have already tried to change the velocity of ball when it collides with the paddle (initially talking about paddle only) but there is no effect of that.

8

The Idea Behind Collision:

  • There's a function namely detectCollision() that's already written for you and ready to use. This function, when it's called, checks whether any of the sides of the ball collides with any GObject (e.g., GRect). If so, it returns that GObject immediately.
  • The simple idea behind collision is that whether any of the points on the ball has the same x and y coordinates as any of the points on the GObject that the ball collides with.
  • This GObject, returned by detectCollision(), carries a lot of information within it (e.g., the x and y coordinates for the top-left point of that GObject, the width, the height and more). These information mean a lot to us.
  • If, rather, the ball doesn't collide with any GObjects at the momentdetectCollision() is called, detectCollision() simply returns NULL. So, you've to keep in mind that you should check for that not to get a segmentation fault trying to deal with NULL wrongly!
  • The idea of the ball bouncing off an object depends on which side of the ball collides with which side of the GObject. This is more of a simple Physics subject. Since the ball has four sides (top, right, bottom and left), one of them collides at a time, it has four collision possibilities:

    1. The top side of the ball collides with a bottom side of a GObject. In this case, the ball bounces off to the bottom.
    2. The right side of the ball collides with a left side of a GObject. In this case, the ball bounces off to the left.
    3. The left side of the ball collides with a right side of a GObject. In this case, the ball bounces off to the right.
    4. The bottom side of the ball collides with a top side of a GObject. In this case, the ball bounces off to the top.

The Idea Behind the Movements of the Ball:

  1. Moving down: the velocity of the ball on the y-axis changes with a positive value (e.g., 1).

  2. Moving up: the velocity of the ball on the y-axis changes with a negative value (e.g., -1).

  3. Moving to the right: the velocity of the ball on the x-axis changes with a positive value.

  4. Moving to the left: the velocity of the ball on the x-axis changes with a negative value.

You may take a look at the following image for visual representation:

GUI coordinates visual representation

Now, using all of these information, it won't be difficult to us to understand the logic of bouncing the ball of. Yes it's just a matter of inverting a value.

I wrote some pseudocode for you for this collision part:

create a GObject variabel named obj
obj = detectCollision()
if obj is not NULL // collision
{
    if obj is the paddle
    {
        if the ball is moving down
        {    
            make the ball move up
        }
    else if obj is a GRect // a brick
    {
        remove this GRect
        decrease the number of bricks
        increase the score
        update the scoreboard
        check which side of the ball collides with which side of the GRect
        bounce the ball off accordingly
    }
}

Good luck!

1
  • Thanks.. I have better idea of how to start.. – julio Aug 30 '14 at 2:00
3

Here is a bit of psuedocode to help you define detectCollision:

  • If object is not equal to NULL then check if:
  • object == paddle, if it is, flip the y-velocity accordingly
  • else if, object == GRect (which in this case is a brick) then:
  • flip the y-velocity, remove the object (removeGWindow), update the points and update the scoreboard (UpdateScoreboard)
2

On the main routine, you have a call to a function that detect collisions.

The ball is the only moving object.

What you need to do is checking if the ball "touches" an object or the walls.

Detecting wall hitting is easy: you only need to compare the position of the ball with the border of the window, and invert the xvelocity in case the position is on the wall.

Detecting paddle collision is not that hard: you have an object (a GRect) called "paddle", but wait: you have also bricks that are GRect objects! In both cases, you need to invert the yvelocity of the ball when a collision happen, but if you hit a brick, you need to also delete the brick (a GRect). That hints to an if() statement to detect what object you hit.

So, the final code would be:

  1. Move the ball
  2. check for object detection
  3. if the ball hit the wall, invert xvelocity
  4. if the ball hit an object, define the type of the object
  5. if the object is not a paddle, delete the object
  6. invert yvelocity

That's all, folks :-)

Luigi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .