~cpp 쿠쿠.. 열심이구나.. ^^a.. DX 공부하면 나줌 갈켜다오.. 헤헤.. 나두 지금 하구 있는거.. 시간되면 여기다(위키..) 쓸께.. 너네두 곧 필요해질 내용이 있을테니.. 근데 지금은 내코가 좀 석자라서.. 시간에 쫓기는 관계로.. 언젠가.. 꼭.. ^^;; - 해성
~cpp
추천 도서: Inside DirectX (필요로 한다면 말하시오. 전자책으로 있어요. --;;)
Tricks of the Windows Game Programming Gurus : Fundamentals of 2D and 3D Game Programming. (DirectX, DirectMusic, 3D sound)
~cpp 다들 알고 계시져? --;; 하하..단위 벡터- 세개의 주요 축들의 방향을 나타내기 위한 벡터.
~cpp
struct point3
{
union // use union - 메모리를 공유하는 성분들에 이름을 지정하는데 사용.
{ // y변수와 v[1]변수는 같은 메모리 조각을 나타낸다.
struct
{
float x, y, z; // 구조체의 이름이 정의되지 않았지 때문에 x,y,z 성분을 원자 단위로 사용.
};
float v[3];
}
point3 () {}
point3 (float X, float Y, float Z) : x(X), y(Y), z(Z) {} // 초기화 목록을 사용. (compiler가 작업을 더 잘 수행할 수 있도록 해준다더군.)
// ...
};
~cpp
struct point3
{
...
point3 operator +(point3 const &a, point3 const &b);
point3 operator -(point3 const &a, point3 const &b);
point3 operator *(point3 const &a, float const &b);
point3 operator *(float const &a, point3 const &b);
point3 operator /(point3 const &a, float const &b);
...
};
// 좌표 할당
inline void point3::Assign (float X, float Y, float Z)
{
x = X; y = Y; z = Z;
}
// vector 값
inline float point3::Mag () const
{
return (flaot)sqrt (x*x + y*y + z*z);
}
// 거리의 제곱 값
inline float point3::MagSquared () const
{
return (x*x + y*y + z*z);
}
// Normalize
inline void point3::Normailze ()
{
float foo = 1/Mag ();
x *= foo; y *= foo; z *= foo;
}
~cpp
// point3의 등가 연산자
#define EPSILON 0.001
inline bool operator ==(point3 const &a, point3 const &b)
{
if (fabs (a.x - b.x) < EPSILON)
{
if (fabs (a.y - b.y) < EPSILON)
{
if (fabs (a.z - b.z) < EPSILON)
{
return true;
}
}
}
return false;
}
~cpp
// 내적
inline float operator *(point3 const &a, point3 const &b)
{
return a.x*b.x + a.y*b.y + a.z*b.z;
}
// 외적
inline point3 operator ^(point3 const &a, point3 const &b)
{
return point3 ( (a.y*b.z - a.z*b.y), (a.z*b.x - a.x*b.z), (a.x*b.y - a.y*b.x) );
}
~cpp
template <class type>
struct polygon
{
int nElem; // number of elements in the polygon
int maxElem;
type *pList;
polygon()
{
nElem = 0;
maxElem = 0;
pList = NULL;
}
polygon( int maxSize )
{
maxElem = maxSize;
pList = new type[maxSize];
}
polygon( const polygon &in )
{
CloneData( in );
}
~polygon()
{
DestroyData();
}
void CloneData( const polygon &in )
{
if( !in.pList )
return;
pList = new type[in.maxElem];
maxElem = in.maxElem;
nElem = in.nElem;
for( int i=0; i<in.nElem; i++ )
{
pList[i] = in.pList[i];
}
}
void DestroyData( )
{
delete[] pList;
pList = NULL;
}
polygon& operator=( const polygon<type> &in )
{
if( &in != this )
{
DestroyData();
CloneData( in );
}
return *this;
}
};
~cpp ax + by + cz + d = 0삼원쌍 <a, b, c>는 면의 법선(법선: 그 면내의 모든 점들에 대해 수직인 벡터), d는 면에서 원점까지의 거리
~cpp
struct plane3 {
point3 n; // Normal of the plane
float d; // Distance along the normal to the origin
plane3( float nX, float nY, float nZ, float D) : n( nX, nY, nZ ), d( D )
{
// All done.
}
plane3( const point3& N, float D) : n( N ), d( D )
{
// All done.
}
// Construct a plane from three 3-D points
plane3( const point3& a, const point3& b, const point3& c);
// Construct a plane from a normal direction and a point on the plane
plane3( const point3& norm, const point3& loc);
// Construct a plane from a polygon
plane3( const polygon<point3>& poly );
plane3()
{
// Do nothing
}
// Flip the orientation of the plane
void Flip();
};
~cpp
Rx (r) = | 1 0 0 |
| 0 cos(r) sin(r) |
| 0 -sin(r) cos(r) |
Ry (r) = | cos(r) 0 -sin(r)|
| 0 1 0 |
| 0 sin(r) cos(r) |
Rz (r) = | cos(r) sin(r) 0 |
| -sin(r) cos(r) 0 |
| 0 0 1 |
ex ) v0 = <2, 0, 0>을 z에 관해 시계 방향으로 45도 회전
v1 = Rz(45)v0
v1이 v0를 z에 관해 시계 방향으로 45도 회전시킨 지점이다.
// 문제점
// x, y, z축으로 회전시킨 행렬들을 하나로 결합하기위한 표준 방법이 없다. // ??
일정한 회전을 주기위해 해야할 일은 단지 필요한 이련의 변환을 알아내고 ~cpp - 특정한 점 p에 존재하는 개체를 z축을 따라 회전시키려는 경우 - v = vT(-p) // 원점으로 위치이동 v = vRz(파이/2) // 파이.. 뭔지는 알겠지.. ㅡㅡ; v = vT(p) // 다시 전 p로 이동.but, 각점들 마다 이런 연산을 수행하려면 너무 느리다.
~cpp v` = (vA)B => v` = v(AB) // 오!~
~cpp | xx(1-cos(r)) + cos (r) yx(1-cos(r)) + z sin(r) xz(1-cos(r)) - y sin(r) 0 | | xy(1-cos(r)) + z sin(r) yy(1-cos(r)) + cos(r) yz(1-cos(r)) - x sin(r) 0 | | xz(1-cos(r)) + y sin(r) yz(1-cos(r)) + x sin(r) zz(1-cos(r)) - cos(r) 0 | | 0 0 0 1 |
~cpp
--------------
| obstacle 2 | | goal |
--------------
--------------
| obstacle 1 |
--------------
|creature.|
우리는 이 서술을 하나의 등식으로 바꿀 수 있다. 최초 우리의 방향은
~cpp
// 실제 사용 예.
bool Process()
{
point3 goalVec = g_goalLoc - m_loc;
if( goalVec.Mag() < g_creatureSpeed )
return false; // goal까지 도착했다.
point3 dirVec = goalVec / goalVec.Mag();
float k = .1f;
// 각각의 obstacle과 검사
for( int i=0; i<g_obstacles.size(); i++ )
{
// creature에서 obstacle까지의 vector
point3 obstacleVec = m_loc - g_obstacles[i].m_loc;
// obstacle과 creature사이의 실제 거리
float dist = obstacleVec.Mag() - g_obstacles[i].m_rad - m_rad;
// this is the vector pointing away from the obstacle ??
obstacleVec.Normalize();
dirVec += obstacleVec * ( k / (dist * dist) ); // creature를 평행이동 시킬 행렬을 얻는다. 모든 obstacle을 검사 하면서...
}
dirVec.Normalize();
m_loc += g_creatureSpeed * dirVec;
return true; // ok. obstacle을 피했다.
}
실행 파일: http://zp.cse.cau.ac.kr/~erunc0/study/d3d/potentialFunc.exe