Browse Source

Sphere-Ray-Intersection (untested)

seba 11 years ago
parent
commit
71be63329c
2 changed files with 48 additions and 11 deletions
  1. 45
    8
      geotypes.cpp
  2. 3
    3
      geotypes.h

+ 45
- 8
geotypes.cpp View File

@@ -62,6 +62,51 @@ Punkt3D Sphere::getPos() const {
62 62
 	return pos;
63 63
 }
64 64
 
65
+int Sphere::getIntersectionParam(const Ray &ray, float *param1, float *param2) {
66
+	int numerg;
67
+	// ax^2 + bx + c = 0
68
+	segl::Punkt3D ehv(1.0f, 1.0f, 1.0f);
69
+	float a = ray.dir*ehv;
70
+	float b = 2*(ray.pos*ehv + pos*ehv);
71
+	float c = ray.pos*ray.pos + pos*pos - 2*(ray.pos*pos) - radius*radius;
72
+	
73
+	float p = b/a;
74
+	float q = c/a;
75
+	
76
+	if((p*p)/4.0f<q) {
77
+		numerg = 0;
78
+	} else if((p*p)/4.0f==q) {
79
+		numerg = 1;
80
+		if(param1!=0) {
81
+			*param1 = -p/2.0f;
82
+		}
83
+	} else {
84
+		numerg = 2;
85
+		if(param1!=0 || param2!=0) {
86
+			if(param1!=0) {
87
+				*param1 = -p/2.0f + sqrt(((p*p)/4.0f)-q);
88
+			}
89
+			if(param2!=0) {
90
+				*param2 = -p/2.0f - sqrt(((p*p)/4.0f)-q);
91
+			}
92
+		}
93
+	}
94
+	
95
+	return numerg;	
96
+}
97
+
98
+int Sphere::getIntersectionPoints(const Ray &ray, segl::Punkt3D *a, segl::Punkt3D *b) {
99
+	float pa, pb;
100
+	int numerg = getIntersectionParam(ray, &pa, &pb);
101
+	if(a)
102
+		*a = ray.get(pa);
103
+	
104
+	if(b)
105
+		*b = ray.get(pb);
106
+	
107
+	return numerg;
108
+}
109
+
65 110
 Ray::Ray() {
66 111
 	dir.set(0.0f, 1.0f, 0.0f);
67 112
 }
@@ -139,14 +184,6 @@ float Ray::dist(Punkt3D p) const {
139 184
 	return abs(p - get( getParam(p) ));
140 185
 }
141 186
 
142
-int getIntersectionParam(const Ray &ray, float *param1, float *param2) {
143
-
144
-}
145
-
146
-int getIntersectionParam(const Ray &ray, segl::Punkt3D *a, segl::Punkt3D *b) {
147
-
148
-}
149
-
150 187
 float Ray::getParam(Punkt3D p, bool onray) const {
151 188
 	if(onray) {
152 189
 		if(!onRay(p))

+ 3
- 3
geotypes.h View File

@@ -47,6 +47,9 @@ class Sphere {
47 47
 		bool collision(const Box & b) const;
48 48
 		bool collision(const Plane &p) const;
49 49
 		
50
+		int getIntersectionParam(const Ray &ray, float *param1, float *param2);
51
+		int getIntersectionPoints(const Ray &ray, segl::Punkt3D *a, segl::Punkt3D *b);
52
+		
50 53
 		bool inSphere(Punkt3D p) const;
51 54
 		Punkt3D getPos() const;
52 55
 };
@@ -66,9 +69,6 @@ class Ray {
66 69
 		float dist(Punkt3D p) const;
67 70
 		float getParam(Punkt3D p, bool onray=false) const;
68 71
 		
69
-		int getIntersectionParam(const Ray &ray, float *param1, float *param2);
70
-		int getIntersectionParam(const Ray &ray, segl::Punkt3D *a, segl::Punkt3D *b);
71
-		
72 72
 		bool collision(const Sphere &s) const;
73 73
 		bool collision(const Ray &r) const;
74 74
 		bool collision(const Box & b) const;

Loading…
Cancel
Save