Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy collision checking impossible???

    ive been searching far and wide for how to detect collisions with java for a game that im making(pong)... not any other language BUT java... am i on a wild goose chase? i think it must be possible cause i play java games all the time that would have to use collision checking... i was thinking something like this:

    public void checkIfBounce(){
    if(ball_x-radius==paddle_x+10){ // if ball&paddle are the same x
    for(int i=-1;i<40;i++){ // 40 is the paddles length
    if(paddle_y+i==ball_y)ballXspeed=-1;
    }
    }
    }

    i thought i could call this in the run() methed of an applet to see if the ball and the paddle had made a collision, then the for loop would test each possible position to see if they had contacted, but it doesnt work... anyone know why? if you know how to detect collisions in java or know a site that tells you how, please tell me...

    thanks...

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A for loop?!?! Sheesh! Why not just compare the ball's position with the top and bottom of the paddle?
    Code:
    // I guess you need circle to rectangle collision detection, right?
    if (ball_x + radius >= paddle_x && ball_x - radius <= paddle_x + 10) {
      if (ball_y +radius >= paddle_y && ball_y - radius <= paddle_y + 40) {
        // BAM!!!
      }
    }
    Off course depending on the ball's speed it could completely miss the paddle, but seeing as you set ballXspeed to -1 I don't think this is the case.
    You could also just use one of the java.awt.Shape subclasses and their contains/intersects methods.

    shmoove


  •  

    Posting Permissions

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