2

My question can actually be broken down into two parts. The first, is what is a reasonable method for determining collision in a Java game? My second, is how to find the point of collision for use later?

Originally I was implementing a rudimentary collision system using a thread I found earlier with a simple method for finding a collision in a polygon. I have posted the code below. However as best I can tell, this will not show me the collision point, and I would have to do something separate to find it.

public boolean collisionDetection(int charX, int charY) {
    if(boundry == null)
        return false;

    int numVert = boundry.length;
    boolean ret = false;
    for(int i = 0, j = numVert - 1; i < numVert; j = i++) {
        if (((boundry[i].y >= charY) != (boundry[j].y >= charY)) && (charX <= (boundry[j].x -boundry[i].x) * (charY - boundry[i].y) / (boundry[j].y - boundry[i].y) + boundry[i].x)) {
             ret = !ret;
        }
    }
    return ret;
}

But I had a thought while working on this... Let us presume that I input my coordinates into the system as Points. I did them in order (e.g. point 1 -> point 2 -> point 3 -> point 1).

Knowing the points are in a connected order (e.g. they form the boundary of the object), is it then logical to say I could just use a number of line intersection methods to find if the character intersected with the border of my polygon? I know the characters current position, as well as the intended position.

Any thoughts, or if you have a suggestion a better implementation method?

Sh4d0wsPlyr
  • 948
  • 12
  • 28
  • `Knowing the points are in a connected order (e.g. they form the boundary of the object), is it then logical to say I could just use a number of line intersection methods to find if the character intersected with the border of my polygon?` If we're talking about two polygons in 2D, then yes. – biziclop Jul 28 '15 at 21:43
  • 2
    And if the character can not be completely *inside* the polygon (or vice versa), then yes.... – Marco13 Jul 28 '15 at 21:51
  • To answer - yes, 2D space. I have some concerns about being "inside" of the polygon, but I am sure I can deal with that later (or run a check). – Sh4d0wsPlyr Jul 28 '15 at 22:15

1 Answers1

0

you need to be doing either point to pint or box to box collision See my answer here

this explains two methods of collision detection. its in 3D but just drop the third axis and can be used for 2D. Hope this helps

Community
  • 1
  • 1
Rob85
  • 1,719
  • 1
  • 23
  • 46