Processing - Hockey 01
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(...)
andtransformRinkXToX(...)
) it wasn’t too bad.
- 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
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.
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.
Step 3 - Consider the corners to be open areas
This came through in two commits:
9e9596cd
- Simply defines the corners asCircle
objects and renders them.80849d95
- Determines if the Puck is within the cornerCircle
, factoring in the Puck’s radius.
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 theCircle
; it needs to be in both of the rectangles to be the desired point.
- My underlying algorithm would return two points that the
126c4ccac
- A fix in creating what is shown as the yellowish rectangle. (Needed to factor in the Puck’s maximum extents, and thus used theRect.expandToInclude(...)
to increase the Yellow rectangle to include the full Puck).
Step 5 - (Try to) Bounce the Puck off the corners
1b735880
- Visual debugging of the Puck’s new location and velocityb47c8912
- 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).
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).
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.