2012-01-29 17:39:56 +00:00
|
|
|
#include "euclid.h"
|
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
#include <math.h>
|
2013-11-03 12:00:31 +00:00
|
|
|
#include "PackStream.h"
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2013-04-20 11:28:56 +00:00
|
|
|
double euclidGet2DAngle(double x, double y)
|
|
|
|
{
|
|
|
|
double nx, ny, d, ret;
|
|
|
|
|
|
|
|
if (x == 0.0)
|
|
|
|
{
|
|
|
|
if (y == 0.0)
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
2013-04-22 14:53:16 +00:00
|
|
|
else if (y < 0.0)
|
|
|
|
{
|
|
|
|
return 3.0 * M_PI_2;
|
|
|
|
}
|
2013-04-20 11:28:56 +00:00
|
|
|
else
|
|
|
|
{
|
2013-04-22 14:53:16 +00:00
|
|
|
return M_PI_2;
|
2013-04-20 11:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d = sqrt(x * x + y * y);
|
|
|
|
nx = x / d;
|
|
|
|
ny = y / d;
|
|
|
|
|
|
|
|
ret = asin(ny);
|
|
|
|
if (nx < 0.0)
|
|
|
|
{
|
|
|
|
ret = M_PI - ret;
|
|
|
|
}
|
2013-04-22 14:53:16 +00:00
|
|
|
return ret < 0.0 ? ret + 2.0 * M_PI : ret;
|
2013-04-20 11:28:56 +00:00
|
|
|
}
|
|
|
|
|
2013-01-30 17:00:26 +00:00
|
|
|
Vector3 euclidGetNormalFromTriangle(Vector3 center, Vector3 bottom, Vector3 right)
|
|
|
|
{
|
|
|
|
Vector3 dx = v3Sub(right, center);
|
|
|
|
Vector3 dz = v3Sub(bottom, center);
|
|
|
|
return v3Normalize(v3Cross(dz, dx));
|
|
|
|
}
|
|
|
|
|
|
|
|
double euclidGetDistance2D(double x1, double y1, double x2, double y2)
|
|
|
|
{
|
|
|
|
double dx = x2 - x1;
|
|
|
|
double dy = y2 - y1;
|
|
|
|
return sqrt(dx * dx + dy * dy);
|
|
|
|
}
|
|
|
|
|
|
|
|
int euclidRayIntersectSphere(Vector3 ray_point, Vector3 ray_direction, Vector3 sphere_center, double sphere_radius, Vector3* hit1, Vector3* hit2)
|
|
|
|
{
|
|
|
|
Vector3 ray_direction_sphere;
|
|
|
|
double a, b, c, d;
|
|
|
|
|
|
|
|
ray_direction_sphere = v3Sub(ray_point, sphere_center);
|
|
|
|
|
|
|
|
a = ray_direction.x * ray_direction.x + ray_direction.y * ray_direction.y + ray_direction.z * ray_direction.z;
|
|
|
|
b = 2 * (ray_direction.x * ray_direction_sphere.x + ray_direction.y * ray_direction_sphere.y + ray_direction.z * ray_direction_sphere.z);
|
|
|
|
c = ray_direction_sphere.x * ray_direction_sphere.x + ray_direction_sphere.y * ray_direction_sphere.y + ray_direction_sphere.z * ray_direction_sphere.z - sphere_radius * sphere_radius;
|
|
|
|
d = b * b - 4 * a * c;
|
|
|
|
|
|
|
|
if (d < 0.0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (d > 0.0)
|
|
|
|
{
|
|
|
|
if (hit1 && hit2)
|
|
|
|
{
|
|
|
|
*hit1 = v3Add(ray_point, v3Scale(ray_direction, (-b - sqrt(d)) / (2 * a)));
|
|
|
|
*hit2 = v3Add(ray_point, v3Scale(ray_direction, (-b + sqrt(d)) / (2 * a)));
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (hit1)
|
|
|
|
{
|
|
|
|
*hit1 = v3Add(ray_point, v3Scale(ray_direction, -b / (2 * a)));
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|