@@ -537,11 +537,11 @@ public void dispose() {
537537 protected PVector [] ray ;
538538 protected PVector hit = new PVector ();
539539 protected PVector screen = new PVector ();
540- protected PVector objCenter = new PVector ();
541- protected PVector objToOrig = new PVector ();
540+
542541 protected PVector origInObjCoord = new PVector ();
543542 protected PVector hitInObjCoord = new PVector ();
544543 protected PVector dirInObjCoord = new PVector ();
544+
545545 protected PVector origInWorldCoord = new PVector ();
546546 protected PVector dirInWorldCoord = new PVector ();
547547
@@ -933,7 +933,6 @@ public void eye() {
933933
934934 // RAY CASTING
935935
936-
937936 @ Override
938937 public PVector [] getRayFromScreen (float screenX , float screenY , PVector [] ray ) {
939938 if (ray == null || ray .length < 2 ) {
@@ -971,22 +970,24 @@ public boolean intersectsSphere(float r, float screenX, float screenY) {
971970
972971 @ Override
973972 public boolean intersectsSphere (float r , PVector origin , PVector direction ) {
974- // Get the center of the sphere in world coordinates
975- objCenter .x = modelview .m03 ;
976- objCenter .y = modelview .m13 ;
977- objCenter .z = modelview .m23 ;
973+ objMatrix = getObjectMatrix (objMatrix );
974+ objMatrix .mult (origin , origInObjCoord );
975+ PVector .add (origin , direction , hit );
976+ objMatrix .mult (hit , hitInObjCoord );
977+ PVector .sub (hitInObjCoord , origInObjCoord , dirInObjCoord );
978978
979- PVector .sub (origin , objCenter , objToOrig );
980- float d = objToOrig .mag ();
979+ float d = origInObjCoord .mag ();
981980
982981 // The eye is inside the sphere
983982 if (d <= r ) return true ;
984983
984+ float p = PVector .dot (dirInObjCoord , origInObjCoord );
985+
985986 // Check if sphere is in front of ray
986- if (PVector . dot ( objToOrig , direction ) > 0 ) return false ;
987+ if (p > 0 ) return false ;
987988
988989 // Check intersection of ray with sphere
989- float b = 2 * PVector . dot ( direction , objToOrig ) ;
990+ float b = 2 * p ;
990991 float c = d * d - r * r ;
991992 float det = b * b - 4 * c ;
992993 return det >= 0 ;
@@ -1101,14 +1102,18 @@ public PVector intersectsPlane(float screenX, float screenY) {
11011102 public PVector intersectsPlane (PVector origin , PVector direction ) {
11021103 modelview .mult (origin , origInWorldCoord );
11031104 modelview .mult (direction , dirInWorldCoord );
1105+ dirInWorldCoord .normalize ();
11041106
1105- // Ray-plane intersection algorithm
1106- PVector w = new PVector (- origInWorldCoord . x , - origInWorldCoord . y , - origInWorldCoord . z );
1107- float d = PApplet . abs ( dirInWorldCoord . z * dirInWorldCoord . z );
1107+ // Plane representation
1108+ PVector point = new PVector (0 , 0 , 0 );
1109+ PVector normal = new PVector ( 0 , 0 , 1 );
11081110
1111+ // Ray-plane intersection algorithm
1112+ float d = PApplet .abs (PVector .dot (normal , dirInWorldCoord ));
11091113 if (d == 0 ) return null ;
11101114
1111- float k = PApplet .abs ((w .z * w .z )/d );
1115+ PVector w = PVector .sub (point , origInWorldCoord );
1116+ float k = PApplet .abs (PVector .dot (normal , w )/d );
11121117 PVector p = PVector .add (origInWorldCoord , dirInWorldCoord ).setMag (k );
11131118
11141119 return p ;
0 commit comments