@@ -5,38 +5,73 @@ class Cylinder extends Collider {
55 public var a : Point ;
66 public var b : Point ;
77 public var r : Float ;
8+ static var tmpSphere = new Sphere (0. , 0. , 0. , 0. );
89
9- public inline function new (a : Point , b : Point , r : Float ) {
10+ public inline function new ( a : Point , b : Point , r : Float ) {
1011 this .a = a ;
1112 this .b = b ;
1213 this .r = r ;
1314 }
1415
1516 public function rayIntersection ( r : Ray , bestMatch : Bool ) : Float {
16- throw " Not implemented" ;
17+ var ro = r .getPos ();
18+ var rd = r .getDir ();
19+ var ra = this .r ;
20+ var pa = a ;
21+ var pb = b ;
22+ var ba = pb - pa ;
23+ var oa = ro - pa ;
24+ var baba = ba .dot (ba );
25+ var bard = ba .dot (rd );
26+ var baoa = ba .dot (oa );
27+ var rdoa = rd .dot (oa );
28+ var oaoa = oa .dot (oa );
29+ var a = baba - bard * bard ;
30+ var b = baba * rdoa - baoa * bard ;
31+ var c = baba * oaoa - baoa * baoa - ra * ra * baba ;
32+ var h = b * b - a * c ;
33+ if ( h >= 0.0 ) {
34+ var hs = hxd. Math .sqrt (h );
35+ var t = (- b - hs )/ a ;
36+ var y = baoa + t * bard ;
37+ if ( y > 0.0 && y < baba )
38+ return t ;
39+ t = ((y < 0.0 ? 0.0 : baba ) - baoa ) / bard ;
40+ if ( hxd. Math .abs (b + a * t ) < hs )
41+ return t ;
42+ }
43+ return - 1 ;
1744 }
1845
1946 public inline function contains ( p : Point ) : Bool {
20- throw " Not implemented" ;
47+ var t = p .sub (a ).dot (b .sub (a )) / a .distanceSq (b );
48+ if ( t < 0 || t > 1 )
49+ return false ;
50+ return p .distanceSq (new Point (a .x + t * (b .x - a .x ), a .y + t * (b .y - a .y ), a .z + t * (b .z - a .z ))) < r * r ;
2151 }
2252
2353 public function inFrustum ( f : Frustum , ? m : h3d. Matrix ) : Bool {
24- throw " Not implemented" ;
54+ if ( m != null )
55+ throw " Not implemented" ;
56+ tmpSphere .load (a .x + (b .x - a .x ), a .y + (b .y - a .y ), a .z + (b .z - a .z ), dimension () * 0.5 );
57+ return tmpSphere .inFrustum (f );
2558 }
2659
2760 public function inSphere ( s : Sphere ) : Bool {
28- throw " Not implemented" ;
61+ tmpSphere .load (a .x + (b .x - a .x ), a .y + (b .y - a .y ), a .z + (b .z - a .z ), dimension () * 0.5 );
62+ return tmpSphere .inSphere (s );
2963 }
3064
3165 public function toString () {
3266 return " Cylinder{" + a + " ," + b + " ," + hxd. Math .fmt (r ) + " }" ;
3367 }
3468
35- public function dimension () : Float {
36- throw " Not implemented" ;
69+ public inline function dimension () : Float {
70+ var h2 = a .distance (b ) * 0.5 ;
71+ return 2 * hxd. Math .sqrt (h2 * h2 + r * r );
3772 }
3873
39- public function closestPoint (p : Point ) : Point {
74+ public function closestPoint ( p : Point ) : Point {
4075 throw " not implemented" ;
4176 }
4277
0 commit comments