c_cpp http://www.gamasutra.com/view/feature/3383/simple_intersection_tests_for_games.php

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp http://www.gamasutra.com/view/feature/3383/simple_intersection_tests_for_games.php相关的知识,希望对你有一定的参考价值。

#include "vector.h"

class PLANE
{
public:

  VECTOR N; 
  //unit normal
  
  SCALAR D; 
  //distance from the plane to the origin
  
  //from a normal and a point
  PLANE( const VECTOR& p0, const VECTOR& n ):	
    N(n), 
    D(-N.dot(p0))
  {}
  
  //from 3 points
  PLANE( const VECTOR& p0, const VECTOR& p1, const VECTOR& p2 ):
    N((p1-p0).cross(p2-p0).unit()),
    D(-N.dot(p0))
  {}
  
  //signed distance from the plane topoint 'p' along 
  //the unit normal
  const SCALAR distanceToPoint( const VECTOR& p ) const
  {
    return N.dot(p) + D;
  }
};

const bool SpherePlaneSweep
(
  const SCALAR	r,	//sphere radius
  const VECTOR&	C0,	//previous position of sphere
  const VECTOR&	C1,	//current position of sphere
  const PLANE&	plane,	//the plane
  VECTOR&	 Ci,	//position of sphere when it first touched the plane
  SCALAR&	 u	//normalized time of collision
)
{
  const SCALAR d0 = plane.distanceToPoint( C0 );
  const SCALAR d1 = plane.distanceToPoint( C1 );
  
  //check if it was touching on previous frame
  if( fabs(d0) <= r )
  {
    Ci = C0;
    u = 0;
    return true;
  }
  
  //check if the sphere penetrated during this frame
  if( d0>r && d1<r )
  {
    u = (d0-r)/(d0-d1);	 //normalized time
    Ci = (1-u)*C0 + u*C1;	//point of first contact
    return true;
  }
  return false;
}

以上是关于c_cpp http://www.gamasutra.com/view/feature/3383/simple_intersection_tests_for_games.php的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 127.单词阶梯

c_cpp MOFSET

c_cpp MOFSET

c_cpp 31.下一个排列

c_cpp string→char *

c_cpp 54.螺旋矩阵