Results 1 to 3 of 3
- 12-08-2010, 05:05 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Solved: Checking for collision of two rectangles using intersect
I'm trying to make a pong game. I've got a basic working example. I'm using intersects to check and see if the ball (a rectangle) intersects the right bumper (another rectangle). If they do intersect, then the ball should go the other way.
Here's the statement I'm using to check for intersection:
Once the ball does intersect the right bumper it will go the other way by multiplying dx by -1. The problem is dx is then constantly multiplied by -1 for some reason and just bounces back and forth by 5 pixels. I can't seem to figure out why intersects keeps returning true even if the ball doesn't intersect the bumper.Java Code:if (ball.intersects(rightBumper) == true) { dx *= -1; }
I've also tried using hit from Graphics2D, but I get the same result.
Can anyone point me in the right direction?Last edited by Boatski; 12-09-2010 at 01:39 PM. Reason: Solved
- 12-09-2010, 04:53 AM #2
Member
- Join Date
- Jun 2010
- Posts
- 28
- Rep Power
- 0
There's a disconnect between your x and y variables and when the ball is actually updated to these values.
Basically, just delete those variables and replace all the x's with box.x and all the y's with box.y. Also delete this statement in paintComponent
The ball will be moved in the actionPerformed method instead (when you change the x's and y's to box.x and box.y).Java Code://Moves the ball around ball.setRect(x, y, 25, 25);
- 12-09-2010, 01:40 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Why only one of the two rectangles is painted?
By JOHNINALBANY in forum Java 2DReplies: 5Last Post: 07-07-2012, 10:54 PM -
Collision between 2 rectangles
By CNew in forum New To JavaReplies: 1Last Post: 12-05-2010, 04:18 AM -
Rectangles method
By bdario1 in forum New To JavaReplies: 31Last Post: 03-31-2010, 09:32 PM -
Area Intersect
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:07 PM -
Area Calculation: Add, Subtract, Intersect, Exclusive Or
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks