Page 1 of 1

Pong Collision

Posted: Sat Oct 16, 2010 1:05 am
by Khearts
Currently, I'm developing my pong clone and most of it seems to be coming along smoothly. Collision detection is really ticking me off, as of late. I have the basic collisions down such as the ball vs. the paddle with a constant velocity. I want to add the feature of increasing velocity when the ball hits the paddle. However, every time I try to implement this, the ball seems to get "stuck" within the paddle and the collision is called many times before I make it reset.

I've also tried putting in a timer. After 100 milliseconds after the ball hits the paddle, velocity increases. Yet, the same problem of the ball getting stuck continues to occur upon its next collision.

Is this an issue of framerate, maybe?

Any ideas on how to make this feature work without it bugging out?

Re: Pong Collision

Posted: Sat Oct 16, 2010 1:11 am
by Milch
I think I know what the problem is, but I'm not sure about this.
If the ball collides with the paddle, you'll need to 'reset' the balls position to the outer side of the pannel.
Why? If you just invert the XVEL/YVEL and add a a little bit to it, it might get stuck inside the pannel because its inverting the velocity every frame.
So you have to put the ball outside, so it doesnt trigger a collision in the next frame.

Post your collision-detection code for further help.

Re: Pong Collision

Posted: Sat Oct 16, 2010 1:15 am
by Khearts
Thanks, Milch, you're seriously my hero. I was thinking the same thing earlier but didn't think it would work. I'll give it some time to try it out tonight and see how it'll work. I actually don't use SDL_Rect for my "box" dimensions as I should, but here is what I have:

Code: Select all

bool Ball::checkcoll(Paddle p1)
{
if(
	((vect.getY() + getH()) > (p1.vect.getY())) &&
	((vect.getX()) < (p1.vect.getX()+p1.getW())) &&
	((vect.getY()) < (p1.vect.getY()+p1.getH())) &&
	((vect.getX()) > (p1.vect.getX())))
	{
		cout << "Collision to One Detected!" << endl;
		
		return true;
	}
	else if(
	((vect.getY() + getH()) > (p1.vect.getY())) && 
	((vect.getX() + getW()) > (p1.vect.getX())) &&
	((vect.getY()) < (p1.vect.getY()+p1.getH())) &&
	((vect.getX() + getW()) < (p1.vect.getX()+p1.getW()))
	)
	{
		
		cout << "Collision to Two detected!" << endl;
		return true;
	}
       else{return false;}
}
And if this returns true, I call:

Code: Select all

void Ball::NegXVel()
{
    vect.setXVel(-(vect.getXVel()));
    if(!SpeedUp.is_started()){SpeedUp.start();}
    if(SpeedUp.get_ticks() >= 100)
    {
        if(vect.getXVel() < 0){vect.setXVel(vect.getXVel()-2);}
        else if(vect.getXVel() > 0){vect.setXVel(vect.getXVel()+2);}
        SpeedUp.stop();
    }
    
}
Just tell me if you need more info. I'm continuously editing my code, by the way.

Re: Pong Collision

Posted: Sat Oct 16, 2010 1:29 am
by Milch
It looks like that happens what I said earlier.
Just try setting the ball outside of the pannel, so it wont collide with it next frame - then it should work ;)

Re: Pong Collision

Posted: Sat Oct 16, 2010 2:49 am
by Khearts
Milch wrote:It looks like that happens what I said earlier.
Just try setting the ball outside of the pannel, so it wont collide with it next frame - then it should work ;)

And, of course, it worked! ;)


Currently running in to more problems with the ball getting stuck in the paddle under high velocities, and it's making me a little angry. :evil: With these upcoming midterms in school, I have no choice but to continue this work on Wednesday evening. :(

Re: Pong Collision

Posted: Sat Oct 16, 2010 4:01 am
by Khearts
Just Kidding. Magically worked by changing some return statements. Will hope to finish an epic game of pong by the end of this week and hope to release it here on TCR.