@@ -537,11 +537,11 @@ public void dispose() {
537
537
protected PVector [] ray ;
538
538
protected PVector hit = new PVector ();
539
539
protected PVector screen = new PVector ();
540
- protected PVector objCenter = new PVector ();
541
- protected PVector objToOrig = new PVector ();
540
+
542
541
protected PVector origInObjCoord = new PVector ();
543
542
protected PVector hitInObjCoord = new PVector ();
544
543
protected PVector dirInObjCoord = new PVector ();
544
+
545
545
protected PVector origInWorldCoord = new PVector ();
546
546
protected PVector dirInWorldCoord = new PVector ();
547
547
@@ -933,7 +933,6 @@ public void eye() {
933
933
934
934
// RAY CASTING
935
935
936
-
937
936
@ Override
938
937
public PVector [] getRayFromScreen (float screenX , float screenY , PVector [] ray ) {
939
938
if (ray == null || ray .length < 2 ) {
@@ -971,22 +970,24 @@ public boolean intersectsSphere(float r, float screenX, float screenY) {
971
970
972
971
@ Override
973
972
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 );
978
978
979
- PVector .sub (origin , objCenter , objToOrig );
980
- float d = objToOrig .mag ();
979
+ float d = origInObjCoord .mag ();
981
980
982
981
// The eye is inside the sphere
983
982
if (d <= r ) return true ;
984
983
984
+ float p = PVector .dot (dirInObjCoord , origInObjCoord );
985
+
985
986
// Check if sphere is in front of ray
986
- if (PVector . dot ( objToOrig , direction ) > 0 ) return false ;
987
+ if (p > 0 ) return false ;
987
988
988
989
// Check intersection of ray with sphere
989
- float b = 2 * PVector . dot ( direction , objToOrig ) ;
990
+ float b = 2 * p ;
990
991
float c = d * d - r * r ;
991
992
float det = b * b - 4 * c ;
992
993
return det >= 0 ;
@@ -1101,14 +1102,18 @@ public PVector intersectsPlane(float screenX, float screenY) {
1101
1102
public PVector intersectsPlane (PVector origin , PVector direction ) {
1102
1103
modelview .mult (origin , origInWorldCoord );
1103
1104
modelview .mult (direction , dirInWorldCoord );
1105
+ dirInWorldCoord .normalize ();
1104
1106
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 );
1108
1110
1111
+ // Ray-plane intersection algorithm
1112
+ float d = PApplet .abs (PVector .dot (normal , dirInWorldCoord ));
1109
1113
if (d == 0 ) return null ;
1110
1114
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 );
1112
1117
PVector p = PVector .add (origInWorldCoord , dirInWorldCoord ).setMag (k );
1113
1118
1114
1119
return p ;
0 commit comments