if (MyFloatRect.Intersects(MyOtherFloatRect))
{
// Collision detection
}
won't work? How should I handle collision? Thank you for reading my post, I'm sure there's a glaring solution I can't figure out
Posted 07 October 2012 - 03:30 PM
Posted 07 October 2012 - 10:03 PM
Edited by Trienco, 07 October 2012 - 10:04 PM.
Posted 07 October 2012 - 11:07 PM
Edited by Black-Rook, 07 October 2012 - 11:09 PM.
Posted 08 October 2012 - 01:57 AM
There are a few ways to do collision detection.
Grids, bounding box, pixel perfect, ect...
For something like this, I would learn bounding box collision if you're new to how collision works.
Your main objective here is to get the x and y values for each object, and their current height and width to create a top, bottom, left, and right. Now, you will be checking if one box (or several) collide with another box, which means collision has been detected. I will show you a basic example.
Paddle:
Paddle_BB_Top = Paddle_Y
Paddle_BB_Left = Paddle_X
Paddle_BB_Right = Paddle_Width + Paddle_BB_Left
Paddle_BB_Bottom = Paddle_Height + Paddle_BB_Top
Ball:
Ball_BB_Top = Ball_Y
Ball_BB_Left = Ball_X
Ball_BB_Right = Ball_Width + Ball_BB_Left
Ball_BB_Bottom = Ball_Height + Ball_BB_Top
Now to detect if collision is true, you would simply have basic check seeing if anything collides.
if (Paddle_BB_Bottom > Ball_BB_Top &&
Paddle_BB_Top < Ball_BB_Bottom &&
Paddle_BB_Right > Ball_BB_Left &&
Paddle_BB_Left < Ball_BB_Right)
{
Collision = true;
}
I hope this helps, feel free to ask any additional questions.
Edit: The most important thing to never forget!!! You must update these values for each frame, or your bounding boxes will have incorrect values. So make a function to update the values before checking for collision.
Posted 08 October 2012 - 03:02 AM
Edited by Black-Rook, 08 October 2012 - 03:14 AM.
Posted 08 October 2012 - 03:16 AM
Maybe I'm reading the question wrong... But I would just create four collision lines, one for top, bottom, left, and right for each block. Then if the ball hits a block's left collision line, it will act in the way you want it to, the same for the block's right collision line..
Posted 08 October 2012 - 03:24 AM
Posted 08 October 2012 - 05:14 AM
He should be refreshing the balls position each frame which makes it impossible to miss a collision check.
"Edit: Just to explain how you would do this encase you ask. You would make four "collision boxes", one for left, right, top, bottom in your block class. This is very easy to setup based on your blocks X and Y values. Then you would just make a line pretty much that outlines the block for collision checking. You can even have the ball hit the corner of the block - meaning it hit the left and bottom lines then move the in proper direction."
Posted 08 October 2012 - 06:17 AM
Posted 08 October 2012 - 08:10 PM
Posted 08 October 2012 - 10:32 PM
Posted 08 October 2012 - 10:45 PM
[]
[][][][]
[]
[]
Edited by Trienco, 08 October 2012 - 10:57 PM.
Posted 09 October 2012 - 01:35 AM
Edited by Black-Rook, 09 October 2012 - 01:47 AM.
Posted 09 October 2012 - 01:50 AM
The only thing I could think of doing is setting up four boxes, one for every side (I made pong, I know what bounding box collision is).
Also, from what I understand, all you have to do to make sure your ball doesn't go past your paddle is use a few variables and run a check every game loop, if I'm correct?
Posted 09 October 2012 - 04:48 PM

Edited by PyroDragn, 09 October 2012 - 04:55 PM.
Posted 09 October 2012 - 05:01 PM
Posted 09 October 2012 - 05:04 PM
Posted 09 October 2012 - 05:23 PM
Sorry, ^ That's kind of what you said, just simpler. Essentially I have 4 collision variables(ballx/bally at the beginning of the game loop, ballx/bally at the end) I update before and after I move the ball and check to see if collision was skipped every game loop. If collision was skipped, I move the ball back to where it should be using some basic linear algebra.
Posted 09 October 2012 - 05:54 PM