2006-12-10

This commit is contained in:
Anonymous Maarten committed 2015-12-03 01:37:02 +01:00
commit 78c27f03c8
153 files changed
+24678

No files matched your search

+281
View File
@@ -0,0 +1,281 @@
/* ColDet - C++ 3D Collision Detection Library
* Copyright (C) 2000 Amir Geva
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Any comments, questions and bug reports send to:
* photon@photoneffect.com
*
* Or visit the home page: http://photoneffect.com/coldet/
*/
#include "sysdep.h"
#include "box.h"
#include "mytritri.h"
__CD__BEGIN
////////////////////////////////////////////////////
// code from here is used in detection process
int BoxTreeInnerNode::getTrianglesNumber()
{
return m_Boxes.size();
}
BoxedTriangle* BoxTreeInnerNode::getTriangle(int which)
{
if (which<0 || which>=getTrianglesNumber()) return NULL;
return m_Boxes[which];
}
RotationState::RotationState(const Matrix3D& transform)
: t(transform)
{
N[0]=Vector3D(t._11,t._12,t._13);
N[1]=Vector3D(t._21,t._22,t._23);
N[2]=Vector3D(t._31,t._32,t._33);
}
inline float DotWithCol(const Vector3D& v, const Matrix3& m, int col)
{
return v.x*m(0,col) + v.y*m(1,col) + v.z*m(2,col);
}
bool Box::intersect(const Vector3D& O, float radius)
{
Vector3D mx=m_Pos+m_Size;
float dist=0.0f;
for(int i=0;i<3;i++)
{
if (O[i] < m_Pos[i])
{
float d=O[i]-m_Pos[i];
dist+=d*d;
}
else
if (O[i] > mx[i])
{
float d=O[i]-mx[i];
dist+=d*d;
}
}
return (dist <= (radius*radius));
}
bool Box::intersect(const Vector3D& O, const Vector3D& D,
float segmax)
{
if (segmax>3e30f) return intersect(O,D); // infinite ray
Vector3D abs_segdir, abs_diff, abs_cross;
Vector3D segdir=0.5f*segmax*D;
Vector3D seg_center=O+segdir;
Vector3D diff=seg_center - getCenter();
int i;
for(i=0;i<3;i++)
{
abs_segdir[i]=flabs(segdir[i]);
abs_diff[i]=flabs(diff[i]);
float f=getSize()[i] + abs_segdir[i];
if (abs_diff[i] > f) return false;
}
Vector3D cross=CrossProduct(segdir,diff);
int idx[] = {0,1,2,0,1};
for(i=0;i<3;i++)
{
int i1=idx[i+1];
int i2=idx[i+2];
abs_cross[i] = flabs(cross[i]);
float f = getSize()[i1]*abs_segdir[i2] + getSize()[i2]*abs_segdir[i1];
if ( abs_cross[i] > f ) return false;
}
return true;
}
bool Box::intersect(const Vector3D& O, const Vector3D& D)
{
Vector3D abs_segdir, abs_cross;
float f;
Vector3D diff = O - getCenter();
for(int i=0;i<3;i++)
{
abs_segdir[i] = flabs(D[i]);
if ( flabs(diff[i])>m_Size[i] && diff[i]*D[i]>=0.0f )
return false;
}
Vector3D cross = CrossProduct(D,diff);
abs_cross[0] = flabs(cross[0]);
f = m_Size[1]*abs_segdir[2] + m_Size[2]*abs_segdir[1];
if ( abs_cross[0] > f )
return false;
abs_cross[1] = flabs(cross[1]);
f = m_Size[0]*abs_segdir[2] + m_Size[2]*abs_segdir[0];
if ( abs_cross[1] > f )
return false;
abs_cross[2] = flabs(cross[2]);
f = m_Size[0]*abs_segdir[1] + m_Size[1]*abs_segdir[0];
if ( abs_cross[2] > f )
return false;
return true;
}
bool Box::intersect(const Box& b, RotationState& rs)
{
const Vector3D bCenter=Transform(b.getCenter(),rs.t);
Vector3D EA=0.5f*getSize();
Vector3D EB=0.5f*b.getSize();
Vector3D distance=bCenter-getCenter();
Matrix3 C,abs_C;
float R0,R1,R,R01;
int i;
for(i=0;i<3;i++)
{
C(i,0)=rs.N[0][i];
C(i,1)=rs.N[1][i];
C(i,2)=rs.N[2][i];
abs_C(i,0)=flabs(C(i,0));
abs_C(i,1)=flabs(C(i,1));
abs_C(i,2)=flabs(C(i,2));
R=flabs(distance[i]);
R1=EB*abs_C.baseRow(i);
R01=EA[i]+R1;
if (R>R01) return false;
}
for(i=0;i<3;i++)
{
R=flabs(rs.N[i]*distance);
R0=DotWithCol(EA,abs_C,i);
R01=R0+EB[i];
if (R>R01) return false;
}
R=flabs(distance.z*C(1,0) - distance.y*C(2,0));
R0=EA.y*abs_C(2,0) + EA.z*abs_C(1,0);
R1=EB.y*abs_C(0,2) + EB.z*abs_C(0,1);
R01=R0+R1;
if (R>R01) return false;
R=flabs(distance.z*C(1,1) - distance.y*C(2,1));
R0=EA.y*abs_C(2,1) + EA.z*abs_C(1,1);
R1=EB.x*abs_C(0,2) + EB.z*abs_C(0,0);
R01=R0+R1;
if (R>R01) return false;
R=flabs(distance.z*C(1,2) - distance.y*C(2,2));
R0=EA.y*abs_C(2,2) + EA.z*abs_C(1,2);
R1=EB.x*abs_C(0,1) + EB.y*abs_C(0,0);
R01=R0+R1;
if (R>R01) return false;
R=flabs(distance.x*C(2,0) - distance.z*C(0,0));
R0=EA.x*abs_C(2,0) + EA.z*abs_C(0,0);
R1=EB.y*abs_C(1,2) + EB.z*abs_C(1,1);
R01=R0+R1;
if (R>R01) return false;
R=flabs(distance.x*C(2,1) - distance.z*C(0,1));
R0=EA.x*abs_C(2,1) + EA.z*abs_C(0,1);
R1=EB.x*abs_C(1,2) + EB.z*abs_C(1,0);
R01=R0+R1;
if (R>R01) return false;
R=flabs(distance.x*C(2,2) - distance.z*C(0,2));
R0=EA.x*abs_C(2,2) + EA.z*abs_C(0,2);
R1=EB.x*abs_C(1,1) + EB.y*abs_C(1,0);
R01=R0+R1;
if (R>R01) return false;
R=flabs(distance.y*C(0,0) - distance.x*C(1,0));
R0=EA.x*abs_C(1,0) + EA.y*abs_C(0,0);
R1=EB.y*abs_C(2,2) + EB.z*abs_C(2,1);
R01=R0+R1;
if (R>R01) return false;
R=flabs(distance.y*C(0,1) - distance.x*C(1,1));
R0=EA.x*abs_C(1,1) + EA.y*abs_C(0,1);
R1=EB.x*abs_C(2,2) + EB.z*abs_C(2,0);
R01=R0+R1;
if (R>R01) return false;
R=flabs(distance.y*C(0,2) - distance.x*C(1,2));
R0=EA.x*abs_C(1,2) + EA.y*abs_C(0,2);
R1=EB.x*abs_C(2,1) + EB.y*abs_C(2,0);
R01=R0+R1;
if (R>R01) return false;
return true;
}
extern "C" {
int tri_tri_intersect(float V0[3],float V1[3],float V2[3],
float U0[3],float U1[3],float U2[3]);
};
Triangle::Triangle(const Vector3D& _1, const Vector3D& _2, const Vector3D& _3)
: v1(_1), v2(_2), v3(_3), center((1.0f/3.0f)*(_1+_2+_3))
{}
bool Triangle::intersect(const Vector3D& O, const Vector3D& D, Vector3D& cp,
float& tparm, float segmax)
{
Plane p(v1,v2,v3);
float denom=p.normal*D;
if (IsZero(denom)) return false;
float t=-(p.d+p.normal*O)/denom;
if (t<=0.0f) return false;
if (t>segmax) return false;
TriangleDesc td(*this,p);
cp=O+t*D;
if (td.pointInTri(cp))
{
tparm=t;
return true;
}
return false;
}
bool Triangle::intersect(const Vector3D& O, float radius, Vector3D& cp)
{
Plane p(v1,v2,v3);
float dist=p.Classify(O);
if (flabs(dist) > radius) return false;
Vector3D point=O-dist*p.normal;
TriangleDesc td(*this,p);
if (td.pointInTri(point))
{
cp=point;
return true;
}
return false;
}
bool Triangle::intersect(const Triangle& t) const
{
return (tri_tri_intersect((float*)&v1.x,
(float*)&v2.x,
(float*)&v3.x,
(float*)&t.v1.x,
(float*)&t.v2.x,
(float*)&t.v3.x) != 0);
}
__CD__END
+205
View File
@@ -0,0 +1,205 @@
/* ColDet - C++ 3D Collision Detection Library
* Copyright (C) 2000 Amir Geva
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Any comments, questions and bug reports send to:
* photon@photoneffect.com
*
* Or visit the home page: http://photoneffect.com/coldet/
*/
#ifndef H_BOX
#define H_BOX
#include <vector>
#include "math3d.h"
#include "sysdep.h"
__CD__BEGIN
/** Stores rotation vectors used in the intersection tests,
to avoid recalculating them each time. */
class RotationState
{
public:
RotationState(const Matrix3D& transform);
Vector3D N[3];
Matrix3D t;
};
/** AABB class, with support for testing against OBBs. */
class Box
{
public:
/** Default constructor */
Box() {}
/** Construct from scalar corner position and size */
Box(float x, float y, float z, float sx, float sy, float sz)
: m_Pos(x,y,z), m_Size(sx,sy,sz),
m_Center(x+0.5f*sx,y+0.5f*sy,z+0.5f*sz) {}
/** Construct from corner position and size */
Box(const Vector3D& pos, const Vector3D& size)
: m_Pos(pos), m_Size(size), m_Center(pos+0.5f*size) {}
/** Copy constructor */
Box(const Box& b) : m_Pos(b.m_Pos), m_Size(b.m_Size), m_Center(b.m_Center) {}
virtual ~Box() {}
/** Returns the box's position */
const Vector3D& getPosition() const { return m_Pos; }
/** Returns the sizes of the box's edges */
const Vector3D& getSize() const { return m_Size; }
/** Returns the center position of the box */
const Vector3D& getCenter() const { return m_Center; }
/** Returns the volume of the box */
float getVolume() const { return m_Size.x*m_Size.y*m_Size.z; }
/** Ray intersection */
bool intersect(const Vector3D& O, const Vector3D& D);
/** Line segment intersection */
bool intersect(const Vector3D& O, const Vector3D& D, float segmax);
/** Sphere intersection */
bool intersect(const Vector3D& O, float radius);
/** Point in box */
bool intersect(const Vector3D& p) const;
/** Aligned box intersection */
bool intersect(const Box& b);
/** Oriented box intersection. */
bool intersect(const Box& b, RotationState& rs);
/** Position of box corner */
Vector3D m_Pos;
/** Size of box box edges */
Vector3D m_Size;
/** Position of box center. m_Pos+0.5f*m_Size; */
Vector3D m_Center;
};
/** A single triangle in the model */
class Triangle
{
public:
/** Default constructor */
Triangle() {}
/** Constructor to build a triangle from 3 points */
Triangle(const Vector3D& _1, const Vector3D& _2, const Vector3D& _3);
/** Tests for intersection with another triangle. */
bool intersect(const Triangle& t) const;
/** Tests for intersection with a ray (O origin, D direction)
Returns true if collision occured.
Outputs collision point in cp
Outputs the distance from the origin to the collision point in tparm
This distance is relative to the magnitude of D
Allows testing against a finite segment, by specifying
the maximum length of the ray in segmax
This length is also relative to the magnitude of D
*/
bool intersect(const Vector3D& O, const Vector3D& D, Vector3D& cp,
float& tparm, float segmax);
/** Test for intersection with a sphere (O origin)
Returns true if collision occured.
Outputs collision point in cp
*/
bool intersect(const Vector3D& O, float radius, Vector3D& cp);
Vector3D v1,v2,v3;
Vector3D center;
};
class BoxedTriangle;
/** Base class for hierarchy tree nodes. */
class BoxTreeNode : public Box
{
public:
/** Default constructor */
BoxTreeNode() : Box() {}
/** Constructor for a box from position and size */
BoxTreeNode(const Vector3D& pos, const Vector3D& size)
: Box(pos,size) {}
/** Returns true if the node is a leaf node. */
virtual bool isLeaf() const = 0;
/** Returns the number of sons this node has */
virtual int getSonsNumber() = 0;
/** Returns a son node, by index */
virtual BoxTreeNode* getSon(int which) = 0;
/** Returns the number of triangles in this node.
Only non-zero for leaf nodes. */
virtual int getTrianglesNumber() = 0;
/** Returns the boxed triangle contained in this node
by its index
*/
virtual BoxedTriangle* getTriangle(int which) = 0;
};
/** Inner node, containing other nodes. */
class BoxTreeInnerNode : public BoxTreeNode
{
public:
BoxTreeInnerNode(const Vector3D& pos, const Vector3D& size, int logdepth)
: BoxTreeNode(pos,size), m_First(NULL), m_Second(NULL),
m_logdepth(logdepth) {};
virtual bool isLeaf() const { return false; }
/** Create the sons that will divide this box */
int createSons(const Vector3D& center);
/** Recalculate the bounds of this box to fully contain
all of its triangles
*/
void recalcBounds(Vector3D& center);
/** Recursively divide this box */
int divide(int p_depth);
int getSonsNumber()
{
int n=0;
if (m_First!=NULL) n++;
if (m_Second!=NULL) n++;
return n;
}
int getTrianglesNumber();
BoxedTriangle* getTriangle(int which);
BoxTreeNode* getSon(int which)
{
if (which==0) return m_First;
if (which==1) return m_Second;
return NULL;
}
BoxTreeNode* m_First;
BoxTreeNode* m_Second;
int m_logdepth;
std::vector<BoxedTriangle*> m_Boxes;
};
/** Leaf node, containing 1 triangle. */
class BoxedTriangle : public BoxTreeNode, public Triangle
{
public:
BoxedTriangle(const Vector3D& _1, const Vector3D& _2, const Vector3D& _3);
virtual bool isLeaf() const { return true; }
int getSonsNumber() { return 0; }
BoxTreeNode* getSon(int which) { return NULL; }
int getTrianglesNumber() { return 1; }
BoxedTriangle* getTriangle(int which)
{
if (which==0) return this;
return NULL;
}
};
__CD__END
#endif // H_BOX
+197
View File
@@ -0,0 +1,197 @@
/* ColDet - C++ 3D Collision Detection Library
* Copyright (C) 2000 Amir Geva
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Any comments, questions and bug reports send to:
* photon@photoneffect.com
*
* Or visit the home page: http://photoneffect.com/coldet/
*/
#include "sysdep.h"
#include "box.h"
__CD__BEGIN
// point in box test
bool Box::intersect(const Vector3D& p) const
{
const Vector3D& pos=getPosition();
const Vector3D& s=getSize();
if (p.x<pos.x || p.x>(pos.x+s.x)) return false;
if (p.y<pos.y || p.y>(pos.y+s.y)) return false;
if (p.z<pos.z || p.z>(pos.z+s.z)) return false;
return true;
}
// Non-oriented intersection test
bool Box::intersect(const Box& b)
{
const Vector3D& t1=getPosition();
Vector3D t2=getPosition()+getSize();
const Vector3D& p1=b.getPosition();
Vector3D p2=b.getPosition()+b.getSize();
return (Max(p1.x,t1.x) <= Min(p2.x,t2.x) &&
Max(p1.y,t1.y) <= Min(p2.y,t2.y) &&
Max(p1.z,t1.z) <= Min(p2.z,t2.z));
}
BoxedTriangle::BoxedTriangle(const Vector3D& _1,
const Vector3D& _2,
const Vector3D& _3)
: BoxTreeNode(), Triangle(_1,_2,_3)
{
m_Pos.x=Min(Min(_1.x,_2.x),_3.x);
m_Pos.y=Min(Min(_1.y,_2.y),_3.y);
m_Pos.z=Min(Min(_1.z,_2.z),_3.z);
Vector3D mx;
mx.x=Max(Max(_1.x,_2.x),_3.x);
mx.y=Max(Max(_1.y,_2.y),_3.y);
mx.z=Max(Max(_1.z,_2.z),_3.z);
m_Size=mx-getPosition();
m_Center=getPosition()+0.5f*getSize();
}
int BoxTreeInnerNode::createSons(const Vector3D& center)
{
int longest=0;
Vector3D p=getPosition();
Vector3D s=getSize();
if (1)
{
Vector3D dist(Vector3D::Zero);
for(unsigned i=0;i<m_Boxes.size();i++)
{
BoxedTriangle* bt=m_Boxes[i];
dist.x+=flabs(bt->center.x - center.x);
dist.y+=flabs(bt->center.y - center.y);
dist.z+=flabs(bt->center.z - center.z);
}
if (dist.y>dist.x && dist.y>dist.z) longest=1;
else
if (dist.z>dist.x && dist.z>dist.y) longest=2;
}
else
{
int dist[3];
dist[0]=dist[1]=dist[2]=0;
for(unsigned i=0;i<m_Boxes.size();i++)
{
BoxedTriangle* bt=m_Boxes[i];
for(int j=0;j<3;j++)
if (bt->center[j] > center[j]) dist[j]++;
else dist[j]--;
}
for(int j=0;j<3;j++)
dist[j]=abs(dist[j]);
if (dist[1]<dist[0] && dist[1]<dist[2]) longest=1;
else
if (dist[2]<dist[0] && dist[2]<dist[1]) longest=2;
}
float s1=center[longest]-p[longest];
float s2=s[longest]-s1;
s[longest]=s1;
m_First=new BoxTreeInnerNode(p,s,m_logdepth);
p[longest]+=s1;
s[longest]=s2;
m_Second=new BoxTreeInnerNode(p,s,m_logdepth);
return longest;
}
void BoxTreeInnerNode::recalcBounds(Vector3D& center)
{
if (m_Boxes.empty()) return;
center=Vector3D::Zero;
Vector3D mn(9e9f,9e9f,9e9f),mx(-9e9f,-9e9f,-9e9f);
for(unsigned i=0;i<m_Boxes.size();i++)
{
BoxedTriangle* bt=m_Boxes[i];
center+=bt->center;
mn.x=Min(Min(Min(bt->v1.x,bt->v2.x),bt->v3.x),mn.x);
mn.y=Min(Min(Min(bt->v1.y,bt->v2.y),bt->v3.y),mn.y);
mn.z=Min(Min(Min(bt->v1.z,bt->v2.z),bt->v3.z),mn.z);
mx.x=Max(Max(Max(bt->v1.x,bt->v2.x),bt->v3.x),mx.x);
mx.y=Max(Max(Max(bt->v1.y,bt->v2.y),bt->v3.y),mx.y);
mx.z=Max(Max(Max(bt->v1.z,bt->v2.z),bt->v3.z),mx.z);
}
center/=float(m_Boxes.size());
m_Pos=mn;
m_Size=mx-mn;
if (m_Size.x==0.0f) { m_Size.x=0.002f; m_Pos.x-=0.001f; }
if (m_Size.y==0.0f) { m_Size.y=0.002f; m_Pos.y-=0.001f; }
if (m_Size.z==0.0f) { m_Size.z=0.002f; m_Pos.z-=0.001f; }
m_Center=getPosition()+0.5f*getSize();
}
int BoxTreeInnerNode::divide(int p_depth)
{
if (m_Boxes.empty()) return 0;
Vector3D center;
recalcBounds(center);
int longest=createSons(center);
BoxTreeInnerNode* f=static_cast<BoxTreeInnerNode*>(m_First);
BoxTreeInnerNode* s=static_cast<BoxTreeInnerNode*>(m_Second);
int depth=1;
int bnum=m_Boxes.size();
#ifdef _DEBUG
int fnum=0;
#endif
for(unsigned i=0;i<bnum;i++)
{
BoxedTriangle* bt=m_Boxes[i];
if (bt->center[longest]<center[longest])
{
f->m_Boxes.push_back(bt);
#ifdef _DEBUG
fnum++;
#endif
}
else
{
s->m_Boxes.push_back(bt);
}
}
int b1num=f->m_Boxes.size();
int b2num=s->m_Boxes.size();
if ((b1num==bnum || b2num==bnum))// && p_depth>m_logdepth)
{
delete m_First; m_First=NULL;
delete m_Second; m_Second=NULL;
return depth+1;
}
m_Boxes.clear();
if (f->m_Boxes.empty()) { delete m_First; m_First=NULL; }
else
if (f->m_Boxes.size()==1)
{
BoxedTriangle* bt=f->m_Boxes.back();
delete m_First;
m_First=bt;
} else depth=f->divide(p_depth+1);
if (s->m_Boxes.empty()) { delete m_Second; m_Second=NULL; }
else
if (s->m_Boxes.size()==1)
{
BoxedTriangle* bt=s->m_Boxes.back();
delete m_Second;
m_Second=bt;
} else depth=Max(depth,s->divide(p_depth+1));
return depth+1;
}
__CD__BEGIN
+389
View File
@@ -0,0 +1,389 @@
/* ColDet - C++ 3D Collision Detection Library
* Copyright (C) 2000 Amir Geva
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Any comments, questions and bug reports send to:
* photon@photoneffect.com
*
* Or visit the home page: http://photoneffect.com/coldet/
*/
#include "sysdep.h"
#include "coldetimpl.h"
#include "mytritri.h"
#include <assert.h>
__CD__BEGIN
class Check
{
public:
Check() {}
Check(BoxTreeNode* f, BoxTreeNode* s, int d)
: m_first(f), m_second(s), depth(d) {}
BoxTreeNode* m_first;
BoxTreeNode* m_second;
int depth;
};
bool CollisionModel3DImpl::collision(CollisionModel3D* other,
int AccuracyDepth,
int MaxProcessingTime,
float* other_transform)
{
m_ColType=Models;
CollisionModel3DImpl* o=static_cast<CollisionModel3DImpl*>(other);
if (!m_Final) throw Inconsistency();
if (!o->m_Final) throw Inconsistency();
Matrix3D t=( other_transform==NULL ? o->m_Transform : *((Matrix3D*)other_transform) );
if (m_Static) t *= m_InvTransform;
else t *= m_Transform.Inverse();
RotationState rs(t);
if (AccuracyDepth<0) AccuracyDepth=0xFFFFFF;
if (MaxProcessingTime==0) MaxProcessingTime=0xFFFFFF;
DWORD EndTime,BeginTime = GetTickCount();
int num=Max(m_Triangles.size(),o->m_Triangles.size());
int Allocated=Max(64,(num>>4));
std::vector<Check> checks(Allocated);
int queue_idx=1;
Check& c=checks[0];
c.m_first=&m_Root;
c.depth=0;
c.m_second=&o->m_Root;
while (queue_idx>0)
{
if (queue_idx>(Allocated/2)) // enlarge the queue.
{
Check c;
checks.insert(checks.end(),Allocated,c);
Allocated*=2;
}
EndTime=GetTickCount();
if (EndTime >= (BeginTime+MaxProcessingTime)) throw TimeoutExpired();
// @@@ add depth check
//Check c=checks.back();
Check& c=checks[--queue_idx];
BoxTreeNode* first=c.m_first;
BoxTreeNode* second=c.m_second;
assert(first!=NULL);
assert(second!=NULL);
if (first->intersect(*second,rs))
{
int tnum1=first->getTrianglesNumber();
int tnum2=second->getTrianglesNumber();
if (tnum1>0 && tnum2>0)
{
{
for(int i=0;i<tnum2;i++)
{
BoxedTriangle* bt2=second->getTriangle(i);
Triangle tt(Transform(bt2->v1,rs.t),Transform(bt2->v2,rs.t),Transform(bt2->v3,rs.t));
for(int j=0;j<tnum1;j++)
{
BoxedTriangle* bt1=first->getTriangle(j);
if (tt.intersect(*bt1))
{
m_ColTri1=*bt1;
m_iColTri1=getTriangleIndex(bt1);
m_ColTri2=tt;
m_iColTri2=o->getTriangleIndex(bt2);
return true;
}
}
}
}
}
else
if (first->getSonsNumber()==0)
{
BoxTreeNode* s1=second->getSon(0);
BoxTreeNode* s2=second->getSon(1);
assert(s1!=NULL);
assert(s2!=NULL);
Check& c1=checks[queue_idx++];
c1.m_first=first;
c1.m_second=s1;
Check& c2=checks[queue_idx++];
c2.m_first=first;
c2.m_second=s2;
}
else
if (second->getSonsNumber()==0)
{
BoxTreeNode* f1=first->getSon(0);
BoxTreeNode* f2=first->getSon(1);
assert(f1!=NULL);
assert(f2!=NULL);
Check& c1=checks[queue_idx++];
c1.m_first=f1;
c1.m_second=second;
Check& c2=checks[queue_idx++];
c2.m_first=f2;
c2.m_second=second;
}
else
{
float v1=first->getVolume();
float v2=second->getVolume();
if (v1>v2)
{
BoxTreeNode* f1=first->getSon(0);
BoxTreeNode* f2=first->getSon(1);
assert(f1!=NULL);
assert(f2!=NULL);
Check& c1=checks[queue_idx++];
c1.m_first=f1;
c1.m_second=second;
Check& c2=checks[queue_idx++];
c2.m_first=f2;
c2.m_second=second;
}
else
{
BoxTreeNode* s1=second->getSon(0);
BoxTreeNode* s2=second->getSon(1);
assert(s1!=NULL);
assert(s2!=NULL);
Check& c1=checks[queue_idx++];
c1.m_first=first;
c1.m_second=s1;
Check& c2=checks[queue_idx++];
c2.m_first=first;
c2.m_second=s2;
}
}
}
}
return false;
}
bool CollisionModel3DImpl::rayCollision(float origin[3],
float direction[3],
bool closest,
float segmin,
float segmax)
{
float mintparm=9e9f,tparm;
Vector3D col_point;
m_ColType=Ray;
Vector3D O;
Vector3D D;
if (m_Static)
{
O=Transform(*(Vector3D*)origin,m_InvTransform);
D=rotateVector(*(Vector3D*)direction,m_InvTransform);
}
else
{
Matrix3D inv=m_Transform.Inverse();
O=Transform(*(Vector3D*)origin,inv);
D=rotateVector(*(Vector3D*)direction,inv);
}
if (segmin!=0.0f) // normalize ray
{
O+=segmin*D;
segmax-=segmin;
segmin=0.0f;
}
if (segmax<segmin)
{
D=-D;
segmax=-segmax;
}
std::vector<BoxTreeNode*> checks;
checks.push_back(&m_Root);
while (!checks.empty())
{
BoxTreeNode* b=checks.back();
checks.pop_back();
if (b->intersect(O,D,segmax))
{
int sons=b->getSonsNumber();
if (sons)
while (sons--) checks.push_back(b->getSon(sons));
else
{
int tri=b->getTrianglesNumber();
while (tri--)
{
BoxedTriangle* bt=b->getTriangle(tri);
Triangle* t=static_cast<Triangle*>(bt);
if (t->intersect(O,D,col_point,tparm,segmax))
{
if (closest)
{
if (tparm<mintparm)
{
mintparm=tparm;
m_ColTri1=*bt;
m_iColTri1=getTriangleIndex(bt);
m_ColPoint=col_point;
}
}
else
{
m_ColTri1=*bt;
m_iColTri1=getTriangleIndex(bt);
m_ColPoint=col_point;
return true;
}
}
}
}
}
}
if (closest && mintparm<9e9f) return true;
return false;
}
bool CollisionModel3DImpl::sphereCollision(float origin[3], float radius)
{
m_ColType=Sphere;
Vector3D O;
if (m_Static)
O=Transform(*(Vector3D*)origin,m_InvTransform);
else
{
Matrix3D inv=m_Transform.Inverse();
O=Transform(*(Vector3D*)origin,inv);
}
std::vector<BoxTreeNode*> checks;
checks.push_back(&m_Root);
while (!checks.empty())
{
BoxTreeNode* b=checks.back();
checks.pop_back();
if (b->intersect(O,radius))
{
int sons=b->getSonsNumber();
if (sons)
while (sons--) checks.push_back(b->getSon(sons));
else
{
int tri=b->getTrianglesNumber();
while (tri--)
{
BoxedTriangle* bt=b->getTriangle(tri);
Triangle* t=static_cast<Triangle*>(bt);
if (t->intersect(O,radius,m_ColPoint))
{
m_ColTri1=*bt;
m_iColTri1=getTriangleIndex(bt);
return true;
}
}
}
}
}
return false;
}
bool CollisionModel3DImpl::getCollidingTriangles(float t1[9], float t2[9], bool ModelSpace)
{
if (ModelSpace)
{
if (t1!=NULL)
{
*((Vector3D*)&t1[0]) = m_ColTri1.v1;
*((Vector3D*)&t1[3]) = m_ColTri1.v2;
*((Vector3D*)&t1[6]) = m_ColTri1.v3;
}
if (t2!=NULL)
{
*((Vector3D*)&t2[0]) = m_ColTri2.v1;
*((Vector3D*)&t2[3]) = m_ColTri2.v2;
*((Vector3D*)&t2[6]) = m_ColTri2.v3;
}
}
else
{
if (t1!=NULL)
{
*((Vector3D*)&t1[0]) = Transform(m_ColTri1.v1,m_Transform);
*((Vector3D*)&t1[3]) = Transform(m_ColTri1.v2,m_Transform);
*((Vector3D*)&t1[6]) = Transform(m_ColTri1.v3,m_Transform);
}
if (t2!=NULL)
{
*((Vector3D*)&t2[0]) = Transform(m_ColTri2.v1,m_Transform);
*((Vector3D*)&t2[3]) = Transform(m_ColTri2.v2,m_Transform);
*((Vector3D*)&t2[6]) = Transform(m_ColTri2.v3,m_Transform);
}
}
return true;
}
bool CollisionModel3DImpl::getCollidingTriangles(int& t1, int& t2)
{
t1=m_iColTri1;
t2=m_iColTri2;
return true;
}
bool CollisionModel3DImpl::getCollisionPoint(float p[3], bool ModelSpace)
{
Vector3D& v=*((Vector3D*)p);
switch (m_ColType)
{
case Models: v=my_tri_tri_intersect(m_ColTri1,m_ColTri2); break;
case Sphere:
case Ray: v=m_ColPoint; break;
default: v=Vector3D::Zero;
}
if (!ModelSpace) v=Transform(v,m_Transform);
return true;
}
bool SphereRayCollision(float center[3], float radius,
float origin[3], float direction[3],
float point[3])
{
Vector3D& C=*((Vector3D*)center);
Vector3D& O=*((Vector3D*)origin);
Vector3D D=((Vector3D*)direction)->Normalized();
Vector3D& P=*((Vector3D*)point);
Vector3D EO=C-O;
float v=EO*D;
float disc=radius*radius - (EO*EO - v*v);
if (disc<0.0f) return false;
float d=sqrt(disc);
P=O+(v-d)*D;
return true;
}
bool SphereSphereCollision(float c1[3], float r1,
float c2[3], float r2)
{
Vector3D& C1=*((Vector3D*)c1);
Vector3D& C2=*((Vector3D*)c2);
float dist=(C2-C1).SquareMagnitude();
float sum=r1+r2;
return (dist < sum*sum);
}
__CD__END
+157
View File
@@ -0,0 +1,157 @@
# Microsoft Developer Studio Project File - Name="coldet" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
CFG=coldet - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "coldet.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "coldet.mak" CFG="coldet - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "coldet - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE "coldet - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
MTL=midl.exe
RSC=rc.exe
!IF "$(CFG)" == "coldet - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "COLDET_EXPORTS" /YX /FD /c
# ADD CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "COLDET_EXPORTS" /YX /FD /c
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD BASE RSC /l 0x409 /d "NDEBUG"
# ADD RSC /l 0x409 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
!ELSEIF "$(CFG)" == "coldet - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "Debug"
# PROP Intermediate_Dir "Debug"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "COLDET_EXPORTS" /YX /FD /GZ /c
# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "COLDET_EXPORTS" /FR /YX /FD /GZ /c
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
# ADD BASE RSC /l 0x409 /d "_DEBUG"
# ADD RSC /l 0x409 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
!ENDIF
# Begin Target
# Name "coldet - Win32 Release"
# Name "coldet - Win32 Debug"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=.\box.cpp
# End Source File
# Begin Source File
SOURCE=.\box_bld.cpp
# End Source File
# Begin Source File
SOURCE=.\coldet.cpp
# End Source File
# Begin Source File
SOURCE=.\coldet_bld.cpp
# End Source File
# Begin Source File
SOURCE=.\math3d.cpp
# End Source File
# Begin Source File
SOURCE=.\mytritri.cpp
# End Source File
# Begin Source File
SOURCE=.\sysdep.cpp
# End Source File
# Begin Source File
SOURCE=.\tritri.c
# End Source File
# End Group
# Begin Group "Header Files"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
SOURCE=.\box.h
# End Source File
# Begin Source File
SOURCE=.\coldet.h
# End Source File
# Begin Source File
SOURCE=.\coldetimpl.h
# End Source File
# Begin Source File
SOURCE=.\math3d.h
# End Source File
# Begin Source File
SOURCE=.\mytritri.h
# End Source File
# Begin Source File
SOURCE=.\sysdep.h
# End Source File
# End Group
# Begin Group "Resource Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# End Group
# End Target
# End Project
+180
View File
@@ -0,0 +1,180 @@
/* ColDet - C++ 3D Collision Detection Library
* Copyright (C) 2000 Amir Geva
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Any comments, questions and bug reports send to:
* photon@photoneffect.com
*
* Or visit the home page: http://photoneffect.com/coldet/
*/
/** \file coldet.h
3D Collision Detection
Interface for the library.
Isolated from any implementation details.
*/
#ifndef H_COLDET
#define H_COLDET
#ifndef EXPORT
#define EXPORT
#endif
/** Collision Model. Will represent the mesh to be tested for
collisions. It has to be notified of all triangles, via
addTriangle()
After all triangles are added, a call to finalize() will
process the information and prepare for collision tests.
Call collision() to check for a collision
Note: Transformations must not contain scaling.
*/
class CollisionModel3D
{
public:
virtual ~CollisionModel3D() {}
/** Optional: Optimization for construction speed.
If you know the number of triangles. */
virtual void setTriangleNumber(int num) = 0;
/** Use any of the forms of this functions to enter the coordinates
of the model's triangles. */
virtual void addTriangle(float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float y3, float z3) = 0;
virtual void addTriangle(float v1[3], float v2[3], float v3[3]) = 0;
/** All triangles have been added, process model. */
virtual void finalize() = 0;
/** The the current affine matrix for the model.
See transform.txt for format information */
virtual void setTransform(float m[16]) = 0;
/** Check for collision with another model.
Do not mix model types here.
MaxProcessingTime determines the maximum time in milliseconds
to check for collision. If a rejection is not found by that
time, the function will return true.
AccuracyDepth is not yet supported.
other_transform allows overriding the other model's
transform, by supplying an alternative one.
This can be useful when testing a model against itself
with different orientations.
*/
virtual bool collision(CollisionModel3D* other,
int AccuracyDepth=-1,
int MaxProcessingTime=0,
float* other_transform=0) = 0;
/** Returns true if the ray given in world space coordinates
intersects with the object.
getCollidingTriangles() and getCollisionPoint() can be
used to retrieve information about a collision.
If closest if false, the first triangle that collides with
the ray is used. Otherwise the closest one will be used.
Closest triangle searching will slow the test considerably.
The default ray is a standard infinite ray. However, using
segmin and segmax you can define a line segment along the
ray.
*/
virtual bool rayCollision(float origin[3],
float direction[3],
bool closest=false,
float segmin=0.0f,
float segmax=3.4e+38F) = 0;
/** Returns true if the given sphere collides with the model.
getCollidingTriangles() and getCollisionPoint() can be
used to retrieve information about a collision.
*/
virtual bool sphereCollision(float origin[3],
float radius) = 0;
/** Retrieve the pair of triangles that collided.
Only valid after a call to collision() that returned true.
t1 is this model's triangle and t2 is the other one.
In case of ray or sphere collision, only t1 will be valid.
The coordinates will be in _this_ model's coordinate space,
unless ModelSpace is false, in which case, coordinates will
be transformed by the model's current transform to world space.
*/
virtual bool getCollidingTriangles(float t1[9], float t2[9], bool ModelSpace=true) = 0;
/** Retrieve the pair of triangles indices that collided.
Only valid after a call to collision() that returned true.
t1 belongs to _this_ model, while t2 is in the other one.
*/
virtual bool getCollidingTriangles(int& t1, int& t2) = 0;
/** Retrieve the detected collision point.
Only valid after a call to collision()
that returned true.
The coordinates will be in _this_ model's coordinate space,
unless ModelSpace is false, in which case, coordinates will
be transformed by the model's current transform to world space.
*/
virtual bool getCollisionPoint(float p[3], bool ModelSpace=true) = 0;
};
/** Timeout exception class. Exception will be thrown if
the detection algorithm could not complete within
the given time limit. */
class TimeoutExpired {};
/** Inconsistency exception. Exception will be thrown if
the model is inconsistent.
Examples:
Checking for collisions before calling finalize()
Trying to add triangles after calling finalize() */
class Inconsistency {};
/** Create a new collision model object.
Use delete when finished with it.
Setting Static to true indicates that the model does not
move a lot, and certain calculations can be done every time
its transform changes instead of every collision test.
*/
EXPORT CollisionModel3D* newCollisionModel3D(bool Static=false);
//////////////////////////////////////////////
// Utility Functions
//////////////////////////////////////////////
/** Checks for intersection between a ray and a sphere.
center, radius define the sphere
origin, direction define the ray
point will contain point of intersection, if one is found.
*/
bool SphereRayCollision(float center[3], float radius,
float origin[3], float direction[3],
float point[3]);
/** Checks for intersection between 2 spheres. */
bool SphereSphereCollision(float c1[3], float r1,
float c2[3], float r2);
#endif // H_COLDET
+74
View File
@@ -0,0 +1,74 @@
/* ColDet - C++ 3D Collision Detection Library
* Copyright (C) 2000 Amir Geva
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Any comments, questions and bug reports send to:
* photon@photoneffect.com
*
* Or visit the home page: http://photoneffect.com/coldet/
*/
#include "sysdep.h"
#include "coldetimpl.h"
__CD__BEGIN
CollisionModel3D* newCollisionModel3D(bool Static)
{
return new CollisionModel3DImpl(Static);
}
CollisionModel3DImpl::CollisionModel3DImpl(bool Static)
: m_Root(Vector3D::Zero, Vector3D::Zero,0),
m_Transform(Matrix3D::Identity),
m_InvTransform(Matrix3D::Identity),
m_ColTri1(Vector3D::Zero,Vector3D::Zero,Vector3D::Zero),
m_ColTri2(Vector3D::Zero,Vector3D::Zero,Vector3D::Zero),
m_iColTri1(0),
m_iColTri2(0),
m_Final(false),
m_Static(Static)
{}
void CollisionModel3DImpl::addTriangle(const Vector3D& v1, const Vector3D& v2, const Vector3D& v3)
{
if (m_Final) throw Inconsistency();
m_Triangles.push_back(BoxedTriangle(v1,v2,v3));
}
void CollisionModel3DImpl::setTransform(const Matrix3D& m)
{
m_Transform=m;
if (m_Static) m_InvTransform=m_Transform.Inverse();
}
void CollisionModel3DImpl::finalize()
{
if (m_Final) throw Inconsistency();
// Prepare initial triangle list
m_Final=true;
for(unsigned i=0;i<m_Triangles.size();i++)
{
BoxedTriangle& bt=m_Triangles[i];
m_Root.m_Boxes.push_back(&bt);
}
int logdepth=0;
for(int num=m_Triangles.size();num>0;num>>=1,logdepth++);
m_Root.m_logdepth=int(logdepth*1.5f);
m_Root.divide(0);
}
__CD__END
+108
View File
@@ -0,0 +1,108 @@
/* ColDet - C++ 3D Collision Detection Library
* Copyright (C) 2000 Amir Geva
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Any comments, questions and bug reports send to:
* photon@photoneffect.com
*
* Or visit the home page: http://photoneffect.com/coldet/
*/
#ifndef H_COLDET_IMPL
#define H_COLDET_IMPL
#include "coldet.h"
#include "box.h"
#include "math3d.h"
#include <vector>
__CD__BEGIN
class CollisionModel3DImpl : public CollisionModel3D
{
public:
CollisionModel3DImpl(bool Static);
void setTriangleNumber(int num) { if (!m_Final) m_Triangles.reserve(num); }
void addTriangle(float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float y3, float z3)
{
addTriangle(Vector3D(x1,y1,z1),
Vector3D(x2,y2,z2),
Vector3D(x3,y3,z3));
}
void addTriangle(float v1[3], float v2[3], float v3[3])
{
addTriangle(Vector3D(v1[0],v1[1],v1[2]),
Vector3D(v2[0],v2[1],v2[2]),
Vector3D(v3[0],v3[1],v3[2]));
}
void addTriangle(const Vector3D& v1, const Vector3D& v2, const Vector3D& v3);
void finalize();
void setTransform(float m[16]) { setTransform(*(Matrix3D*)m); }
void setTransform(const Matrix3D& m);
bool collision(CollisionModel3D* other,
int AccuracyDepth,
int MaxProcessingTime,
float* other_transform);
bool rayCollision(float origin[3], float direction[3], bool closest,
float segmin, float segmax);
bool sphereCollision(float origin[3], float radius);
bool getCollidingTriangles(float t1[9], float t2[9], bool ModelSpace);
bool getCollidingTriangles(int& t1, int& t2);
bool getCollisionPoint(float p[3], bool ModelSpace);
int getTriangleIndex(BoxedTriangle* bt)
{
//return int(bt-m_Triangles.begin());
return int(bt-&(*m_Triangles.begin()));
}
/** Stores all the actual triangles. Other objects will use
pointers into this array.
*/
std::vector<BoxedTriangle> m_Triangles;
/** Root of the hierarchy tree */
BoxTreeInnerNode m_Root;
/** The current transform and its inverse */
Matrix3D m_Transform,m_InvTransform;
/** The triangles that last collided */
Triangle m_ColTri1,m_ColTri2;
/** The indices of the triangles that last collided */
int m_iColTri1,m_iColTri2;
/** The collision point of the last test */
Vector3D m_ColPoint;
/** Type of the last collision test */
enum { Models, Ray, Sphere }
m_ColType;
/** Flag for indicating the model is finalized. */
bool m_Final;
/** Static models will maintain the same transform for a while
so the inverse transform is calculated each set instead
of in the collision test. */
bool m_Static;
};
__CD__BEGIN
#endif // H_COLDET_IMPL
+50
View File
@@ -0,0 +1,50 @@
PROJECT=coldet
LIB=libcoldet.a
CC=g++
OPT=-O2
CFLAGS=-c $(OPT) -DGCC
OBJS= \
coldet.o \
coldet_bld.o \
box.o \
box_bld.o \
tritri.o \
math3d.o \
sysdep.o \
mytritri.o
all: $(LIB)
$(LIB): $(OBJS)
rm -f $(LIB)
ar cr $(LIB) $(OBJS)
ranlib $(LIB)
coldet.o: coldet.cpp
$(CC) $(CFLAGS) coldet.cpp
coldet_bld.o: coldet_bld.cpp
$(CC) $(CFLAGS) coldet_bld.cpp
box.o: box.cpp
$(CC) $(CFLAGS) box.cpp
box_bld.o: box_bld.cpp
$(CC) $(CFLAGS) box_bld.cpp
tritri.o: tritri.c
gcc $(CFLAGS) tritri.c
mytritri.o: mytritri.cpp
$(CC) $(CFLAGS) mytritri.cpp
math3d.o: math3d.cpp
$(CC) $(CFLAGS) math3d.cpp
sysdep.o: sysdep.cpp
$(CC) $(CFLAGS) sysdep.cpp
clean:
rm -f *.o $(LIB)
+93
View File
@@ -0,0 +1,93 @@
/* ColDet - C++ 3D Collision Detection Library
* Copyright (C) 2000 Amir Geva
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Any comments, questions and bug reports send to:
* photon@photoneffect.com
*
* Or visit the home page: http://photoneffect.com/coldet/
*/
#include "sysdep.h"
#include "math3d.h"
const Vector3D Vector3D::Zero(0.0f,0.0f,0.0f);
const Matrix3D Matrix3D::Identity(1.0f,0.0f,0.0f,0.0f,
0.0f,1.0f,0.0f,0.0f,
0.0f,0.0f,1.0f,0.0f,
0.0f,0.0f,0.0f,1.0f);
inline float
MINOR(const Matrix3D& m, const int r0, const int r1, const int r2, const int c0, const int c1, const int c2)
{
return m(r0,c0) * (m(r1,c1) * m(r2,c2) - m(r2,c1) * m(r1,c2)) -
m(r0,c1) * (m(r1,c0) * m(r2,c2) - m(r2,c0) * m(r1,c2)) +
m(r0,c2) * (m(r1,c0) * m(r2,c1) - m(r2,c0) * m(r1,c1));
}
Matrix3D
Matrix3D::Adjoint() const
{
return Matrix3D( MINOR(*this, 1, 2, 3, 1, 2, 3),
-MINOR(*this, 0, 2, 3, 1, 2, 3),
MINOR(*this, 0, 1, 3, 1, 2, 3),
-MINOR(*this, 0, 1, 2, 1, 2, 3),
-MINOR(*this, 1, 2, 3, 0, 2, 3),
MINOR(*this, 0, 2, 3, 0, 2, 3),
-MINOR(*this, 0, 1, 3, 0, 2, 3),
MINOR(*this, 0, 1, 2, 0, 2, 3),
MINOR(*this, 1, 2, 3, 0, 1, 3),
-MINOR(*this, 0, 2, 3, 0, 1, 3),
MINOR(*this, 0, 1, 3, 0, 1, 3),
-MINOR(*this, 0, 1, 2, 0, 1, 3),
-MINOR(*this, 1, 2, 3, 0, 1, 2),
MINOR(*this, 0, 2, 3, 0, 1, 2),
-MINOR(*this, 0, 1, 3, 0, 1, 2),
MINOR(*this, 0, 1, 2, 0, 1, 2));
}
float
Matrix3D::Determinant() const
{
return m[0][0] * MINOR(*this, 1, 2, 3, 1, 2, 3) -
m[0][1] * MINOR(*this, 1, 2, 3, 0, 2, 3) +
m[0][2] * MINOR(*this, 1, 2, 3, 0, 1, 3) -
m[0][3] * MINOR(*this, 1, 2, 3, 0, 1, 2);
}
Matrix3D
Matrix3D::Inverse() const
{
return (1.0f / Determinant()) * Adjoint();
}
Vector3D Matrix3D::GetTranslate() const
{
return Vector3D(m[3][0], m[3][1], m[3][2]);
}
void Matrix3D::Translate(const Vector3D & v)
{
m[3][0] += v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0];
m[3][1] += v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1];
m[3][2] += v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2];
}
+335
View File
@@ -0,0 +1,335 @@
/* ColDet - C++ 3D Collision Detection Library
* Copyright (C) 2000 Amir Geva
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Any comments, questions and bug reports send to:
* photon@photoneffect.com
*
* Or visit the home page: http://photoneffect.com/coldet/
*/
#ifndef H_MATH3D
#define H_MATH3D
#include <math.h>
struct Vector3D;
struct Matrix3;
struct Matrix3D;
struct Plane;
inline float flabs(float f) { return (f>=0.0f?f:-f); }
const float epsilon=1e-8f;
inline bool IsZero(float f) { return flabs(f)<epsilon; }
Vector3D operator*(float scalar, const Vector3D& v);
Vector3D operator*(const Vector3D & v, float scalar);
float operator*(const Vector3D& v1, const Vector3D& v2);
Vector3D operator+(const Vector3D& v1, const Vector3D& v2);
Vector3D operator-(const Vector3D& v1, const Vector3D& v2);
Vector3D CrossProduct(const Vector3D& v1, const Vector3D& v2);
Matrix3D operator*(const Matrix3D& m1, const Matrix3D& m2);
Matrix3D operator*(float scalar, const Matrix3D& m);
struct Vector3D
{
float x,y,z;
static const Vector3D Zero;
Vector3D() {}
Vector3D(float X, float Y, float Z) : x(X), y(Y), z(Z) {}
Vector3D(const Vector3D& v) : x(v.x), y(v.y), z(v.z) {}
Vector3D& operator+=(const Vector3D& v) { x+=v.x; y+=v.y; z+=v.z; return *this; }
Vector3D& operator*=(float s) { x*=s; y*=s; z*=s; return *this; }
Vector3D& operator/=(float s) { return *this *= (1.0f/s); }
bool operator==(const Vector3D& v) { return x==v.x && y==v.y && z==v.z; }
Vector3D operator- () const { return Vector3D(-x,-y,-z); }
float SquareMagnitude () const { return x*x+y*y+z*z; }
float Magnitude () const { return (float)sqrt(SquareMagnitude()); }
Vector3D Normalized () const { return (1.0f/Magnitude())*(*this); }
float operator[] (int i) const { return ((float*)&x)[i]; }
float& operator[] (int i) { return ((float*)&x)[i]; }
};
#define _11 sclr.s11
#define _12 sclr.s12
#define _13 sclr.s13
#define _14 sclr.s14
#define _21 sclr.s21
#define _22 sclr.s22
#define _23 sclr.s23
#define _24 sclr.s24
#define _31 sclr.s31
#define _32 sclr.s32
#define _33 sclr.s33
#define _34 sclr.s34
#define _41 sclr.s41
#define _42 sclr.s42
#define _43 sclr.s43
#define _44 sclr.s44
struct Matrix3
{
union {
struct { float s11,s12,s13,
s21,s22,s23,
s31,s32,s33; } sclr;
float m[3][3];
};
static const Matrix3 Identity;
Vector3D& baseRow(int i) { return *((Vector3D*)m[i]); }
float operator() (int i, int j) const { return m[i][j]; }
float& operator() (int i, int j) { return m[i][j]; }
};
struct Matrix3D
{
union {
struct { float s11,s12,s13,s14,
s21,s22,s23,s24,
s31,s32,s33,s34,
s41,s42,s43,s44; } sclr;
float m[4][4];
};
static const Matrix3D Identity;
Matrix3D() {}
Matrix3D(float f11, float f12, float f13, float f14,
float f21, float f22, float f23, float f24,
float f31, float f32, float f33, float f34,
float f41, float f42, float f43, float f44)
{
_11=f11; _12=f12; _13=f13; _14=f14;
_21=f21; _22=f22; _23=f23; _24=f24;
_31=f31; _32=f32; _33=f33; _34=f34;
_41=f41; _42=f42; _43=f43; _44=f44;
}
Matrix3D& operator*= (const Matrix3D& m)
{
return *this = *this * m;
}
friend Matrix3D PitchMatrix3D(const float theta);
friend Matrix3D YawMatrix3D(const float theta);
friend Matrix3D RollMatrix3D(const float theta);
void rotate(const Vector3D& v);
Matrix3D Inverse() const;
Matrix3D Adjoint() const;
float Determinant() const;
Vector3D GetTranslate() const;
void Translate(const Vector3D & v);
float operator() (int i, int j) const { return m[i][j]; }
float& operator() (int i, int j) { return m[i][j]; }
};
struct Plane
{
Vector3D normal;
float d;
Plane(const Vector3D& a, const Vector3D& b, const Vector3D& c)
{
normal = CrossProduct(b - a, c - a).Normalized();
d = -normal * a;
}
float Classify(const Vector3D& v)
{
return v * normal + d;
}
};
inline Vector3D operator* (float scalar, const Vector3D& v)
{
return Vector3D(scalar*v.x,scalar*v.y,scalar*v.z);
}
inline Vector3D operator* (const Vector3D& v, float scalar)
{
return Vector3D(scalar*v.x,scalar*v.y,scalar*v.z);
}
inline Vector3D operator+ (const Vector3D& v1, const Vector3D& v2)
{
return Vector3D(v1.x+v2.x,v1.y+v2.y,v1.z+v2.z);
}
inline Vector3D operator- (const Vector3D& v1, const Vector3D& v2)
{
return Vector3D(v1.x-v2.x,v1.y-v2.y,v1.z-v2.z);
}
inline float operator* (const Vector3D& v1, const Vector3D& v2)
{
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
}
inline Vector3D CrossProduct(const Vector3D& v1, const Vector3D& v2)
{
return Vector3D(v1.y*v2.z-v2.y*v1.z,
v1.z*v2.x-v2.z*v1.x,
v1.x*v2.y-v2.x*v1.y);
}
inline Vector3D Transform(const Vector3D& v, const Matrix3D& m)
{
return Vector3D(v.x*m._11 + v.y*m._21 + v.z*m._31 + m._41,
v.x*m._12 + v.y*m._22 + v.z*m._32 + m._42,
v.x*m._13 + v.y*m._23 + v.z*m._33 + m._43);
}
inline Vector3D rotateVector(const Vector3D& v, const Matrix3D& m)
{
return Vector3D(v.x*m._11 + v.y*m._21 + v.z*m._31,
v.x*m._12 + v.y*m._22 + v.z*m._32,
v.x*m._13 + v.y*m._23 + v.z*m._33);
}
inline Matrix3D operator*(float scalar, const Matrix3D& m)
{
return Matrix3D(scalar*m(0,0),scalar*m(0,1),scalar*m(0,2),scalar*m(0,3),
scalar*m(1,0),scalar*m(1,1),scalar*m(1,2),scalar*m(1,3),
scalar*m(2,0),scalar*m(2,1),scalar*m(2,2),scalar*m(2,3),
scalar*m(3,0),scalar*m(3,1),scalar*m(3,2),scalar*m(3,3));
}
inline Matrix3D operator*(const Matrix3D& m1, const Matrix3D& m2)
{
return Matrix3D(
m1._11*m2._11 + m1._12*m2._21 + m1._13*m2._31 + m1._14*m2._41,
m1._11*m2._12 + m1._12*m2._22 + m1._13*m2._32 + m1._14*m2._42,
m1._11*m2._13 + m1._12*m2._23 + m1._13*m2._33 + m1._14*m2._43,
m1._11*m2._14 + m1._12*m2._24 + m1._13*m2._34 + m1._14*m2._44,
m1._21*m2._11 + m1._22*m2._21 + m1._23*m2._31 + m1._24*m2._41,
m1._21*m2._12 + m1._22*m2._22 + m1._23*m2._32 + m1._24*m2._42,
m1._21*m2._13 + m1._22*m2._23 + m1._23*m2._33 + m1._24*m2._43,
m1._21*m2._14 + m1._22*m2._24 + m1._23*m2._34 + m1._24*m2._44,
m1._31*m2._11 + m1._32*m2._21 + m1._33*m2._31 + m1._34*m2._41,
m1._31*m2._12 + m1._32*m2._22 + m1._33*m2._32 + m1._34*m2._42,
m1._31*m2._13 + m1._32*m2._23 + m1._33*m2._33 + m1._34*m2._43,
m1._31*m2._14 + m1._32*m2._24 + m1._33*m2._34 + m1._34*m2._44,
m1._41*m2._11 + m1._42*m2._21 + m1._43*m2._31 + m1._44*m2._41,
m1._41*m2._12 + m1._42*m2._22 + m1._43*m2._32 + m1._44*m2._42,
m1._41*m2._13 + m1._42*m2._23 + m1._43*m2._33 + m1._44*m2._43,
m1._41*m2._14 + m1._42*m2._24 + m1._43*m2._34 + m1._44*m2._44);
}
inline void
Matrix3D::rotate(const Vector3D& v)
{
if (v.x!=0.0f) *this = PitchMatrix3D(v.x) * (*this);
if (v.y!=0.0f) *this = YawMatrix3D (v.y) * (*this);
if (v.z!=0.0f) *this = RollMatrix3D (v.z) * (*this);
}
inline Matrix3D
TranslateMatrix3D(const Vector3D& v)
{
return Matrix3D(1.0f,0.0f,0.0f,0.0f,
0.0f,1.0f,0.0f,0.0f,
0.0f,0.0f,1.0f,0.0f,
v.x, v.y, v.z,1.0f);
}
inline Matrix3D
ScaleMatrix3D(const Vector3D& v)
{
return Matrix3D( v.x,0.0f,0.0f,0.0f,
0.0f, v.y,0.0f,0.0f,
0.0f,0.0f, v.z,0.0f,
0.0f,0.0f,0.0f,1.0f);
}
inline Matrix3D
ScaleMatrix3D(const float s)
{
return ScaleMatrix3D(Vector3D(s,s,s));
}
inline Matrix3D
PitchMatrix3D(const float c, const float s)
{
return Matrix3D(1.0f, 0.0f, 0.0f, 0.0f,
0.0f, c, -s, 0.0f,
0.0f, s, c, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
inline Matrix3D
PitchMatrix3D(const float theta)
{
return PitchMatrix3D((float) cos(theta), (float) sin(theta));
}
inline Matrix3D
YawMatrix3D(const float c, const float s)
{
return Matrix3D( c, 0.0f, s, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
-s, 0.0f, c, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
inline Matrix3D
YawMatrix3D(const float theta)
{
return YawMatrix3D((float) cos(theta), (float) sin(theta));
}
inline Matrix3D
RollMatrix3D(const float c, const float s)
{
return Matrix3D(c, -s, 0.0f, 0.0f,
s, c, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
inline Matrix3D
RollMatrix3D(const float theta)
{
return RollMatrix3D((float) cos(theta), (float) sin(theta));
}
template<class T>
inline T Max(T a, T b)
{
return (a>b ? a : b);
}
template<class T>
inline T Min(T a, T b)
{
return (a<b ? a : b);
}
#endif // H_MATH3D
+73
View File
@@ -0,0 +1,73 @@
/* ColDet - C++ 3D Collision Detection Library
* Copyright (C) 2000 Amir Geva
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Any comments, questions and bug reports send to:
* photon@photoneffect.com
*
* Or visit the home page: http://photoneffect.com/coldet/
*/
#include "sysdep.h"
#include "mytritri.h"
Vector3D my_tri_tri_intersect(const Triangle& t1, const Triangle& t2)
{
Plane p1(t1.v1,t1.v2,t1.v3);
int other_side=0;
{
float f1=p1.Classify(t2.v1);
float f2=p1.Classify(t2.v2);
float f3=p1.Classify(t2.v3);
float f12=f1*f2;
float f23=f2*f3;
if (f12>0.0f && f23>0.0f) return Vector3D::Zero;
other_side=(f12<0.0f?(f23<0.0f?1:0):2);
}
Plane p2(t2.v1,t2.v2,t2.v3);
Vector3D n12(p1.normal+p2.normal);
TriangleDesc td2(t2,p2);
const Vector3D& a2=td2[other_side+1];
const Vector3D& b2=td2[other_side];
const Vector3D& c2=td2[other_side+2];
float t21=-(p1.d+p2.d+a2*n12)/((b2-a2)*n12);
TriangleDesc td1(t1,p1);
Vector3D P21(a2+t21*(b2-a2));
if (td1.pointInTri(P21)) return P21;
float t22=-(p1.d+p2.d+c2*n12)/((b2-c2)*n12);
Vector3D P22(c2+t22*(b2-c2));
if (td1.pointInTri(P22)) return P22;
{
float f1=p2.Classify(t1.v1);
float f2=p2.Classify(t1.v2);
float f3=p2.Classify(t1.v3);
float f12=f1*f2;
float f23=f2*f3;
if (f12>0.0f && f23>0.0f) return Vector3D::Zero;
other_side=(f12<0.0f?(f23<0.0f?1:0):2);
}
const Vector3D& a1=td1[other_side+1];
const Vector3D& b1=td1[other_side];
const Vector3D& c1=td1[other_side+2];
float t11=-(p1.d+p2.d+a1*n12)/((b1-a1)*n12);
Vector3D P11(a1+t11*(b1-a1));
if (td2.pointInTri(P11)) return P11;
float t12=-(p1.d+p2.d+c1*n12)/((b1-c1)*n12);
Vector3D P12(c1+t12*(b1-c1));
if (td2.pointInTri(P12)) return P12;
return Vector3D::Zero;
}
+96
View File
@@ -0,0 +1,96 @@
/* ColDet - C++ 3D Collision Detection Library
* Copyright (C) 2000 Amir Geva
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Any comments, questions and bug reports send to:
* photon@photoneffect.com
*
* Or visit the home page: http://photoneffect.com/coldet/
*/
#ifndef H_MYTRITRI
#define H_MYTRITRI
#include "box.h"
/** A slower triangle-triangle intersection test, that returns the
point of intersection. */
Vector3D my_tri_tri_intersect(const Triangle& t1, const Triangle& t2);
/** Triangle description class. It is used to determine if a point
on the triangle's plane is inside the triangle. */
class TriangleDesc : public Triangle
{
public:
TriangleDesc(const Triangle& t, const Plane& p)
: Triangle(t)
{
const Vector3D& n=p.normal;
Vector3D a(flabs(n.x),flabs(n.y),flabs(n.z));
if (a.x>a.y)
{
if (a.x>a.z) { i1=1; i2=2; }
else { i1=0; i2=1; }
}
else
{
if (a.y>a.z) { i1=0; i2=2; }
else { i1=0; i2=1; }
}
}
bool pointInTri(const Vector3D& P)
{
Vector3D u(P[i1]-v1[i1],
v2[i1]-v1[i1],
v3[i1]-v1[i1]);
Vector3D v(P[i2]-v1[i2],
v2[i2]-v1[i2],
v3[i2]-v1[i2]);
float a,b;
if (u.y==0.0f)
{
b=u.x/u.z;
if (b>=0.0f && b<=1.0f) a=(v.x-b*v.z)/v.y;
else return false;
}
else
{
b=(v.x*u.y-u.x*v.y)/(v.z*u.y-u.z*v.y);
if (b>=0.0f && b<=1.0f) a=(u.x-b*u.z)/u.y;
else return false;
}
return (a>=0 && (a+b)<=1);
}
const Vector3D& operator[] (int index)
{
switch (index)
{
case 0: return v1;
case 1: return v2;
case 2: return v3;
case 3: return v1;
}
return v2;
}
int i1,i2;
};
#endif // H_MYTRITRI
+52
View File
@@ -0,0 +1,52 @@
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>ColDet - Quick Start</title>
</head>
<body>
<center>
<h1>
Quickstart:</h1></center>
<h3>
Model Setup:</h3>
For each mesh, create a collision model by using:
<pre>CollisionModel3D* model = newCollisionModel3D();</pre>
(Shared meshes can use one model)
<br>Add all the triangles the mesh has to the model by using:
<pre>model->addTriangle(vertex1,vertex2,vertex3);</pre>
Call:
<pre>model->finalize();</pre>
&nbsp;
<h3>
Collision Test:</h3>
Assuming you have two models (m1,m2), either set both of their’s transformation
<br>matrices (world matrix) by calling:
<pre>m1->setTransform(model1_transformation_matrix);</pre>
<pre>m2->setTransform(model2_transformation_matrix);</pre>
or set only one of them (in case you’re testing the model against itself,
with different
<br>transform) Then call:
<pre>m1->collision(m2);</pre>
The function returns a bool indicating if a collision has occurred.&nbsp;&nbsp;
Note that if you test a
<br>model against itself with a different transform, you need to supply
that transform as an
<br>optional parameter.
<br>&nbsp;
<h3>
Collision Test Results:</h3>
Use the getCollidingTriangles function to get which triangles have collided.&nbsp;&nbsp;
Use the
<br>getCollisionPoint function to find the exact collision point.
<br>&nbsp;
<h3>
Other Collision Tests:</h3>
You can use the rayCollision and sphereCollision functions to test the
model against
<br>these primitives.
</body>
</html>
+29
View File
@@ -0,0 +1,29 @@
ColDet - 3D Collision Detection Library
Copyright (C) 2000 Amir Geva
Description:
ColDet is a 3D collision detection library, intended for games.
It supports generic polyhedra, and even polygon soups.
Requirements:
It is written in standard C++ and can be compiled on these systems:
Windows: Visual C++ 6
Windows: Borland C++ Builder 5
Linux: g++ 2.8
Other systems that have g++ will probably compile with no modification.
The code is portable to any system with a standard C++ compliant compiler
(as compliant as they get)
Installation:
Use the supplied Visual C++ project file (coldet.dsp)
or the makefile (for systems with g++)
In other cases, just create a project/makefile and include all of the source files.
Distribution:
It is distributed under the Library GNU Public License (See the file: COPYING)
Any redistribution of the files in this package must include the entire package.
Contact Information:
Web Site: http://photoneffect.com/coldet/
email: photon@photoneffect.com
+41
View File
@@ -0,0 +1,41 @@
/* ColDet - C++ 3D Collision Detection Library
* Copyright (C) 2000 Amir Geva
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Any comments, questions and bug reports send to:
* photon@photoneffect.com
*
* Or visit the home page: http://photoneffect.com/coldet/
*/
#include "sysdep.h"
#ifdef GCC
#include <sys/time.h>
// Returns a time index in milliseconds
DWORD GetTickCount()
{
static struct timezone tz={0,0};
static const double t1=1000.0;
static const double t2=0.001;
timeval t;
gettimeofday(&t,&tz);
return long((t.tv_sec&0x000FFFFF)*t1 + t.tv_usec*t2);
}
#endif
+55
View File
@@ -0,0 +1,55 @@
/* ColDet - C++ 3D Collision Detection Library
* Copyright (C) 2000 Amir Geva
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Any comments, questions and bug reports send to:
* photon@photoneffect.com
*
* Or visit the home page: http://photoneffect.com/coldet/
*/
#ifndef H_SYSDEP
#define H_SYSDEP
#ifdef GCC
typedef unsigned long DWORD;
DWORD GetTickCount();
#define __CD__BEGIN
#define __CD__END
#elif defined(WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define __CD__BEGIN
#define __CD__END
#ifdef COLDET_EXPORTS
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif
#else
#error No system specified (WIN32 GCC)
#endif
#ifndef EXPORT
#define EXPORT
#endif
#endif // H_SYSDEP
+21
View File
@@ -0,0 +1,21 @@
The matrices used to describe model transformations are affine 4x4 matrices
which are D3D style, row major with translations in the 4th row.
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The translation vector Tx,Ty,Tz is in elements 13,14,15
If you use the transposed scheme, which is:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
With the translation vector in elements 13,14,15 you can use
this matrix without any conversion.
It is also important to note that the transformations should include
no scaling, as it disrupts the consistency of the collision model.
+292
View File
@@ -0,0 +1,292 @@
/* Triangle/triangle intersection test routine,
* by Tomas Moller, 1997.
* See article "A Fast Triangle-Triangle Intersection Test",
* Journal of Graphics Tools, 2(2), 1997
*
* int tri_tri_intersect(float V0[3],float V1[3],float V2[3],
* float U0[3],float U1[3],float U2[3])
*
* parameters: vertices of triangle 1: V0,V1,V2
* vertices of triangle 2: U0,U1,U2
* result : returns 1 if the triangles intersect, otherwise 0
*
*/
#include <math.h>
/* if USE_EPSILON_TEST is true then we do a check:
if |dv|<EPSILON then dv=0.0;
else no check is done (which is less robust)
*/
#define USE_EPSILON_TEST TRUE
#define EPSILON 0.000001
/* some macros */
#define CROSS(dest,v1,v2) \
dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
#define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
#define SUB(dest,v1,v2) \
dest[0]=v1[0]-v2[0]; \
dest[1]=v1[1]-v2[1]; \
dest[2]=v1[2]-v2[2];
/* sort so that a<=b */
#define SORT(a,b) \
if(a>b) \
{ \
float c; \
c=a; \
a=b; \
b=c; \
}
#define ISECT(VV0,VV1,VV2,D0,D1,D2,isect0,isect1) \
isect0=VV0+(VV1-VV0)*D0/(D0-D1); \
isect1=VV0+(VV2-VV0)*D0/(D0-D2);
#define COMPUTE_INTERVALS(VV0,VV1,VV2,D0,D1,D2,D0D1,D0D2,isect0,isect1) \
if(D0D1>0.0f) \
{ \
/* here we know that D0D2<=0.0 */ \
/* that is D0, D1 are on the same side, D2 on the other or on the plane */ \
ISECT(VV2,VV0,VV1,D2,D0,D1,isect0,isect1); \
} \
else if(D0D2>0.0f) \
{ \
/* here we know that d0d1<=0.0 */ \
ISECT(VV1,VV0,VV2,D1,D0,D2,isect0,isect1); \
} \
else if(D1*D2>0.0f || D0!=0.0f) \
{ \
/* here we know that d0d1<=0.0 or that D0!=0.0 */ \
ISECT(VV0,VV1,VV2,D0,D1,D2,isect0,isect1); \
} \
else if(D1!=0.0f) \
{ \
ISECT(VV1,VV0,VV2,D1,D0,D2,isect0,isect1); \
} \
else if(D2!=0.0f) \
{ \
ISECT(VV2,VV0,VV1,D2,D0,D1,isect0,isect1); \
} \
else \
{ \
/* triangles are coplanar */ \
return coplanar_tri_tri(N1,V0,V1,V2,U0,U1,U2); \
}
/* this edge to edge test is based on Franlin Antonio's gem:
"Faster Line Segment Intersection", in Graphics Gems III,
pp. 199-202 */
#define EDGE_EDGE_TEST(V0,U0,U1) \
Bx=U0[i0]-U1[i0]; \
By=U0[i1]-U1[i1]; \
Cx=V0[i0]-U0[i0]; \
Cy=V0[i1]-U0[i1]; \
f=Ay*Bx-Ax*By; \
d=By*Cx-Bx*Cy; \
if((f>0 && d>=0 && d<=f) || (f<0 && d<=0 && d>=f)) \
{ \
e=Ax*Cy-Ay*Cx; \
if(f>0) \
{ \
if(e>=0 && e<=f) return 1; \
} \
else \
{ \
if(e<=0 && e>=f) return 1; \
} \
}
#define EDGE_AGAINST_TRI_EDGES(V0,V1,U0,U1,U2) \
{ \
float Ax,Ay,Bx,By,Cx,Cy,e,d,f; \
Ax=V1[i0]-V0[i0]; \
Ay=V1[i1]-V0[i1]; \
/* test edge U0,U1 against V0,V1 */ \
EDGE_EDGE_TEST(V0,U0,U1); \
/* test edge U1,U2 against V0,V1 */ \
EDGE_EDGE_TEST(V0,U1,U2); \
/* test edge U2,U1 against V0,V1 */ \
EDGE_EDGE_TEST(V0,U2,U0); \
}
#define POINT_IN_TRI(V0,U0,U1,U2) \
{ \
float a,b,c,d0,d1,d2; \
/* is T1 completly inside T2? */ \
/* check if V0 is inside tri(U0,U1,U2) */ \
a=U1[i1]-U0[i1]; \
b=-(U1[i0]-U0[i0]); \
c=-a*U0[i0]-b*U0[i1]; \
d0=a*V0[i0]+b*V0[i1]+c; \
\
a=U2[i1]-U1[i1]; \
b=-(U2[i0]-U1[i0]); \
c=-a*U1[i0]-b*U1[i1]; \
d1=a*V0[i0]+b*V0[i1]+c; \
\
a=U0[i1]-U2[i1]; \
b=-(U0[i0]-U2[i0]); \
c=-a*U2[i0]-b*U2[i1]; \
d2=a*V0[i0]+b*V0[i1]+c; \
if(d0*d1>0.0) \
{ \
if(d0*d2>0.0) return 1; \
} \
}
int coplanar_tri_tri(float N[3],float V0[3],float V1[3],float V2[3],
float U0[3],float U1[3],float U2[3])
{
float A[3];
short i0,i1;
/* first project onto an axis-aligned plane, that maximizes the area */
/* of the triangles, compute indices: i0,i1. */
A[0]=fabs(N[0]);
A[1]=fabs(N[1]);
A[2]=fabs(N[2]);
if(A[0]>A[1])
{
if(A[0]>A[2])
{
i0=1; /* A[0] is greatest */
i1=2;
}
else
{
i0=0; /* A[2] is greatest */
i1=1;
}
}
else /* A[0]<=A[1] */
{
if(A[2]>A[1])
{
i0=0; /* A[2] is greatest */
i1=1;
}
else
{
i0=0; /* A[1] is greatest */
i1=2;
}
}
/* test all edges of triangle 1 against the edges of triangle 2 */
EDGE_AGAINST_TRI_EDGES(V0,V1,U0,U1,U2);
EDGE_AGAINST_TRI_EDGES(V1,V2,U0,U1,U2);
EDGE_AGAINST_TRI_EDGES(V2,V0,U0,U1,U2);
/* finally, test if tri1 is totally contained in tri2 or vice versa */
POINT_IN_TRI(V0,U0,U1,U2);
POINT_IN_TRI(U0,V0,V1,V2);
return 0;
}
int tri_tri_intersect(float V0[3],float V1[3],float V2[3],
float U0[3],float U1[3],float U2[3])
{
float E1[3],E2[3];
float N1[3],N2[3],d1,d2;
float du0,du1,du2,dv0,dv1,dv2;
float D[3];
float isect1[2], isect2[2];
float du0du1,du0du2,dv0dv1,dv0dv2;
short index;
float vp0,vp1,vp2;
float up0,up1,up2;
float b,c,max;
/* compute plane equation of triangle(V0,V1,V2) */
SUB(E1,V1,V0);
SUB(E2,V2,V0);
CROSS(N1,E1,E2);
d1=-DOT(N1,V0);
/* plane equation 1: N1.X+d1=0 */
/* put U0,U1,U2 into plane equation 1 to compute signed distances to the plane*/
du0=DOT(N1,U0)+d1;
du1=DOT(N1,U1)+d1;
du2=DOT(N1,U2)+d1;
/* coplanarity robustness check */
#if USE_EPSILON_TEST==TRUE
if(fabs(du0)<EPSILON) du0=0.0;
if(fabs(du1)<EPSILON) du1=0.0;
if(fabs(du2)<EPSILON) du2=0.0;
#endif
du0du1=du0*du1;
du0du2=du0*du2;
if(du0du1>0.0f && du0du2>0.0f) /* same sign on all of them + not equal 0 ? */
return 0; /* no intersection occurs */
/* compute plane of triangle (U0,U1,U2) */
SUB(E1,U1,U0);
SUB(E2,U2,U0);
CROSS(N2,E1,E2);
d2=-DOT(N2,U0);
/* plane equation 2: N2.X+d2=0 */
/* put V0,V1,V2 into plane equation 2 */
dv0=DOT(N2,V0)+d2;
dv1=DOT(N2,V1)+d2;
dv2=DOT(N2,V2)+d2;
#if USE_EPSILON_TEST==TRUE
if(fabs(dv0)<EPSILON) dv0=0.0;
if(fabs(dv1)<EPSILON) dv1=0.0;
if(fabs(dv2)<EPSILON) dv2=0.0;
#endif
dv0dv1=dv0*dv1;
dv0dv2=dv0*dv2;
if(dv0dv1>0.0f && dv0dv2>0.0f) /* same sign on all of them + not equal 0 ? */
return 0; /* no intersection occurs */
/* compute direction of intersection line */
CROSS(D,N1,N2);
/* compute and index to the largest component of D */
max=fabs(D[0]);
index=0;
b=fabs(D[1]);
c=fabs(D[2]);
if(b>max) max=b,index=1;
if(c>max) max=c,index=2;
/* this is the simplified projection onto L*/
vp0=V0[index];
vp1=V1[index];
vp2=V2[index];
up0=U0[index];
up1=U1[index];
up2=U2[index];
/* compute interval for triangle 1 */
COMPUTE_INTERVALS(vp0,vp1,vp2,dv0,dv1,dv2,dv0dv1,dv0dv2,isect1[0],isect1[1]);
/* compute interval for triangle 2 */
COMPUTE_INTERVALS(up0,up1,up2,du0,du1,du2,du0du1,du0du2,isect2[0],isect2[1]);
SORT(isect1[0],isect1[1]);
SORT(isect2[0],isect2[1]);
if(isect1[1]<isect2[0] || isect2[1]<isect1[0]) return 0;
return 1;
}