00001
00002
00003
00004
00005 #include "Sphere.h"
00006
00007 #include "AABB.h"
00008
00009 #include <stdio.h>
00010
00011
00012 namespace pge {
00013
00014
00015
00016
00017
00018
00019
00020 Sphere::Sphere(const Vector3f ¢er, float radius) {
00021 m_center = center;
00022 m_radius = radius;
00023 }
00024
00025
00026
00027
00028
00029
00030
00031 Sphere::Sphere(AABB *aabb) {
00032 float middleX;
00033 float middleY;
00034 float middleZ;
00035 float dx;
00036 float dy;
00037 float dz;
00038
00039
00040 dx = aabb->m_maxX - aabb->m_minX;
00041 dy = aabb->m_maxY - aabb->m_minY;
00042 dz = aabb->m_maxZ - aabb->m_minZ;
00043
00044
00045 middleX = (dx / 2.0f) + aabb->m_minX;
00046 middleY = (dy / 2.0f) + aabb->m_minY;
00047 middleZ = (dz / 2.0f) + aabb->m_minZ;
00048
00049
00050 m_center = Vector3f(middleX, middleY, middleZ);
00051
00052
00053
00054
00055
00056
00057
00058 m_radius = dx;
00059 if(dy > m_radius) {
00060 m_radius = dy;
00061 }
00062 if(dz > m_radius) {
00063 m_radius = dz;
00064 }
00065 }
00066
00067
00068
00069
00070
00071
00072
00073 Sphere::~Sphere(void) {
00074 }
00075
00076
00077
00078
00079
00080
00081
00082 float Sphere::getDistance(const Sphere &sphere) {
00083 Vector3f v;
00084 v = m_center.subtract(sphere.m_center);
00085 return v.magnitude();
00086 }
00087
00088
00089
00090
00091
00092
00093
00094 float Sphere::getSquaredDistance(const Sphere &sphere) {
00095 Vector3f v;
00096 v = m_center.subtract(sphere.m_center);
00097 return v.sqrMagnitude();
00098 }
00099
00100
00101
00102
00103
00104
00105
00106 float Sphere::getDistance(const Vector3f &point) {
00107 Vector3f v;
00108 v = m_center.subtract(point);
00109 return v.magnitude();
00110 }
00111
00112
00113
00114
00115
00116
00117
00118 float Sphere::getSquaredDistance(const Vector3f &point) {
00119 Vector3f v;
00120 v = m_center.subtract(point);
00121 return v.sqrMagnitude();
00122 }
00123
00124
00125
00126
00127
00128
00129
00130 bool Sphere::isIntersecting(const Sphere &sphere) {
00131 float d;
00132
00133
00134 d = getSquaredDistance(sphere);
00135 if((m_radius * m_radius) >= (d - (sphere.m_radius * sphere.m_radius))) {
00136 return true;
00137 }
00138 return false;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147 bool Sphere::isPointInside(const Vector3f &point) {
00148 float d;
00149
00150
00151 d = getSquaredDistance(point);
00152 if((m_radius * m_radius) >= d) {
00153 return true;
00154 }
00155 return false;
00156 }
00157 };