Sphere-Ray-Intersection (untested)

This commit is contained in:
seba 2009-09-23 00:17:45 +02:00
parent e8de930395
commit 71be63329c
2 changed files with 48 additions and 11 deletions

View File

@ -62,6 +62,51 @@ Punkt3D Sphere::getPos() const {
return pos; return pos;
} }
int Sphere::getIntersectionParam(const Ray &ray, float *param1, float *param2) {
int numerg;
// ax^2 + bx + c = 0
segl::Punkt3D ehv(1.0f, 1.0f, 1.0f);
float a = ray.dir*ehv;
float b = 2*(ray.pos*ehv + pos*ehv);
float c = ray.pos*ray.pos + pos*pos - 2*(ray.pos*pos) - radius*radius;
float p = b/a;
float q = c/a;
if((p*p)/4.0f<q) {
numerg = 0;
} else if((p*p)/4.0f==q) {
numerg = 1;
if(param1!=0) {
*param1 = -p/2.0f;
}
} else {
numerg = 2;
if(param1!=0 || param2!=0) {
if(param1!=0) {
*param1 = -p/2.0f + sqrt(((p*p)/4.0f)-q);
}
if(param2!=0) {
*param2 = -p/2.0f - sqrt(((p*p)/4.0f)-q);
}
}
}
return numerg;
}
int Sphere::getIntersectionPoints(const Ray &ray, segl::Punkt3D *a, segl::Punkt3D *b) {
float pa, pb;
int numerg = getIntersectionParam(ray, &pa, &pb);
if(a)
*a = ray.get(pa);
if(b)
*b = ray.get(pb);
return numerg;
}
Ray::Ray() { Ray::Ray() {
dir.set(0.0f, 1.0f, 0.0f); dir.set(0.0f, 1.0f, 0.0f);
} }
@ -139,14 +184,6 @@ float Ray::dist(Punkt3D p) const {
return abs(p - get( getParam(p) )); return abs(p - get( getParam(p) ));
} }
int getIntersectionParam(const Ray &ray, float *param1, float *param2) {
}
int getIntersectionParam(const Ray &ray, segl::Punkt3D *a, segl::Punkt3D *b) {
}
float Ray::getParam(Punkt3D p, bool onray) const { float Ray::getParam(Punkt3D p, bool onray) const {
if(onray) { if(onray) {
if(!onRay(p)) if(!onRay(p))

View File

@ -47,6 +47,9 @@ class Sphere {
bool collision(const Box & b) const; bool collision(const Box & b) const;
bool collision(const Plane &p) const; bool collision(const Plane &p) const;
int getIntersectionParam(const Ray &ray, float *param1, float *param2);
int getIntersectionPoints(const Ray &ray, segl::Punkt3D *a, segl::Punkt3D *b);
bool inSphere(Punkt3D p) const; bool inSphere(Punkt3D p) const;
Punkt3D getPos() const; Punkt3D getPos() const;
}; };
@ -66,9 +69,6 @@ class Ray {
float dist(Punkt3D p) const; float dist(Punkt3D p) const;
float getParam(Punkt3D p, bool onray=false) const; float getParam(Punkt3D p, bool onray=false) const;
int getIntersectionParam(const Ray &ray, float *param1, float *param2);
int getIntersectionParam(const Ray &ray, segl::Punkt3D *a, segl::Punkt3D *b);
bool collision(const Sphere &s) const; bool collision(const Sphere &s) const;
bool collision(const Ray &r) const; bool collision(const Ray &r) const;
bool collision(const Box & b) const; bool collision(const Box & b) const;