phetsims / kite

A library for creating, manipulating and displaying 2D shapes in JavaScript.
http://scenerystack.org/
MIT License
15 stars 6 forks source link

Incorrect hit area for trapezoid #34

Closed samreid closed 10 years ago

samreid commented 10 years ago

I noticed an incorrect hit area for this trapezoid in Seasons:

M 415 298.5 L 414.99999999999994 94.5 L 468.596798162286 101.08659447295564 L 468.59679816228606 291.91340552704435 Z

kite-map

samreid commented 10 years ago

Some notes about @jonathanolson and my work on this so far. Following the maths at http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect/565282#565282 we tried:

      var delta = this._end.minus( this._start );
      var divisor = delta.crossScalar( ray.dir );
      if ( divisor === 0 ) {
        return result;
      }
      var t = ray.pos.minus( this._start ).crossScalar( ray.dir ) / divisor;
      if ( t < 0 || t >= 1 ) {
        return result;
      }

as a replacement for

      // check to make sure our point is in our line segment (specifically, in the bounds (start,end], not including the start point so we don't double-count intersections)
      if ( start.x !== end.x && ( start.x > end.x ? ( intersection.x >= start.x || intersection.x < end.x ) : ( intersection.x <= start.x || intersection.x > end.x ) ) ) {
        return result;
      }
      if ( start.y !== end.y && ( start.y > end.y ? ( intersection.y >= start.y || intersection.y < end.y ) : ( intersection.y <= start.y || intersection.y > end.y ) ) ) {
        return result;
      }

in Line.js

and found it is much less buggy for the case above (the interior of the trapezoid looks solved), but there still looks like a problem, see below:

test

samreid commented 10 years ago

The above proposal does pass all kite unit tests (after renaming another t variable below to t2 for the "behind the ray test) and no error cases are detectable within seasons (including outside of the panel as pictured above), the sim where this bug was identified. Perhaps the above proposal is safe to proceed with? Let's discuss.