Pong Hit Detection

Started by
12 comments, last by JDCAce 11 years, 6 months ago
Thank you very much, EngineProgrammer! I think this will still be tough, but you've given me a great head start!

UPDATE: It works! Using some math I found elsewhere, I got my program to run flawlessly! Now for my next project: Randomly-dropped power-ups!
Advertisement

[quote name='Chad Smith' timestamp='1348963079' post='4985195']
[quote name='EngineProgrammer' timestamp='1348926458' post='4985043']
Also. Try to avoid allocating and deallocating objects at run-time. Create the paddles and their hit-regions at compile-time. smile.png
By the way are you having memory leaks? You are allocating Rectangles but aren't deallocating them in that function..... That function is called in a while-loop right? If so, you need to delete the Rectangles at the end of the function.


~EngineProgrammer


I think you may be confused on what language he is using. He is using C#.and not C++. That stuff is left up to the garbage collector.
[/quote]

Yes - deallocation of memory is left to the garbage collector. Still yet - the programmer is creating a seven new rectangles during each update cycle. I believe EngineProgrammer was only trying to say that the rectangles should be created only once. From that point forward, the only values that should change are the X & Y values. The paddle (in the original pong) doesn't change sizes so the width and height values will remain constant.

Is it completely necessary - probably not. Pong isn't so memory intensive that the creating the 7 rectangles 60 times a second will be noticed. But isn't it better to break bad programming habits earlier on? It'd probably clean up the code a bit too...

-Shadow
[/quote]

Yes that may be true but I was talking more towards when he said it looked like he had a memory leak because he was not deleting what he would new. That is why I just wanted to bring up that he was using C# and not C++. Sorry for the misunderstanding.

Yes that may be true but I was talking more towards when he said it looked like he had a memory leak because he was not deleting what he would new. That is why I just wanted to bring up that he was using C# and not C++. Sorry for the misunderstanding.


Oh - my bad. :D

BTW - I wasted a little time yesterday and on my machine, I didn't really see any performance hits until well above the 1000 rectangles mark. I had a < .00005 loss when recreating the 1000 rectangles. At 10'000 rectangles, adjusting the X & Y values of the rectangle ran around .0005 seconds (.5 ms) per frame while creating new ones each cycle dropped me to .0009 seconds (.9ms) per frame. Inversely - that's a drop from ~2000FPS to ~1100FPS.

Again - not really a necessity for something so simple as rectangles. But an interesting experiment and good learning experience none-the-less!
I agree, ShadowValence. I worked on my coding and I think it's a lot cleaner and a bit more efficient now. Like I said, I found the math elsewhere, so I won't claim that as mine, but after a lot of working, I think I understand it. Also, as EngineProgrammer suggested, I made only one Rectangle per paddle (and one per ball), and saved the values in the respective class (ie, leftPaddle.rectangle). I added an Update() method to the Paddle class in which the values of the rectangle are updated, using rectangle.X and rectangle.Y, instead of creating a whole new rectangle each Update().

This project really has turned out to be a valuable learning experience, and I can't thank all of you enough!

[rollup='Code']
private void checkHit()
{
float intersectY = ball.location.Y;
float relativeIntersectY;
float normalizedRelativeIntersectY;
double bounceAngle;
if(leftPaddle.rectangle.Intersects(ball.rectangle))
{
relativeIntersectY = (leftPaddle.location.Y + (leftPaddle.Height / 2)) - intersectY;
normalizedRelativeIntersectY = (relativeIntersectY / (leftPaddle.Height / 2));
bounceAngle = (normalizedRelativeIntersectY * (Math.PI / 4));
ball.velocity.X = (float)(10 * Math.Cos(bounceAngle));
ball.velocity.Y = (float)(5 * -Math.Sin(bounceAngle));
}
else if(rightPaddle.rectangle.Intersects(ball.rectangle))
{
relativeIntersectY = (rightPaddle.location.Y + (rightPaddle.Height / 2)) - intersectY;
normalizedRelativeIntersectY = (relativeIntersectY / (rightPaddle.Height / 2));
bounceAngle = (normalizedRelativeIntersectY * (Math.PI / 4));
ball.velocity.X = (float)(10 * Math.Cos(bounceAngle));
ball.velocity.Y = (float)(5 * -Math.Sin(bounceAngle));
ball.velocity.X = -ball.velocity.X;
}
}

[/rollup]

This topic is closed to new replies.

Advertisement