7
neeno
4y

Achieving decent 2D collision detection using ray casting in unity is kinda hard. I have immense respect for gameplay programmers.

I'm almost there though...

Comments
  • 2
    Collisions are difficult!
    And fun!

    And then there's physics engines....

    I totally respect the devs who write those.
  • 0
    @Root yep, they really are. I'm only working with square colliders and I'm already afraid of needing to implement more complex shapes in the future... But it sure is fun, been playing with this for about 3 hours now.
  • 3
    @ItsaMeTuni Squares are the second-easiest. Go for triangles next; they're still on the easy side, but will lead you to the solutions for complicated shapes. 😊

    I went from there to a pentagon, to a star, to a spaceship png.
  • 1
    @Root Neat, thanks!
  • 2
    Unity doesn't have collision for 2D shapes? Confused...

    Been playing with Godot and 3D rigid bodies.
  • 1
    @Demolishun It does, but using the physics engine for a 2D character controller is a nono
  • 2
    @ItsaMeTuni Huh, I wonder if Godot supports that. Will have to check. Never really played with Unity.

    Please post any videos you create for your project. Those are always fun to check out.
  • 1
    That's why I only work with 3D.
  • 1
    @Demolishun I don't actually handle the collision detection itself, I use some of unity's functions for that. The hard part is preventing the player from getting into walls, glitching, losing all momentum when hitting something on an angle, etc.

    I plan to start a Dev channel with a video on this game. Kinda like Dani, Randall or ThinMatrix (these guys inspire me quite a lot).
  • 1
    @Ranchu correct me if I'm wrong but, as far as I know, a 3D character controller would need the same sort of thing, but a bit harder to implement because of the extra dimension.
  • 1
    @ItsaMeTuni "The secret ingredient is plagiarism"
  • 2
    @M1sf3t Circle/sphere is the easiest, doubly so when all shapes are circles/spheres. It's literally just a distance check with the object's radius (plus consdering speed, etc.)

    Squares and rectangles are the second easiest, especially if there is no rotation involved. iterate over all objects, check if your shape includes any of their points. Rotation turns an otherwise simple shape into a much more complicated one.

    Collisions between circles and rects are pretty simple, especially without rotation: circle-in-square. Treat the circle like a square and do bounds checking on the rects. if there's a collision, do a radius check to handle the corner cases :)

    Triangles change the logic from simple bounds checking into real math (algebra! and geometry! but sadly no calc). This will make you want to wirte a more general approach that will probably apply to rotating shapes as well.

    Pentagons force you to consider the general case for any polygon -- probably a small change if you did the triangle well.

    Stars are another large leap because they are concave, and therefore the collision checking is more complex. You can position two stars such that their radii intersect, but the shapes themselves do not touch.

    From here, it's a matter of optimizing your concave collision checking to be more general case, and to work with arbitrary shapes e.g. PNGs. Generally by creating a vector map of the shape.

    From here is another step: more complicated shapes that have isolated pieces, such as the tittle (dot) on a lowercase "i" or "j". For real-physics scenarios, you can usually ignore these, but they help when implementing/optimizing the next two steps:

    Three dimensions makes all of this more difficult.

    And one last step (at least before adding more dimensions like time): concave 3d shapes, specifically those with holes. Think of a pipe, or a car driving under a bridge.

    There are also a lot of ingenious (and sometimes painfully simple) ways to optimize these checks.
  • 0
    @M1sf3t 😅
    Ir can be tricky and frustrating.

    If you consider the current placement and one tick's velocity when doing collisions, you'll be able to do ricochets accurately, and it should prevent any clipping
Add Comment