Results 1 to 14 of 14
Thread: Collision dectection
- 02-27-2012, 08:15 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Collision dectection
Hi folks
Back again. Trying to register two moving objects intersecting on the frame. Code as follows:
But no matter what, collision always prints the line. I've even tried if(!o.intersects(x)); to see what happened and I still get the output. Any hints?Java Code:public Rectangle drawBat() { x = new Rectangle(batX, batY, breadth, length); return x; } public Rectangle drawBall() { o = new Rectangle(ballX, ballY, circumference, circumference); return o; } public void collision(Rectangle o, Rectangle x) { o = drawBall(); x = drawBat(); if(o.intersects(x)); { System.out.println(x.x + "..." + x.y + "..." + o.x + ".." + o.y); } }
ThanksLast edited by jazzermonty; 02-27-2012 at 08:16 PM. Reason: typo
- 02-27-2012, 08:33 PM #2
Re: Collision dectection
If you want help, you have to provide an SSCCE. You're using a few variables there whose values are completely unknown to us, which makes it impossible to help you.
Have you tried drawing the objects on the screen? Do they indeed intersect?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-27-2012, 08:56 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Re: Collision dectection
Hi Kevin
Appologies. Variables declaration as follows:
The bat and ball move about the screen using an external container within another class, and yes, the do intersect (or not, depending on the position of the bat or ball.Java Code:this.breadth = 10; this.circumference= 60; this.batLength = 125; this.batY = 160; this.batX = 460; this.ballX = 200; this.ballY = 250;
But the method above 'collision' always prints the line, wither it's true or false.
Thanks
- 02-27-2012, 09:01 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Re: Collision dectection
Ok, I realise I'm not being very clear here. Let me put this in context. I have a JPanel that draws the bat and ball on screen. The bat moves depending on the arrow keys (this works) and the ballbounces about on screen( this works) if it hits the edge of the screen. I now want to detect the collision point of the bat and ball. batY changes depending on the arrow up or arrow down. The ball moves about using an offset.
Is this enough?
- 02-27-2012, 09:19 PM #5
Re: Collision dectection
Draw a picture of the ball and bat when they are colliding.want to detect the collision point of the bat and ball.
Write on the drawing their x,y location. Now do some arithmetic to compute the x,y using the size of each to determine when they are colliding.
Draw another picture where the ball and bat are not colliding and verify on it that your formulas for when they collide is correct when they are not colliding.
Look at the Polygon class. It has methods that can help.
- 02-27-2012, 09:24 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
- 02-27-2012, 09:28 PM #7
Re: Collision dectection
I made suggestions for two ways to do it. Your choice.
One could give you the x,y of where the collision occurred.
The other gives a true/false for if a collision occurred.
- 02-27-2012, 09:32 PM #8
Re: Collision dectection
if(o.intersects(x));
- 02-27-2012, 09:33 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Re: Collision dectection
Sorry Norm, still not following.
I've been trying to implement this:
http://http://docs.oracle.com/javase...awt.Rectangle)
I've seen examples where this works, just not for me. And I'm doing what you say in your previous post (see line 5).
Java Code:public void collision(Rectangle o, Rectangle x) { o = drawBall(); x = drawBat(); if(!o.intersects(x)); { System.out.println(x.x + "..." + x.y + "..." + o.x + ".." + o.y); } }Last edited by jazzermonty; 02-27-2012 at 09:37 PM.
- 02-27-2012, 09:44 PM #10
Re: Collision dectection
Look at post #8 and see what is colored RED
- 02-27-2012, 09:49 PM #11
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
- 02-27-2012, 09:51 PM #12
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Re: Collision dectection
Ah, just got it. Thanks a lot Norm, really helpful.
You guys rock (bloomin syntax errors).
- 02-27-2012, 09:52 PM #13
Re: Collision dectection
Bummer.
What is wrong with the compiler you are using. When I compile this statement I get a warning:
Java Code:TestCode10.java:473: warning: [empty] empty statement after if if("x".equals("y")); ^
- 02-27-2012, 09:54 PM #14
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Similar Threads
-
ImageView Collision
By TP-Oreilly in forum AndroidReplies: 1Last Post: 01-30-2012, 02:17 AM -
Collision
By PhQ in forum New To JavaReplies: 5Last Post: 12-12-2011, 02:20 AM -
2D - Collision messed up
By überfuzz in forum New To JavaReplies: 0Last Post: 06-22-2011, 09:20 PM -
Collision in GUI 2d Game help
By heysayjump in forum New To JavaReplies: 7Last Post: 03-14-2011, 02:01 AM -
Collision
By shadycharacter in forum New To JavaReplies: 0Last Post: 04-13-2010, 09:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks