I want to start of by saying; sorry for making another collision thread. This is not only about collision though.
I've read through the forum and checked alot of other resources I just can't figure out how to do this and I need some help with my specific code. I'm new to Java and I still have some problems adapting others code to my own.
Anyways. I'm doing a modified bricks game, a little more advanced and a little diffrent. What i have is a ball (Ellipse2D.Double) and a unspecified level of blocks(Rectangle2D.Double) (diffrent kinds and diffrent amounts for each level).
(this part is not done yet, it only exists in my head
What I thought of doing is a createLevel class that reads a specific levels layout from a csv or xml file, don't really know how to do that yet. The game panel should be tiled in a grid with tiles the same size as a block (all blocks have the same size). The level file would include a list of each tile and what kind of block (if any) should be present at that specific tile. The createLevel class then renders a level using the level file. I don't really know how to handle empty tiles. Should I place a special kind of invisible block with no collision or none at all?
First off, does that sound like a good idea for handeling diffrent levels?
createLevel would also store all blocks in an array (top left to bottom right tiles I guess). This would be for the collision since I need to check what kind of block the ball hits and in what tile it is.
Then, every time the ball moves I need to check if it hits something. I already made it collide with boundries of the game panel.
My "main question" is: how to do this?
I've tried 3 diffrent approaches. All with little or no luck.
1. First I tried converting the ball and blocks to Areas and to a simple isEmpty() check on the ball to see if it was overlapping any block. This did how ever not even compile. I must've messed something up. Tried doing it exactly as I found it in another thread here on the forums. No success.
2. Then I tried getting the x, y and dimensions for each block and checking if they overlapped by using Ellipse2D.intersects. This kinda worked. I got it to respond to colliding with the block (I only draw one just to test it). However the ball did get stuck on the block.. Couldn't get it to bounce of. This was my most successful try. I would think this is the most correct and exact way to do it. Am I right?
3. Then I tried using the balls x and y coordinates to do a number of checks: first was the ball under the block (ball x > block x and ball x < block x + block width) and then check if the ball was aligned with any of the blocks y coordinates. I think that this failed due to my ball not moving one pixel at a time and therefore skipping the if-statement checking for y coordinates. Could probably be refined to work but would also probably be very ineffecient. So screw number 3.
Since I have 7 classes I'll post a link to a zip with all files in it. It's pretty well commented I think. Maybe not.. at least a little. [http://styrman.madtracker.net/Diamonds3.rar]
Any tips will be most welcome =)
Thanks for reading my long thread.. ^^
EDIT: Forgot to mention. in the collision method I need to be able to decide from what direction the bal hits the block. if the ball hits top or bottom, just change direction (bounce), if the ball hits sides keep going up/down but bounce the ball a little to the side, if ball hits the corner of a block send it back the same way it came.