예제 #1
0
//==============================================================================
Bool test(const Aabb& aabb, const LineSegment& ls)
{
	F32 maxS = MIN_F32;
	F32 minT = MAX_F32;

	// do tests against three sets of planes
	for(U i = 0; i < 3; ++i)
	{
		// segment is parallel to plane
		if(isZero(ls.getDirection()[i]))
		{
			// segment passes by box
			if(ls.getOrigin()[i] < aabb.getMin()[i]
				|| ls.getOrigin()[i] > aabb.getMax()[i])
			{
				return false;
			}
		}
		else
		{
			// compute intersection parameters and sort
			F32 s =
				(aabb.getMin()[i] - ls.getOrigin()[i]) / ls.getDirection()[i];
			F32 t =
				(aabb.getMax()[i] - ls.getOrigin()[i]) / ls.getDirection()[i];
			if(s > t)
			{
				F32 temp = s;
				s = t;
				t = temp;
			}

			// adjust min and max values
			if(s > maxS)
			{
				maxS = s;
			}
			if(t < minT)
			{
				minT = t;
			}

			// check for intersection failure
			if(minT < 0.0 || maxS > 1.0 || maxS > minT)
			{
				return false;
			}
		}
	}

	// done, have intersection
	return true;
}
예제 #2
0
bool intersects(const LineSegment<type>& l1, const LineSegment<type>& l2) {
  if(l1.isDegenerate() || l2.isDegenerate()) {
    return false;
  }
  type t = -(dot(l2.getNormal(), l1.getPoint1())+l2.getOffset())/dot(l2.getNormal(), l1.getDirection());
  if(t < 0 || t > 1) {
    return false;
  }
  t = -(dot(l1.getNormal(), l2.getPoint1())+l1.getOffset())/dot(l1.getNormal(), l2.getDirection());
  if(t < 0 || t > 1) {
    return false;
  }
  return true;
}
예제 #3
0
bool intersects(typename LineSegment<type>::Point& i, const LineSegment<type>& l1, const LineSegment<type>& l2) {
  if(l1.m_degenerate || l2.m_degenerate) {
    return false;
  }
  type t = -(dot(l2.getNormal(), l1.getPoint1)+l2.getOffset())/dot(l2.getNormal(), l1.getDirection());
  if(t < 0 || t > 1) {
    return false;
  }
  t = -(dot(l1.getNormal(), l2.getPoint1())+l1.getOffset())/dot(l1.getNormal(), l2.getDirection());
  if(t < 0 || t > 1) {
    return false;
  }
  i = lerp(l2.getPoint1(), l2.getPoint2(), t);      
  return true;
}
예제 #4
0
//==============================================================================
static Bool test(const LineSegment& ls, const Obb& obb)
{
	F32 maxS = MIN_F32;
	F32 minT = MAX_F32;

	// compute difference vector
	Vec4 diff = obb.getCenter() - ls.getOrigin();

	// for each axis do
	for(U i = 0; i < 3; ++i)
	{
		// get axis i
		Vec4 axis = obb.getRotation().getColumn(i).xyz0();

		// project relative vector onto axis
		F32 e = axis.dot(diff);
		F32 f = ls.getDirection().dot(axis);

		// ray is parallel to plane
		if(isZero(f))
		{
			// ray passes by box
			if(-e - obb.getExtend()[i] > 0.0 || -e + obb.getExtend()[i] > 0.0)
			{
				return false;
			}
			continue;
		}

		F32 s = (e - obb.getExtend()[i]) / f;
		F32 t = (e + obb.getExtend()[i]) / f;

		// fix order
		if(s > t)
		{
			F32 temp = s;
			s = t;
			t = temp;
		}

		// adjust min and max values
		if(s > maxS)
		{
			maxS = s;
		}
		if(t < minT)
		{
			minT = t;
		}

		// check for intersection failure
		if(minT < 0.0 || maxS > 1.0 || maxS > minT)
		{
			return false;
		}
	}

	// done, have intersection
	return true;
}
예제 #5
0
//==============================================================================
void CollisionDebugDrawer::visit(const LineSegment& ls)
{
	m_dbg->setModelMatrix(Mat4::getIdentity());
	m_dbg->begin(GL_LINES);
	m_dbg->pushBackVertex(ls.getOrigin().xyz());
	m_dbg->pushBackVertex((ls.getOrigin() + ls.getDirection()).xyz());
	m_dbg->end();
}
예제 #6
0
//==============================================================================
Bool test(const LineSegment& ls, const Sphere& s)
{
	const Vec4& v = ls.getDirection();
	Vec4 w0 = s.getCenter() - ls.getOrigin();
	F32 w0dv = w0.dot(v);
	F32 rsq = s.getRadius() * s.getRadius();

	if(w0dv < 0.0) // if the ang is >90
	{
		return w0.getLengthSquared() <= rsq;
	}

	Vec4 w1 = w0 - v; // aka center - P1, where P1 = seg.origin + seg.dir
	F32 w1dv = w1.dot(v);

	if(w1dv > 0.0) // if the ang is <90
	{
		return w1.getLengthSquared() <= rsq;
	}

	// the big parenthesis is the projection of w0 to v
	Vec4 tmp = w0 - (v * (w0.dot(v) / v.getLengthSquared()));
	return tmp.getLengthSquared() <= rsq;
}