0

This is collision detection with paddle (bottom right and left ) But the ball passes through the paddle and it is velocity does not change.


my questions are

  1. Is this the right implementation of collision detection with paddle ?

  2. Where should i call collision detection function ?


 GObject detectCollision(GWindow window, GOval ball)
 {
// ball's location
double x = getX(ball);
double y = getY(ball);
// initial velocity
double x_velocity = 1;
double y_velocity = 1.5 ;

// for checking for collisions
GObject object;
// check for collision at ball's bottom-left corner
object = getGObjectAt(window, x, y + 2 * RADIUS);
if (object != NULL)
{   
    if (strcmp(getType(object), "GRect") == 0)
    {
        y_velocity = -y_velocity ;
    }
    return object;
}

// check for collision at ball's bottom-right corner
object = getGObjectAt(window, x + 2 * RADIUS, y + 2 * RADIUS);
if (object != NULL)
{
    if (strcmp(getType(object), "GRect") == 0)
    {
        y_velocity = -y_velocity ;
    }
    return object;
}
// no collision
return NULL;

}

2
  • 1
    In your question, you make several fragmented statements, but you don't actually ask a question. Can you please edit your question and clarify what you are asking, what you need help with, and what you really mean? – Cliff B Jul 27 '15 at 19:09
  • @CliffB post edited.thanks fot – Elhaw Jul 27 '15 at 22:56
1

The function detectCollision(...) has a specific purpose - to detect whether the ball has collided with another object, and, if it has, to return the identifier of that object. Since this function is used to detect collisions with multiple objects, you should expand it to detect collisions on all 4 corners.

You are attempting to change the velocity of the ball within the detectCollision() function. While you could change the velocity within the function, it's a bad idea because it is altering and complicating the purpose of the function, which will be used to detect collisions with other objects besides the paddle that might require a different reaction. This is one of the lessons of writing functions - a function should have a well defined purpose and scope limited to that purpose. Trying to do too much is an indicator that you should probably be creating additional functions, or moving some of the operations to the calling routine. KISS - Keep It Simple...!

Even if you wanted to change the velocity of the ball, this code doesn't do it. You've defined two vars for x and y velocity, but they are not associated with the ball, or with any object. In order to change the velocity, you would have to change the velocity of the ball using the move() function. You can't just change a variable to affect the actual movement of the ball.

The detectCollision() function needs to be called from main(), in the while loop that does the work of moving the ball. It will depend on your implementation on exactly where to use it. You should be thinking in these terms:
1. call detectCollision(). 2. If(collision detected) then change direction/velocity.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

You must log in to answer this question.

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