1
\$\begingroup\$

enter image description here

I have some difficulties to understand how to be sure if there is a collision between a space ship (black) and an asteroid (in blue). A space ship is placed on a case and an asteroid comes from the right, like on the image.

Also Y1 != Y2, there are not exactly on the same line. What is the best way, in that case, to detect if there is a collision ? Is AABB collision the correct algorith I need ?

\$\endgroup\$
  • \$\begingroup\$ Hi Asm, are you using the shapes of the objects as the colliders? If you are then you can split your hexagon into 2 triangles and a square to simplify your collision algorithm, or are you just checking for a point to point collision? \$\endgroup\$ – Matthew Pigram Dec 19 '14 at 0:12
  • \$\begingroup\$ Hi Matthew, thank you for your comment. I confirm that I'm using the shapes of the objects. If an asteroid touch the point (X2, Y2) of the triangle, the space ship will be erased. I have got severeal lines and columns and many objects on my grid. \$\endgroup\$ – Asm Dec 19 '14 at 0:18
  • \$\begingroup\$ possible duplicate of Triangle-triangle continuous collision detection \$\endgroup\$ – congusbongus Dec 19 '14 at 0:30
  • \$\begingroup\$ You probably want the separation algorithm, but are you aware that most games use simple hitboxes rather than complex polygon collision detection? \$\endgroup\$ – sam hocevar Dec 19 '14 at 2:32
1
\$\begingroup\$

I think either AABB collision test or circle-circle is sufficient for your needs. Usually it is not necessary to have perfect pixel level precision in determining whether 2 objects hit.

For circle-circle, have all your objects have a center x,y and a radius r; to determine whether 2 circles overlap: test (obj1.x-obj2.x)^2 + (obj1.y-obj2.y)^2 > (obj1.r + obj2.r)^2

For box, have all your objects have x,y, width and height, and check whether the box overlap in x axis and y axis.

Edit: In case that you really really want to use triangle - polygon level of precision, you need to first implement/find a intersection test for lineSegment vs lineSegment. Then for the intersection test between 2 polygons, test whether any combination of their edges intersects OR whether one object fully contains the other (point in shape test).

\$\endgroup\$
0
\$\begingroup\$

To complete @user55564 I give some more information about line segments intersection.

In general you can check if any Intersection exist between your two polygon (even non-convex) as what I've described Here.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.