The goal of this sketch was to establish the x-y constraints for the puck (and later Players) moving around a Hockey rink.

This ended up being a fairly involved process, including one off-shoot exploration of geometry in: https://github.com/brianhonohan/sketchbook/pull/91

Some notes about my approach:

  • 4c997515 - Creating a “mock” puck with a draggable trajectory vastly accelerated my development
    • This was inspired by that PR above.
  • I used “Viewer” classes to translated the pure data models to the screen
    • This was an approach I established in the base sketch and continued here. I’ve done a fair bit of other sketches in the mean time without this dual mode of translating screen coordinates down to model coords, and back … but once I re-bought into it (and had simple utility functions: like transformXToRinkX(...) and transformRinkXToX(...)) it wasn’t too bad.

I’ll try to do a more in depth write but, here is the gist of the collision detection added in this PR:

Step 1 - Establish (simple) Open Areas of the ice

If the Puck is in this areas, don’t need to perform any further checks or alter position or velocity.

rink_debug_simple_open_areas

But if the Puck is beyond the min X or Y, or max X or Y, then try to determine the board it hit.

  • b5405807 - Initial (albeit flawed) approach to determine the end or side boards (straight line boundaries)
  • Fixed / improved in these two commits to factor in the starting position of the puck, because it may have cross through a corner to get to the zone that would previously been interpreted as having hit a straight end or side board.

Step 2 - Add another (overlapping) open area for simple checks

This has two benefits:

  • Adds to the area that a quick check (without performing distance checks with square roots) will suffice when the Puck is in an open area.
  • Handles the case I was seeing where the Puck would be straddling two of the open areas but not fully in either one; thus wrongly interpreted as violating a constraint.

rink_debug_simple_open_areas-02

Step 3 - Consider the corners to be open areas

This came through in two commits:

  • 9e9596cd - Simply defines the corners as Circle objects and renders them.
  • 80849d95 - Determines if the Puck is within the corner Circle, factoring in the Puck’s radius.

rink_debug_simple_open_areas-03

Step 4 - Determine the point on the corner that the Puck has hit

This was added through:

  • ae9ef41b1 - Initial commit, which included visual debugging areas;
    • My underlying algorithm would return two points that the LineSegment of the Puck’s trajectory will hit on the Circle; it needs to be in both of the rectangles to be the desired point.
  • 126c4ccac - A fix in creating what is shown as the yellowish rectangle. (Needed to factor in the Puck’s maximum extents, and thus used the Rect.expandToInclude(...) to increase the Yellow rectangle to include the full Puck).

rink_debug_corner_collision-01

Step 5 - (Try to) Bounce the Puck off the corners

  • 1b735880 - Visual debugging of the Puck’s new location and velocity
  • b47c8912 - Preliminary (but unfortunately bug-ridden) approach to bouncing a puck off of a curved surface

(Screenshot shows the incorrect calculation of the new Puck location … but the errors were different for each corner, with the bottom left corner being nearly perfect, so I new I was on the right path).

rink_debug-

Step 6 - Debugging / tweaking

Eventually ironed out most of the bugs.

(I think there are other improvements to be made regarding collision points of end and side boards, but this is satisfactory for now).

hockey-6

Next:

  • Consider recursively apply the constraints in a single frame, sometimes it bounces off of two walls (or same wall twice or more) in the same iteration; currently that is not handled and the puck is allowed to disappear.
  • But I think I might jump to some goal line detection (and maybe basic icing) and hopefully move onto autonomous agents that can learn to play Hockey.