]>
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_
17 * A round shape like a circle or sphere.
18 * TODO: This class needs some work.
21 #include <moof/contact.hh>
22 #include <moof/cullable.hh>
23 #include <moof/drawable.hh>
24 #include <moof/frustum.hh>
25 #include <moof/math.hh>
26 #include <moof/opengl.hh>
27 #include <moof/shape.hh>
37 struct sphere
: public cullable
, public drawable
, public shape
<D
>
39 typedef moof::vector
< scalar
, fixed
<D
> > vector
;
47 sphere(const vector
& p
, scalar r
) :
52 void init(const vector
& p
, scalar r
)
58 void init(const vector
& p
, const vector
& o
)
61 radius
= (o
- p
).length();
64 //void enclose_vertices(const vector vertices[], unsigned count);
66 //void draw(scalar alpha = 0.0) const;
67 //bool is_visible(const frustum& frustum) const;
69 void enclose_vertices(const vector vertices
[], unsigned count
)
74 void draw(scalar alpha
= 0.0) const;
76 bool is_visible(const frustum
& frustum
) const
82 bool intersect(const sphere
<D
>& sphere
, contact
<D
>& hit
) const
84 vector n
= sphere
.point
- point
;
85 scalar distance
= n
.length();
86 scalar limit
= radius
+ sphere
.radius
;
88 if (distance
> limit
) return false;
90 hit
.normal
= n
.normalize();
91 hit
.distance
= limit
- distance
;
92 hit
.point
= hit
.normal
* radius
;
97 bool intersect(const vector
& point2
, contact
<D
>& hit
) const
99 vector n
= point2
- point
;
100 scalar distance
= n
.length();
102 if (distance
> radius
) return false;
104 hit
.normal
= n
.normalize();
105 hit
.distance
= radius
- distance
;
111 // a ray inside the sphere will not intersect on its way out
112 bool intersect(const ray
<D
>& ray
,
113 typename
moof::ray
<D
>::contact
& hit
) const
115 vector b
= point
- ray
.point
;
116 scalar z
= dot(b
, ray
.direction
);
118 // check if the ball is behind the ray
119 if (z
< SCALAR(0.0)) return false;
121 scalar d2
= dot(b
, b
) - z
*z
;
122 scalar r2
= radius
* radius
;
124 // check for an intersection
125 if (d2
> r2
) return false;
127 hit
.distance
= z
- std::sqrt(r2
- d2
);
128 if (hit
.distance
< SCALAR(0.0)) return false;
131 ray
.solve(surfacePoint
, hit
.distance
);
132 hit
.normal
= surfacePoint
- point
;
139 inline bool sphere
<3>::is_visible(const frustum
& frustum
) const
141 return frustum
.contains(*this);
145 inline void sphere
<2>::draw(scalar alpha
) const
147 GLUquadricObj
* sphereObj
= gluNewQuadric();
148 gluQuadricDrawStyle(sphereObj
, GLU_LINE
);
152 glTranslate(promote(point
));
153 gluSphere(sphereObj
, GLdouble(radius
), 16, 16);
157 gluDeleteQuadric(sphereObj
);
161 inline void sphere
<3>::draw(scalar alpha
) const
163 GLUquadricObj
* sphereObj
= gluNewQuadric();
164 gluQuadricDrawStyle(sphereObj
, GLU_LINE
);
169 gluSphere(sphereObj
, GLdouble(radius
), 16, 16);
173 gluDeleteQuadric(sphereObj
);
177 inline bool checkCollision(const sphere
<D
>& a
, const sphere
<D
>& b
)
179 scalar d
= (a
.point
- b
.point
).length();
180 return d
< (a
.radius
+ b
.radius
);
184 typedef sphere
<2> sphere2
;
185 typedef sphere2 circle
;
186 typedef sphere
<3> sphere3
;
191 #endif // _MOOF_SPHERE_HH_
This page took 0.042782 seconds and 4 git commands to generate.