• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Ping pong game collision detection error

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1gowtam  Members

Posted 21 April 2013 - 01:54 AM

Hello all, I'm a beginner, just trying to learn the elements of game development. I am creating a ping pong game clone, with allegro library. Following are the classes in my program:
3.Ball

#include "paddle.h"
#include <iostream>

{
p = new Position();
p->x = 0.0;
p->y = SCREEN_HEIGHT/2.0;

v = new Velocity();
v->x = 4.0;
v->y = 4.0;

if (!image) {
return false;
}
return true;
}

{
return image;
}

{
return al_get_bitmap_height(image);
}

{
return al_get_bitmap_width(image);
}

{
return p;
}

{
return v;
}

{
if ( al_key_down(keyState, ALLEGRO_KEY_DOWN) ) {
if(p->y+getHeight() < SCREEN_HEIGHT ) {
p->y += v->y;
}
} else if ( al_key_down(keyState, ALLEGRO_KEY_UP) ) {
if ( (p->y - v->y) >= 0 ) {
p->y -= v->y;
}
}
}

{
al_draw_bitmap(image, p->x, p->y,0);
} 

#include "ai_paddle.h"
#include <iostream>

{
p = new Position();
p->y = SCREEN_HEIGHT/2.0;

v = new Velocity();
v->x = 4.0;
v->y = 4.0;

p->x = SCREEN_WIDTH - al_get_bitmap_width(image);

if (!image) {
return false;
}
return true;
}

{
return image;
}

{
return al_get_bitmap_height(image);
}

{
return al_get_bitmap_width(image);
}

{
return p;
}

{
return v;
}

{
if ( al_key_down(keyState, ALLEGRO_KEY_DOWN) ) {
if(p->y+getHeight() < SCREEN_HEIGHT ) {
p->y += v->y;
}
} else if ( al_key_down(keyState, ALLEGRO_KEY_UP) ) {
if ( (p->y - v->y) >= 0 ) {
p->y -= v->y;
}
}
}

{
al_draw_bitmap(image, p->x, p->y,0);
}

{
if ( ball_velocity->x > 0 ) {
if ( ball_velocity->y > 0 && p->y + getHeight() < ball_position->y ) {
if(p->y+getHeight() < SCREEN_HEIGHT ) {
p->y += v->y;
}
} else {
if ( (p->y - v->y) >= 0 && p->y > ball_position->y ) {
p->y -= v->y;
}
}
}
}


3.Ball.cpp code:

#include "ball.h"

bool Ball::init()
{
p = new Position();
p->x = 0.0;
p->y = 200.0;

v = new Velocity();
v->x = 3.0;
v->y = 2.0;

if(!image) {
return false;
}
return true;
}

bool Ball::collisionWithWall()
{
bool collision_flag = false;

if ( p->y <= 0 || p->y + getHeight() >= SCREEN_HEIGHT ) {
collision_flag = true;
}

return collision_flag;
}

{
bool collision_flag = false;

if( p->y >= pd->getPosition()->y && p->y <= (pd->getPosition()->y+pd->getHeight()) ) {
if ( p->x <= (pd->getPosition()->x+pd->getWidth()) ) {
collision_flag = true;
}
} else if ( p->y >= aipd->getPosition()->y && p->y <= (aipd->getPosition()->y+aipd->getHeight()) ) {
if ( p->x+getWidth() >= (aipd->getPosition()->x) ) {
collision_flag = true;
}
}

return collision_flag;
}

{
if ( collisionWithPaddles(pd, aipd) ) {
v->x = -v->x;
} else if ( p->y <= 0 || p->y + getHeight() >= SCREEN_HEIGHT ) {
v->y = -v->y;
}
p->x += v->x;
p->y += v->y;
}

Position* Ball::getPosition()
{
return p;
}

Velocity* Ball::getVelocity()
{
return v;
}

void Ball::draw()
{
al_draw_bitmap(image, p->x, p->y, 0);
}

float Ball::getHeight()
{
return al_get_bitmap_height(image);
}

float Ball::getWidth()
{
return al_get_bitmap_width(image);
}


4. Main function:

#include <iostream>
#include <stdio.h>
#include <allegro5/allegro.h>
#include "allegro5/allegro_image.h"
#include "allegro5/allegro_native_dialog.h"
#include "ball.h"
#include <iostream>

using namespace std;

const float FPS = 60;

const int BOUNCER_SIZE = 32;
enum MYKEYS { KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT };

int main(int argc, char** argv)
{
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE* event_queue = NULL;
ALLEGRO_TIMER* timer = NULL;
bool redraw = true;
bool doexit = false;
bool key[4] = {false, false, false, false };
ALLEGRO_KEYBOARD_STATE keyState;

if(!al_init()) {
fprintf(stderr, "failed to initialize!");
return -1;
}

al_show_native_message_box(display, "Error", "Error", "Failed to initialize al_init_image_addon!",
NULL, ALLEGRO_MESSAGEBOX_ERROR);
return 0;
}

if(!al_install_keyboard()) {
fprintf(stderr, "failed to initialize keyboard!");
return -1;
}

timer = al_create_timer(1.0/FPS);
if(!timer) {
fprintf(stderr, "failed to initialize timer!");
return -1;
}

display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
if(!display) {
fprintf(stderr, "display failed to initialize!");
return -1;
}

event_queue = al_create_event_queue();
if(!event_queue) {
fprintf(stderr, "failed to create event_queue!\n");
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}

cout<<"error";
return -1;
}

Ball ball;
if ( !ball.init() ) {
return -1;
}

return -1;
}

al_register_event_source(event_queue, al_get_display_event_source(display));

al_register_event_source(event_queue, al_get_timer_event_source(timer));

al_register_event_source(event_queue, al_get_keyboard_event_source());

al_clear_to_color(al_map_rgb(0,0,0));

al_flip_display();

al_start_timer(timer);

while(!doexit) {
ALLEGRO_EVENT ev;

al_wait_for_event(event_queue, &ev);

if ( ev.type == ALLEGRO_EVENT_TIMER ) {
al_get_keyboard_state(&keyState);
redraw = true;
} else if ( ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE ) {
break;
} else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
switch(ev.keyboard.keycode) {
case ALLEGRO_KEY_ESCAPE:
doexit = true;
break;
}
}

if(redraw && al_is_event_queue_empty(event_queue)) {
redraw = false;
al_clear_to_color(al_map_rgb(0,0,0));
ball.draw();
al_flip_display();
}
}

return 0;
}


Along with these, i created a global header file, in which i put common structs:

#ifndef GLOBAL_H
#define GLOBAL_H

#include "allegro5/allegro.h"
#include "allegro5/allegro_image.h"

struct Position {
float x;
float y;
};

struct Velocity {
float x;
float y;
};

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

#endif


The problem is, sometimes the ball goes through the AI paddle, dont seem to get it why.

Other issue is, sometimes the ball collides the player paddle but it just doesn't change the direction. It would be helpful if you could point out the error or where I'm going wrong.

Thank you!

### #2Somber  Members

Posted 21 April 2013 - 09:50 AM

The reason the ball sometimes passes through the AI paddle is that you update its position after you've checked for collisions. Move the aipaddle.update() call to prior to the ball.update() call to remedy this, although it still might not register: I think if the combined speed between the ball and the paddle are greater than the paddle's bounding box width, the ball can potentially pass through without the program ever acknowledging it.

Quick notes:

- Why are you not using CollisionWithWall?

- Is there any particular reason why the collision detection and input code are in the ball and paddle classes, as opposed to a space of their own?

- You can reduce the amount of duplicate code by having a common base Paddle class both for player controlled and AI controlled paddles.

Edited by Somber, 21 April 2013 - 09:51 AM.

### #3Satharis  Members

Posted 21 April 2013 - 09:57 PM

Generally you want to move objects and then check collision due to the fact that it completely messes up collision resolution if only one object collides with another. Also what you're talking, the ball passing through the paddle is known as tunnelling. Normally that's just a thing related to objects moving faster than the update rate, but part of that I'm sure is caused by the way you're handling collision detection.

For your issue with the ball sometimes not reversing direction, taking a wild guess I'd say that happens when the ball moves into a paddle partially and then reverses its velocity and doesn't 'escape' the paddle on the next frame. Generally with collision detection you want to either move an object back to it's previous position when a collision is detected or you want to "nudge" it until it is free from the other object. Only then do you apply things like velocity changes. Unless you want colliding objects to travel through each other, collisions are basically an event that should be reversed in code before they're ever drawn.

For fixing tunneling your options are a little limited, you can slow the objects down by capping their velocity, adjust sprite size, change to a continuous form of collision detection, so on. With a game like pong unless you're letting the balls move supersonic speeds or your update rate is extremely low you shouldn't really have an issue.

### #4gowtam  Members

Posted 18 June 2013 - 06:33 AM

The reason the ball sometimes passes through the AI paddle is that you update its position after you've checked for collisions. Move the aipaddle.update() call to prior to the ball.update() call to remedy this, although it still might not register: I think if the combined speed between the ball and the paddle are greater than the paddle's bounding box width, the ball can potentially pass through without the program ever acknowledging it.

Quick notes:

- Why are you not using CollisionWithWall?

- Is there any particular reason why the collision detection and input code are in the ball and paddle classes, as opposed to a space of their own?

- You can reduce the amount of duplicate code by having a common base Paddle class both for player controlled and AI controlled paddles.

Thank you for the response . About the collision detection: I realized it as a function where the ball just checks if it collided with paddles or the wall. Am I wrong in my assumption?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.