]>
Dogcows Code - chaz/yoink/blob - src/Moof/Sphere.hh
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
12 #ifndef _MOOF_SPHERE_HH_
13 #define _MOOF_SPHERE_HH_
15 #include <Moof/Contact.hh>
16 #include <Moof/Cullable.hh>
17 #include <Moof/Drawable.hh>
18 #include <Moof/Frustum.hh>
19 #include <Moof/Math.hh>
20 #include <Moof/OpenGL.hh>
21 #include <Moof/Shape.hh>
23 // TODO this needs work
34 struct Sphere
: public Cullable
, public Drawable
, public Shape
<D
>
36 typedef cml::vector
< Scalar
, cml::fixed
<D
> > Vector
;
44 Sphere(const Vector
& p
, Scalar r
) :
49 void init(const Vector
& p
, Scalar r
)
55 void init(const Vector
& p
, const Vector
& o
)
58 radius
= (o
- p
).length();
61 //void encloseVertices(const Vector vertices[], unsigned count);
63 //void draw(Scalar alpha = 0.0) const;
64 //bool isVisible(const Frustum& frustum) const;
66 void encloseVertices(const Vector vertices
[], unsigned count
)
71 void draw(Scalar alpha
= 0.0) const;
73 bool isVisible(const Frustum
& frustum
) const
79 bool intersect(const Sphere
<D
>& sphere
, Contact
<D
>& hit
) const
81 Vector n
= sphere
.point
- point
;
82 Scalar distance
= n
.length();
83 Scalar limit
= radius
+ sphere
.radius
;
85 if (distance
> limit
) return false;
87 hit
.normal
= n
.normalize();
88 hit
.distance
= limit
- distance
;
89 hit
.point
= hit
.normal
* radius
;
94 bool intersect(const Vector
& point2
, Contact
<D
>& hit
) const
96 Vector n
= point2
- point
;
97 Scalar distance
= n
.length();
99 if (distance
> radius
) return false;
101 hit
.normal
= n
.normalize();
102 hit
.distance
= radius
- distance
;
108 // a ray inside the sphere will not intersect on its way out
109 bool intersect(const Ray
<D
>& ray
, typename Ray
<D
>::Contact
& hit
) const
111 Vector b
= point
- ray
.point
;
112 Scalar z
= cml::dot(b
, ray
.direction
);
114 // check if the ball is behind the ray
115 if (z
< SCALAR(0.0)) return false;
117 Scalar d2
= cml::dot(b
, b
) - z
*z
;
118 Scalar r2
= radius
* radius
;
120 // check for an intersection
121 if (d2
> r2
) return false;
123 hit
.distance
= z
- std::sqrt(r2
- d2
);
124 if (hit
.distance
< SCALAR(0.0)) return false;
127 ray
.solve(surfacePoint
, hit
.distance
);
128 hit
.normal
= surfacePoint
- point
;
135 inline bool Sphere
<3>::isVisible(const Frustum
& frustum
) const
137 return frustum
.contains(*this);
141 inline void Sphere
<2>::draw(Scalar alpha
) const
143 GLUquadricObj
* sphereObj
= gluNewQuadric();
144 gluQuadricDrawStyle(sphereObj
, GLU_LINE
);
148 glTranslate(promote(point
));
149 gluSphere(sphereObj
, GLdouble(radius
), 16, 16);
153 gluDeleteQuadric(sphereObj
);
157 inline void Sphere
<3>::draw(Scalar alpha
) const
159 GLUquadricObj
* sphereObj
= gluNewQuadric();
160 gluQuadricDrawStyle(sphereObj
, GLU_LINE
);
165 gluSphere(sphereObj
, GLdouble(radius
), 16, 16);
169 gluDeleteQuadric(sphereObj
);
173 inline bool checkCollision(const Sphere
<D
>& a
, const Sphere
<D
>& b
)
175 Scalar d
= (a
.point
- b
.point
).length();
176 return d
< (a
.radius
+ b
.radius
);
180 typedef Sphere
<2> Sphere2
;
181 typedef Sphere2 Circle
;
182 typedef Sphere
<3> Sphere3
;
187 #endif // _MOOF_SPHERE_HH_
This page took 0.040104 seconds and 4 git commands to generate.