This is my very first post and I am new to this forum. I wish to both benefit and contribute to this forum with regards to Java Programming. Anyway, I not new to Java but new to Java Game Development. I am making a very basic version of break out. To briefly describe what breakout is: It is a simple game where the players paddle is located on the bottom of the screen with a ball bouncing around the screen. The player must move the paddle (located on the bottom of the screen) left or right and prevent the ball from hitting the south side of the screen. On the top of the screen are where all the bricks are positioned. When the ball hits the players paddle and bounces back up it hits one of the bricks. At this point the brick hit disappears and the ball then bounces back down towards the player's paddle. This is the basic principle of breakout.
The problem I am having is this. I am using intersects to detect the collision between the ball on the screen as it bounces about with both the player's paddle and the bricks. The collision works but to a certain extent. When using intersects, when the ball say moves towards the north side of the paddle, the ball hits the paddle and collision works fine. If the ball however moves towards the paddle from the south side of the paddle, the ball travels through the paddle and when it hits the north side it then reacts as per collision design. In essence, collision (intersects) works only when the ball hits either the paddle or a brick if it hits the object from a certain side. Otherwise it travels through the object and once it hits the other side, it then reacts to collision (e.g. brick disappears, ball bounces etc).
As I am new to intersects I am not sure why this is happening.
I have given the collision code below.
Thanks in advance for any help provided.
Also, sorry for the long post. This is my first post and was not entirely sure how much info to provide.
It may be an issue with your setYDirection() method. Could you post that? Also, the instance data for the ball class would be helpful.
And how are you going about dealing with motion? Do you have velocity variables and a tick? or are you doing position with logic?
Joined: Mar 04, 2012
Here is the code for the ball:
The ball is a thread object where at each interval of Thread.sleep the move command is called. This makes the ball moves (the players paddle is similar to this). There really isnt a velocity per say. As reuqested the setYDirection is also present in the code. The code provided is the entirty of the ball class. Once again, any help would be welcome.
Alright, I'm not ENTIRELY sure, as my set-up when I do something like this is a bit different. I believe the problem is with your setX/YDirection methods.
Instead of only negating that variable when intersects occurs, you want to toggle it. I would probably write it something like this...
This way, if the variable is positive, it'll become negative, and vice versa.
Your setup before only allowed the intersects to work from one direction, because if the variable was already negative, then nothing would really occur, and it would go right through.
When I work with my movement, I have a velocity variable which does the same thing--toggles when necessary.
Hope this helps,
Joined: Mar 04, 2012
Thanks a lot buddy. You really helped me out here. I wasn't event aware of the fact that a negative number would remain negative. Yes, your idea worked perfectly for me.