Results 1 to 2 of 2
  1. #1
    Äppelpaj is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Wink Collision Detection

    Hello

    im using netBeans as a program.
    I am searching for a simple script for a collision detection when my player (p) hits an object (ob1), it gonna show that player hits object. Something like system.out.println ();

    Anyone know what to do?

    Greetings// Äppelpaj

  2. #2
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Collision Detection

    It depends on the shape of the objects. Most object shapes can be approximated fairly well by rectangles, so I'll just assume for now that both objects are rectangles.

    For collision detection of rectangles, I personally find it's convenient to store the rectangle data in the following way:
    x, y = the coordinates of the CENTER of the rectangle (as opposed to the upper-left corner, which is traditional).
    w, h = HALF of the dimensions of the rectangle (in other words, w = distance from center to left or center to right, h = distance from center to top or center to bottom)

    Using this way of storing the data makes collision detection extremely easy:
    Java Code:
    if(Math.abs(p.x-ob.x)<p.w+ob.w && Math.abs(p.y-ob.y)<p.h+ob.h){
        //collision
    }else{
        //no collision
    }
    Last edited by DiamondSoul; 10-13-2011 at 03:29 PM. Reason: forgot closing [/code] tag

Similar Threads

  1. LWJGL Collision Detection
    By Furyright in forum Java 2D
    Replies: 1
    Last Post: 10-09-2011, 06:48 AM
  2. Collision Detection
    By sunde887 in forum Java 2D
    Replies: 2
    Last Post: 10-07-2011, 11:40 PM
  3. Collision Detection?
    By Alerhau in forum New To Java
    Replies: 39
    Last Post: 09-07-2011, 04:55 PM
  4. Really Need help with some collision detection
    By Harwad in forum New To Java
    Replies: 1
    Last Post: 01-23-2011, 12:38 AM
  5. Collision Detection
    By dotabyss in forum Java Gaming
    Replies: 0
    Last Post: 03-14-2010, 06:13 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
  •