Pong Collision

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Khearts
ES Beta Backer
ES Beta Backer
Posts: 50
Joined: Sun Oct 10, 2010 5:07 pm
Current Project: Super Mario Bros Clone and 3D Engine
Favorite Gaming Platforms: Dreamcast
Programming Language of Choice: C/++

Pong Collision

Post 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?
User avatar
Milch
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Sat Jul 11, 2009 5:55 am
Programming Language of Choice: C++
Location: Austria, Vienna

Re: Pong Collision

Post 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.
Follow me on twitter!
User avatar
Khearts
ES Beta Backer
ES Beta Backer
Posts: 50
Joined: Sun Oct 10, 2010 5:07 pm
Current Project: Super Mario Bros Clone and 3D Engine
Favorite Gaming Platforms: Dreamcast
Programming Language of Choice: C/++

Re: Pong Collision

Post 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.
User avatar
Milch
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Sat Jul 11, 2009 5:55 am
Programming Language of Choice: C++
Location: Austria, Vienna

Re: Pong Collision

Post 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 ;)
Follow me on twitter!
User avatar
Khearts
ES Beta Backer
ES Beta Backer
Posts: 50
Joined: Sun Oct 10, 2010 5:07 pm
Current Project: Super Mario Bros Clone and 3D Engine
Favorite Gaming Platforms: Dreamcast
Programming Language of Choice: C/++

Re: Pong Collision

Post 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. :(
User avatar
Khearts
ES Beta Backer
ES Beta Backer
Posts: 50
Joined: Sun Oct 10, 2010 5:07 pm
Current Project: Super Mario Bros Clone and 3D Engine
Favorite Gaming Platforms: Dreamcast
Programming Language of Choice: C/++

Re: Pong Collision

Post 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.
Post Reply