Results 1 to 14 of 14
  1. #1
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Collision dectection

    Hi folks

    Back again. Trying to register two moving objects intersecting on the frame. Code as follows:

    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);
            }
        }
    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?

    Thanks
    Last edited by jazzermonty; 02-27-2012 at 08:16 PM. Reason: typo

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default 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!

  3. #3
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Collision dectection

    Quote Originally Posted by KevinWorkman View Post
    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?
    Hi Kevin

    Appologies. Variables declaration as follows:

    Java Code:
    this.breadth = 10;
            this.circumference= 60;
            this.batLength = 125;
            this.batY = 160;
            this.batX = 460;
            this.ballX = 200;
            this.ballY = 250;
    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.

    But the method above 'collision' always prints the line, wither it's true or false.

    Thanks

  4. #4
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default 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?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Collision dectection

    want to detect the collision point of the bat and ball.
    Draw a picture of the ball and bat when they are colliding.
    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.

  6. #6
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Collision dectection

    Quote Originally Posted by Norm View Post
    Draw a picture of the ball and bat when they are colliding.
    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.
    Sorry Norm, you lost me there. I thought the intersect() class would detect this for me, rather than(as I suspect your getting at) doing this longhand. Is this what you mean?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default 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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Collision dectection

    if(o.intersects(x));

  9. #9
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default 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.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Collision dectection

    Look at post #8 and see what is colored RED

  11. #11
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Collision dectection

    Quote Originally Posted by Norm View Post
    Look at post #8 and see what is colored RED
    Not seeing the RED (colour blindness sucks). Thanks for you patience.

  12. #12
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Collision dectection

    Ah, just got it. Thanks a lot Norm, really helpful.

    You guys rock (bloomin syntax errors).

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default 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"));
                             ^

  14. #14
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Collision dectection

    Quote Originally Posted by Norm View Post
    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"));
    
    
                             ^
    Nothing, my java foo ain't soo good. I replied in post #12 where I realised what you were getting at. Cheers for your help once again.

Similar Threads

  1. ImageView Collision
    By TP-Oreilly in forum Android
    Replies: 1
    Last Post: 01-30-2012, 02:17 AM
  2. Collision
    By PhQ in forum New To Java
    Replies: 5
    Last Post: 12-12-2011, 02:20 AM
  3. 2D - Collision messed up
    By überfuzz in forum New To Java
    Replies: 0
    Last Post: 06-22-2011, 09:20 PM
  4. Collision in GUI 2d Game help
    By heysayjump in forum New To Java
    Replies: 7
    Last Post: 03-14-2011, 02:01 AM
  5. Collision
    By shadycharacter in forum New To Java
    Replies: 0
    Last Post: 04-13-2010, 09:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •