2012-11-26 18:58:24 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
|
|
|
|
Doom 3 BFG Edition GPL Source Code
|
2012-11-28 15:47:07 +00:00
|
|
|
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
|
|
|
|
|
|
|
|
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
|
|
|
|
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
===============================================================================
|
|
|
|
|
|
|
|
Trace model vs. polygonal model collision detection.
|
|
|
|
|
|
|
|
===============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma hdrstop
|
2012-12-22 15:18:19 +00:00
|
|
|
#include "precompiled.h"
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "CollisionModel_local.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
===============================================================================
|
|
|
|
|
|
|
|
Collision detection for translational motion
|
|
|
|
|
|
|
|
===============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idCollisionModelManagerLocal::TranslateEdgeThroughEdge
|
|
|
|
|
|
|
|
calculates fraction of the translation completed at which the edges collide
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE int idCollisionModelManagerLocal::TranslateEdgeThroughEdge( idVec3& cross, idPluecker& l1, idPluecker& l2, float* fraction )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
float d, t;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
/*
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
a = start of line
|
|
|
|
b = end of line
|
|
|
|
dir = movement direction
|
|
|
|
l1 = pluecker coordinate for line
|
|
|
|
l2 = pluecker coordinate for edge we might collide with
|
|
|
|
a+dir = start of line after movement
|
|
|
|
b+dir = end of line after movement
|
|
|
|
t = scale factor
|
|
|
|
solve pluecker inner product for t of line (a+t*dir : b+t*dir) and line l2
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
v[0] = (a[0]+t*dir[0]) * (b[1]+t*dir[1]) - (b[0]+t*dir[0]) * (a[1]+t*dir[1]);
|
|
|
|
v[1] = (a[0]+t*dir[0]) * (b[2]+t*dir[2]) - (b[0]+t*dir[0]) * (a[2]+t*dir[2]);
|
|
|
|
v[2] = (a[0]+t*dir[0]) - (b[0]+t*dir[0]);
|
|
|
|
v[3] = (a[1]+t*dir[1]) * (b[2]+t*dir[2]) - (b[1]+t*dir[1]) * (a[2]+t*dir[2]);
|
|
|
|
v[4] = (a[2]+t*dir[2]) - (b[2]+t*dir[2]);
|
|
|
|
v[5] = (b[1]+t*dir[1]) - (a[1]+t*dir[1]);
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
l2[0] * v[4] + l2[1] * v[5] + l2[2] * v[3] + l2[4] * v[0] + l2[5] * v[1] + l2[3] * v[2] = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
solve t
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
v[0] = (a[0]+t*dir[0]) * (b[1]+t*dir[1]) - (b[0]+t*dir[0]) * (a[1]+t*dir[1]);
|
|
|
|
v[0] = (a[0]*b[1]) + a[0]*t*dir[1] + b[1]*t*dir[0] + (t*t*dir[0]*dir[1]) -
|
|
|
|
((b[0]*a[1]) + b[0]*t*dir[1] + a[1]*t*dir[0] + (t*t*dir[0]*dir[1]));
|
|
|
|
v[0] = a[0]*b[1] + a[0]*t*dir[1] + b[1]*t*dir[0] - b[0]*a[1] - b[0]*t*dir[1] - a[1]*t*dir[0];
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
v[1] = (a[0]+t*dir[0]) * (b[2]+t*dir[2]) - (b[0]+t*dir[0]) * (a[2]+t*dir[2]);
|
|
|
|
v[1] = (a[0]*b[2]) + a[0]*t*dir[2] + b[2]*t*dir[0] + (t*t*dir[0]*dir[2]) -
|
|
|
|
((b[0]*a[2]) + b[0]*t*dir[2] + a[2]*t*dir[0] + (t*t*dir[0]*dir[2]));
|
|
|
|
v[1] = a[0]*b[2] + a[0]*t*dir[2] + b[2]*t*dir[0] - b[0]*a[2] - b[0]*t*dir[2] - a[2]*t*dir[0];
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
v[2] = (a[0]+t*dir[0]) - (b[0]+t*dir[0]);
|
|
|
|
v[2] = a[0] - b[0];
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
v[3] = (a[1]+t*dir[1]) * (b[2]+t*dir[2]) - (b[1]+t*dir[1]) * (a[2]+t*dir[2]);
|
|
|
|
v[3] = (a[1]*b[2]) + a[1]*t*dir[2] + b[2]*t*dir[1] + (t*t*dir[1]*dir[2]) -
|
|
|
|
((b[1]*a[2]) + b[1]*t*dir[2] + a[2]*t*dir[1] + (t*t*dir[1]*dir[2]));
|
|
|
|
v[3] = a[1]*b[2] + a[1]*t*dir[2] + b[2]*t*dir[1] - b[1]*a[2] - b[1]*t*dir[2] - a[2]*t*dir[1];
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
v[4] = (a[2]+t*dir[2]) - (b[2]+t*dir[2]);
|
|
|
|
v[4] = a[2] - b[2];
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
v[5] = (b[1]+t*dir[1]) - (a[1]+t*dir[1]);
|
|
|
|
v[5] = b[1] - a[1];
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
v[0] = a[0]*b[1] + a[0]*t*dir[1] + b[1]*t*dir[0] - b[0]*a[1] - b[0]*t*dir[1] - a[1]*t*dir[0];
|
|
|
|
v[1] = a[0]*b[2] + a[0]*t*dir[2] + b[2]*t*dir[0] - b[0]*a[2] - b[0]*t*dir[2] - a[2]*t*dir[0];
|
|
|
|
v[2] = a[0] - b[0];
|
|
|
|
v[3] = a[1]*b[2] + a[1]*t*dir[2] + b[2]*t*dir[1] - b[1]*a[2] - b[1]*t*dir[2] - a[2]*t*dir[1];
|
|
|
|
v[4] = a[2] - b[2];
|
|
|
|
v[5] = b[1] - a[1];
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
v[0] = (a[0]*dir[1] + b[1]*dir[0] - b[0]*dir[1] - a[1]*dir[0]) * t + a[0]*b[1] - b[0]*a[1];
|
|
|
|
v[1] = (a[0]*dir[2] + b[2]*dir[0] - b[0]*dir[2] - a[2]*dir[0]) * t + a[0]*b[2] - b[0]*a[2];
|
|
|
|
v[2] = a[0] - b[0];
|
|
|
|
v[3] = (a[1]*dir[2] + b[2]*dir[1] - b[1]*dir[2] - a[2]*dir[1]) * t + a[1]*b[2] - b[1]*a[2];
|
|
|
|
v[4] = a[2] - b[2];
|
|
|
|
v[5] = b[1] - a[1];
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
l2[4] * (a[0]*dir[1] + b[1]*dir[0] - b[0]*dir[1] - a[1]*dir[0]) * t + l2[4] * (a[0]*b[1] - b[0]*a[1])
|
|
|
|
+ l2[5] * (a[0]*dir[2] + b[2]*dir[0] - b[0]*dir[2] - a[2]*dir[0]) * t + l2[5] * (a[0]*b[2] - b[0]*a[2])
|
|
|
|
+ l2[3] * (a[0] - b[0])
|
|
|
|
+ l2[2] * (a[1]*dir[2] + b[2]*dir[1] - b[1]*dir[2] - a[2]*dir[1]) * t + l2[2] * (a[1]*b[2] - b[1]*a[2])
|
|
|
|
+ l2[0] * (a[2] - b[2])
|
|
|
|
+ l2[1] * (b[1] - a[1]) = 0
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
t = (- l2[4] * (a[0]*b[1] - b[0]*a[1]) -
|
|
|
|
l2[5] * (a[0]*b[2] - b[0]*a[2]) -
|
|
|
|
l2[3] * (a[0] - b[0]) -
|
|
|
|
l2[2] * (a[1]*b[2] - b[1]*a[2]) -
|
|
|
|
l2[0] * (a[2] - b[2]) -
|
|
|
|
l2[1] * (b[1] - a[1])) /
|
|
|
|
(l2[4] * (a[0]*dir[1] + b[1]*dir[0] - b[0]*dir[1] - a[1]*dir[0]) +
|
|
|
|
l2[5] * (a[0]*dir[2] + b[2]*dir[0] - b[0]*dir[2] - a[2]*dir[0]) +
|
|
|
|
l2[2] * (a[1]*dir[2] + b[2]*dir[1] - b[1]*dir[2] - a[2]*dir[1]));
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
d = l2[4] * (a[0]*dir[1] + b[1]*dir[0] - b[0]*dir[1] - a[1]*dir[0]) +
|
|
|
|
l2[5] * (a[0]*dir[2] + b[2]*dir[0] - b[0]*dir[2] - a[2]*dir[0]) +
|
|
|
|
l2[2] * (a[1]*dir[2] + b[2]*dir[1] - b[1]*dir[2] - a[2]*dir[1]);
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
t = - ( l2[4] * (a[0]*b[1] - b[0]*a[1]) +
|
|
|
|
l2[5] * (a[0]*b[2] - b[0]*a[2]) +
|
|
|
|
l2[3] * (a[0] - b[0]) +
|
|
|
|
l2[2] * (a[1]*b[2] - b[1]*a[2]) +
|
|
|
|
l2[0] * (a[2] - b[2]) +
|
|
|
|
l2[1] * (b[1] - a[1]));
|
|
|
|
t /= d;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
MrE pats Pluecker on the head.. good monkey
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
edgeDir = a - b;
|
|
|
|
d = l2[4] * (edgeDir[0]*dir[1] - edgeDir[1]*dir[0]) +
|
|
|
|
l2[5] * (edgeDir[0]*dir[2] - edgeDir[2]*dir[0]) +
|
|
|
|
l2[2] * (edgeDir[1]*dir[2] - edgeDir[2]*dir[1]);
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
d = l2[4] * cross[0] + l2[5] * cross[1] + l2[2] * cross[2];
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( d == 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
*fraction = 1.0f;
|
|
|
|
// no collision ever
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
t = -l1.PermutedInnerProduct( l2 );
|
|
|
|
// if the lines cross each other to begin with
|
2012-11-28 15:47:07 +00:00
|
|
|
if( fabs( t ) < idMath::FLT_SMALLEST_NON_DENORMAL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
*fraction = 0.0f;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// fraction of movement at the time the lines cross each other
|
|
|
|
*fraction = t / d;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
CM_AddContact
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void CM_AddContact( cm_traceWork_t* tw )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tw->numContacts >= tw->maxContacts )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// copy contact information from trace_t
|
|
|
|
tw->contacts[tw->numContacts] = tw->trace.c;
|
|
|
|
tw->numContacts++;
|
|
|
|
// set fraction back to 1 to find all other contacts
|
|
|
|
tw->trace.fraction = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
CM_SetVertexSidedness
|
|
|
|
|
|
|
|
stores for the given model vertex at which side of one of the trm edges it passes
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void CM_SetVertexSidedness( cm_vertex_t* v, const idPluecker& vpl, const idPluecker& epl, const int bitNum )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
const int mask = 1 << bitNum;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( ( v->sideSet & mask ) == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
const float fl = vpl.PermutedInnerProduct( epl );
|
|
|
|
v->side = ( v->side & ~mask ) | ( ( fl < 0.0f ) ? mask : 0 );
|
|
|
|
v->sideSet |= mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
CM_SetEdgeSidedness
|
|
|
|
|
|
|
|
stores for the given model edge at which side one of the trm vertices
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void CM_SetEdgeSidedness( cm_edge_t* edge, const idPluecker& vpl, const idPluecker& epl, const int bitNum )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
const int mask = 1 << bitNum;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( ( edge->sideSet & mask ) == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
const float fl = vpl.PermutedInnerProduct( epl );
|
|
|
|
edge->side = ( edge->side & ~mask ) | ( ( fl < 0.0f ) ? mask : 0 );
|
|
|
|
edge->sideSet |= mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idCollisionModelManagerLocal::TranslateTrmEdgeThroughPolygon
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idCollisionModelManagerLocal::TranslateTrmEdgeThroughPolygon( cm_traceWork_t* tw, cm_polygon_t* poly, cm_trmEdge_t* trmEdge )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i, edgeNum;
|
|
|
|
float f1, f2, dist, d1, d2;
|
|
|
|
idVec3 start, end, normal;
|
2012-11-28 15:47:07 +00:00
|
|
|
cm_edge_t* edge;
|
|
|
|
cm_vertex_t* v1, *v2;
|
|
|
|
idPluecker* pl, epsPl;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// check edges for a collision
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < poly->numEdges; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
edgeNum = poly->edges[i];
|
2012-11-28 15:47:07 +00:00
|
|
|
edge = tw->model->edges + abs( edgeNum );
|
2012-11-26 18:58:24 +00:00
|
|
|
// if this edge is already checked
|
2012-11-28 15:47:07 +00:00
|
|
|
if( edge->checkcount == idCollisionModelManagerLocal::checkCount )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// can never collide with internal edges
|
2012-11-28 15:47:07 +00:00
|
|
|
if( edge->internal )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pl = &tw->polygonEdgePlueckerCache[i];
|
|
|
|
// get the sides at which the trm edge vertices pass the polygon edge
|
|
|
|
CM_SetEdgeSidedness( edge, *pl, tw->vertices[trmEdge->vertexNum[0]].pl, trmEdge->vertexNum[0] );
|
|
|
|
CM_SetEdgeSidedness( edge, *pl, tw->vertices[trmEdge->vertexNum[1]].pl, trmEdge->vertexNum[1] );
|
|
|
|
// if the trm edge start and end vertex do not pass the polygon edge at different sides
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !( ( ( edge->side >> trmEdge->vertexNum[0] ) ^ ( edge->side >> trmEdge->vertexNum[1] ) ) & 1 ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// get the sides at which the polygon edge vertices pass the trm edge
|
|
|
|
v1 = tw->model->vertices + edge->vertexNum[INT32_SIGNBITSET( edgeNum )];
|
|
|
|
CM_SetVertexSidedness( v1, tw->polygonVertexPlueckerCache[i], trmEdge->pl, trmEdge->bitNum );
|
|
|
|
v2 = tw->model->vertices + edge->vertexNum[INT32_SIGNBITNOTSET( edgeNum )];
|
2012-11-28 15:47:07 +00:00
|
|
|
CM_SetVertexSidedness( v2, tw->polygonVertexPlueckerCache[i + 1], trmEdge->pl, trmEdge->bitNum );
|
2012-11-26 18:58:24 +00:00
|
|
|
// if the polygon edge start and end vertex do not pass the trm edge at different sides
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !( ( v1->side ^ v2->side ) & ( 1 << trmEdge->bitNum ) ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// if there is no possible collision between the trm edge and the polygon edge
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !idCollisionModelManagerLocal::TranslateEdgeThroughEdge( trmEdge->cross, trmEdge->pl, *pl, &f1 ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// if moving away from edge
|
2012-11-28 15:47:07 +00:00
|
|
|
if( f1 < 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// pluecker coordinate for epsilon expanded edge
|
|
|
|
epsPl.FromLine( tw->model->vertices[edge->vertexNum[0]].p + edge->normal * CM_CLIP_EPSILON,
|
|
|
|
tw->model->vertices[edge->vertexNum[1]].p + edge->normal * CM_CLIP_EPSILON );
|
|
|
|
// calculate collision fraction with epsilon expanded edge
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !idCollisionModelManagerLocal::TranslateEdgeThroughEdge( trmEdge->cross, trmEdge->pl, epsPl, &f2 ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// if no collision with epsilon edge or moving away from edge
|
2012-11-28 15:47:07 +00:00
|
|
|
if( f2 > 1.0f || f1 < f2 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( f2 < 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
f2 = 0.0f;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( f2 < tw->trace.fraction )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw->trace.fraction = f2;
|
|
|
|
// create plane with normal vector orthogonal to both the polygon edge and the trm edge
|
|
|
|
start = tw->model->vertices[edge->vertexNum[0]].p;
|
|
|
|
end = tw->model->vertices[edge->vertexNum[1]].p;
|
|
|
|
tw->trace.c.normal = ( end - start ).Cross( trmEdge->end - trmEdge->start );
|
|
|
|
// FIXME: do this normalize when we know the first collision
|
|
|
|
tw->trace.c.normal.Normalize();
|
|
|
|
tw->trace.c.dist = tw->trace.c.normal * start;
|
|
|
|
// make sure the collision plane faces the trace model
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tw->trace.c.normal * trmEdge->start - tw->trace.c.dist < 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw->trace.c.normal = -tw->trace.c.normal;
|
|
|
|
tw->trace.c.dist = -tw->trace.c.dist;
|
|
|
|
}
|
|
|
|
tw->trace.c.contents = poly->contents;
|
|
|
|
tw->trace.c.material = poly->material;
|
|
|
|
tw->trace.c.type = CONTACT_EDGE;
|
|
|
|
tw->trace.c.modelFeature = edgeNum;
|
|
|
|
tw->trace.c.trmFeature = trmEdge - tw->edges;
|
|
|
|
// calculate collision point
|
|
|
|
normal[0] = trmEdge->cross[2];
|
|
|
|
normal[1] = -trmEdge->cross[1];
|
|
|
|
normal[2] = trmEdge->cross[0];
|
|
|
|
dist = normal * trmEdge->start;
|
|
|
|
d1 = normal * start - dist;
|
|
|
|
d2 = normal * end - dist;
|
|
|
|
f1 = d1 / ( d1 - d2 );
|
|
|
|
//assert( f1 >= 0.0f && f1 <= 1.0f );
|
|
|
|
tw->trace.c.point = start + f1 * ( end - start );
|
|
|
|
// if retrieving contacts
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tw->getContacts )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
CM_AddContact( tw );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
CM_TranslationPlaneFraction
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
float CM_TranslationPlaneFraction( const idPlane& plane, const idVec3& start, const idVec3& end )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
const float d2 = plane.Distance( end );
|
|
|
|
// if the end point is closer to the plane than an epsilon we still take it for a collision
|
2012-11-28 15:47:07 +00:00
|
|
|
if( d2 >= CM_CLIP_EPSILON )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
const float d1 = plane.Distance( start );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if completely behind the polygon
|
2012-11-28 15:47:07 +00:00
|
|
|
if( d1 <= 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
// leaves polygon
|
2012-11-28 15:47:07 +00:00
|
|
|
if( d1 - d2 < idMath::FLT_SMALLEST_NON_DENORMAL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
return ( d1 - CM_CLIP_EPSILON ) / ( d1 - d2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idCollisionModelManagerLocal::TranslateTrmVertexThroughPolygon
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idCollisionModelManagerLocal::TranslateTrmVertexThroughPolygon( cm_traceWork_t* tw, cm_polygon_t* poly, cm_trmVertex_t* v, int bitNum )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i, edgeNum;
|
|
|
|
float f;
|
2012-11-28 15:47:07 +00:00
|
|
|
cm_edge_t* edge;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
f = CM_TranslationPlaneFraction( poly->plane, v->p, v->endp );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( f < tw->trace.fraction )
|
|
|
|
{
|
|
|
|
|
|
|
|
for( i = 0; i < poly->numEdges; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
edgeNum = poly->edges[i];
|
2012-11-28 15:47:07 +00:00
|
|
|
edge = tw->model->edges + abs( edgeNum );
|
2012-11-26 18:58:24 +00:00
|
|
|
CM_SetEdgeSidedness( edge, tw->polygonEdgePlueckerCache[i], v->pl, bitNum );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( INT32_SIGNBITSET( edgeNum ) ^ ( ( edge->side >> bitNum ) & 1 ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( f < 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
f = 0.0f;
|
|
|
|
}
|
|
|
|
tw->trace.fraction = f;
|
|
|
|
// collision plane is the polygon plane
|
|
|
|
tw->trace.c.normal = poly->plane.Normal();
|
|
|
|
tw->trace.c.dist = poly->plane.Dist();
|
|
|
|
tw->trace.c.contents = poly->contents;
|
|
|
|
tw->trace.c.material = poly->material;
|
|
|
|
tw->trace.c.type = CONTACT_TRMVERTEX;
|
2012-11-28 15:47:07 +00:00
|
|
|
tw->trace.c.modelFeature = *reinterpret_cast<int*>( &poly );
|
2012-11-26 18:58:24 +00:00
|
|
|
tw->trace.c.trmFeature = v - tw->vertices;
|
|
|
|
tw->trace.c.point = v->p + tw->trace.fraction * ( v->endp - v->p );
|
|
|
|
// if retrieving contacts
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tw->getContacts )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
CM_AddContact( tw );
|
|
|
|
// no need to store the trm vertex more than once as a contact
|
|
|
|
v->used = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idCollisionModelManagerLocal::TranslatePointThroughPolygon
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idCollisionModelManagerLocal::TranslatePointThroughPolygon( cm_traceWork_t* tw, cm_polygon_t* poly, cm_trmVertex_t* v )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i, edgeNum;
|
|
|
|
float f;
|
2012-11-28 15:47:07 +00:00
|
|
|
cm_edge_t* edge;
|
2012-11-26 18:58:24 +00:00
|
|
|
idPluecker pl;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
f = CM_TranslationPlaneFraction( poly->plane, v->p, v->endp );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( f < tw->trace.fraction )
|
|
|
|
{
|
|
|
|
|
|
|
|
for( i = 0; i < poly->numEdges; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
edgeNum = poly->edges[i];
|
2012-11-28 15:47:07 +00:00
|
|
|
edge = tw->model->edges + abs( edgeNum );
|
2012-11-26 18:58:24 +00:00
|
|
|
// if we didn't yet calculate the sidedness for this edge
|
2012-11-28 15:47:07 +00:00
|
|
|
if( edge->checkcount != idCollisionModelManagerLocal::checkCount )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
float fl;
|
|
|
|
edge->checkcount = idCollisionModelManagerLocal::checkCount;
|
2012-11-28 15:47:07 +00:00
|
|
|
pl.FromLine( tw->model->vertices[edge->vertexNum[0]].p, tw->model->vertices[edge->vertexNum[1]].p );
|
2012-11-26 18:58:24 +00:00
|
|
|
fl = v->pl.PermutedInnerProduct( pl );
|
|
|
|
edge->side = ( fl < 0.0f );
|
|
|
|
}
|
|
|
|
// if the point passes the edge at the wrong side
|
|
|
|
//if ( (edgeNum > 0) == edge->side ) {
|
2012-11-28 15:47:07 +00:00
|
|
|
if( INT32_SIGNBITSET( edgeNum ) ^ edge->side )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( f < 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
f = 0.0f;
|
|
|
|
}
|
|
|
|
tw->trace.fraction = f;
|
|
|
|
// collision plane is the polygon plane
|
|
|
|
tw->trace.c.normal = poly->plane.Normal();
|
|
|
|
tw->trace.c.dist = poly->plane.Dist();
|
|
|
|
tw->trace.c.contents = poly->contents;
|
|
|
|
tw->trace.c.material = poly->material;
|
|
|
|
tw->trace.c.type = CONTACT_TRMVERTEX;
|
2012-11-28 15:47:07 +00:00
|
|
|
tw->trace.c.modelFeature = *reinterpret_cast<int*>( &poly );
|
2012-11-26 18:58:24 +00:00
|
|
|
tw->trace.c.trmFeature = v - tw->vertices;
|
|
|
|
tw->trace.c.point = v->p + tw->trace.fraction * ( v->endp - v->p );
|
|
|
|
// if retrieving contacts
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tw->getContacts )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
CM_AddContact( tw );
|
|
|
|
// no need to store the trm vertex more than once as a contact
|
|
|
|
v->used = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idCollisionModelManagerLocal::TranslateVertexThroughTrmPolygon
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idCollisionModelManagerLocal::TranslateVertexThroughTrmPolygon( cm_traceWork_t* tw, cm_trmPolygon_t* trmpoly, cm_polygon_t* poly, cm_vertex_t* v, idVec3& endp, idPluecker& pl )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i, edgeNum;
|
|
|
|
float f;
|
2012-11-28 15:47:07 +00:00
|
|
|
cm_trmEdge_t* edge;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
f = CM_TranslationPlaneFraction( trmpoly->plane, v->p, endp );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( f < tw->trace.fraction )
|
|
|
|
{
|
|
|
|
|
|
|
|
for( i = 0; i < trmpoly->numEdges; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
edgeNum = trmpoly->edges[i];
|
2012-11-28 15:47:07 +00:00
|
|
|
edge = tw->edges + abs( edgeNum );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
CM_SetVertexSidedness( v, pl, edge->pl, edge->bitNum );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( INT32_SIGNBITSET( edgeNum ) ^ ( ( v->side >> edge->bitNum ) & 1 ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( f < 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
f = 0.0f;
|
|
|
|
}
|
|
|
|
tw->trace.fraction = f;
|
|
|
|
// collision plane is the inverse trm polygon plane
|
|
|
|
tw->trace.c.normal = -trmpoly->plane.Normal();
|
|
|
|
tw->trace.c.dist = -trmpoly->plane.Dist();
|
|
|
|
tw->trace.c.contents = poly->contents;
|
|
|
|
tw->trace.c.material = poly->material;
|
|
|
|
tw->trace.c.type = CONTACT_MODELVERTEX;
|
|
|
|
tw->trace.c.modelFeature = v - tw->model->vertices;
|
|
|
|
tw->trace.c.trmFeature = trmpoly - tw->polys;
|
|
|
|
tw->trace.c.point = v->p + tw->trace.fraction * ( endp - v->p );
|
|
|
|
// if retrieving contacts
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tw->getContacts )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
CM_AddContact( tw );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idCollisionModelManagerLocal::TranslateTrmThroughPolygon
|
|
|
|
|
|
|
|
returns true if the polygon blocks the complete translation
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idCollisionModelManagerLocal::TranslateTrmThroughPolygon( cm_traceWork_t* tw, cm_polygon_t* p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i, j, k, edgeNum;
|
|
|
|
float fraction, d;
|
|
|
|
idVec3 endp;
|
2012-11-28 15:47:07 +00:00
|
|
|
idPluecker* pl;
|
|
|
|
cm_trmVertex_t* bv;
|
|
|
|
cm_trmEdge_t* be;
|
|
|
|
cm_trmPolygon_t* bp;
|
|
|
|
cm_vertex_t* v;
|
|
|
|
cm_edge_t* e;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if already checked this polygon
|
2012-11-28 15:47:07 +00:00
|
|
|
if( p->checkcount == idCollisionModelManagerLocal::checkCount )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
p->checkcount = idCollisionModelManagerLocal::checkCount;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if this polygon does not have the right contents behind it
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !( p->contents & tw->contents ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if the the trace bounds do not intersect the polygon bounds
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !tw->bounds.IntersectsBounds( p->bounds ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// only collide with the polygon if approaching at the front
|
2012-11-28 15:47:07 +00:00
|
|
|
if( ( p->plane.Normal() * tw->dir ) > 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if the polygon is too far from the first heart plane
|
|
|
|
d = p->bounds.PlaneDistance( tw->heartPlane1 );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( idMath::Fabs( d ) > tw->maxDistFromHeartPlane1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if the polygon is too far from the second heart plane
|
|
|
|
d = p->bounds.PlaneDistance( tw->heartPlane2 );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( idMath::Fabs( d ) > tw->maxDistFromHeartPlane2 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fraction = tw->trace.fraction;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// fast point trace
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tw->pointTrace )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idCollisionModelManagerLocal::TranslatePointThroughPolygon( tw, p, &tw->vertices[0] );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// trace bounds should cross polygon plane
|
2012-11-28 15:47:07 +00:00
|
|
|
switch( tw->bounds.PlaneSide( p->plane ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
case PLANESIDE_CROSS:
|
|
|
|
break;
|
|
|
|
case PLANESIDE_FRONT:
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tw->model->isConvex )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw->quickExit = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// calculate pluecker coordinates for the polygon edges and polygon vertices
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < p->numEdges; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
edgeNum = p->edges[i];
|
2012-11-28 15:47:07 +00:00
|
|
|
e = tw->model->edges + abs( edgeNum );
|
2012-11-26 18:58:24 +00:00
|
|
|
// reset sidedness cache if this is the first time we encounter this edge during this trace
|
2012-11-28 15:47:07 +00:00
|
|
|
if( e->checkcount != idCollisionModelManagerLocal::checkCount )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
e->sideSet = 0;
|
|
|
|
}
|
|
|
|
// pluecker coordinate for edge
|
|
|
|
tw->polygonEdgePlueckerCache[i].FromLine( tw->model->vertices[e->vertexNum[0]].p,
|
2012-11-28 15:47:07 +00:00
|
|
|
tw->model->vertices[e->vertexNum[1]].p );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
v = &tw->model->vertices[e->vertexNum[INT32_SIGNBITSET( edgeNum )]];
|
|
|
|
// reset sidedness cache if this is the first time we encounter this vertex during this trace
|
2012-11-28 15:47:07 +00:00
|
|
|
if( v->checkcount != idCollisionModelManagerLocal::checkCount )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
v->sideSet = 0;
|
|
|
|
}
|
|
|
|
// pluecker coordinate for vertex movement vector
|
|
|
|
tw->polygonVertexPlueckerCache[i].FromRay( v->p, -tw->dir );
|
|
|
|
}
|
|
|
|
// copy first to last so we can easily cycle through for the edges
|
|
|
|
tw->polygonVertexPlueckerCache[p->numEdges] = tw->polygonVertexPlueckerCache[0];
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// trace trm vertices through polygon
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < tw->numVerts; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
bv = tw->vertices + i;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( bv->used )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idCollisionModelManagerLocal::TranslateTrmVertexThroughPolygon( tw, p, bv, i );
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// trace trm edges through polygon
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 1; i <= tw->numEdges; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
be = tw->edges + i;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( be->used )
|
|
|
|
{
|
|
|
|
idCollisionModelManagerLocal::TranslateTrmEdgeThroughPolygon( tw, p, be );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// trace all polygon vertices through the trm
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < p->numEdges; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
edgeNum = p->edges[i];
|
2012-11-28 15:47:07 +00:00
|
|
|
e = tw->model->edges + abs( edgeNum );
|
|
|
|
|
|
|
|
if( e->checkcount == idCollisionModelManagerLocal::checkCount )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// set edge check count
|
|
|
|
e->checkcount = idCollisionModelManagerLocal::checkCount;
|
|
|
|
// can never collide with internal edges
|
2012-11-28 15:47:07 +00:00
|
|
|
if( e->internal )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// got to check both vertices because we skip internal edges
|
2012-11-28 15:47:07 +00:00
|
|
|
for( k = 0; k < 2; k++ )
|
|
|
|
{
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
v = tw->model->vertices + e->vertexNum[k ^ INT32_SIGNBITSET( edgeNum )];
|
|
|
|
// if this vertex is already checked
|
2012-11-28 15:47:07 +00:00
|
|
|
if( v->checkcount == idCollisionModelManagerLocal::checkCount )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// set vertex check count
|
|
|
|
v->checkcount = idCollisionModelManagerLocal::checkCount;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if the vertex is outside the trace bounds
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !tw->bounds.ContainsPoint( v->p ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// vertex end point after movement
|
|
|
|
endp = v->p - tw->dir;
|
|
|
|
// pluecker coordinate for vertex movement vector
|
2012-11-28 15:47:07 +00:00
|
|
|
pl = &tw->polygonVertexPlueckerCache[i + k];
|
|
|
|
|
|
|
|
for( j = 0; j < tw->numPolys; j++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
bp = tw->polys + j;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( bp->used )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idCollisionModelManagerLocal::TranslateVertexThroughTrmPolygon( tw, bp, p, v, endp, *pl );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if there was a collision with this polygon and we are not retrieving contacts
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tw->trace.fraction < fraction && !tw->getContacts )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
fraction = tw->trace.fraction;
|
|
|
|
endp = tw->start + fraction * tw->dir;
|
|
|
|
// decrease bounds
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < 3; i++ )
|
|
|
|
{
|
|
|
|
if( tw->start[i] < endp[i] )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw->bounds[0][i] = tw->start[i] + tw->size[0][i] - CM_BOX_EPSILON;
|
|
|
|
tw->bounds[1][i] = endp[i] + tw->size[1][i] + CM_BOX_EPSILON;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw->bounds[0][i] = endp[i] + tw->size[0][i] - CM_BOX_EPSILON;
|
|
|
|
tw->bounds[1][i] = tw->start[i] + tw->size[1][i] + CM_BOX_EPSILON;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return ( tw->trace.fraction == 0.0f );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idCollisionModelManagerLocal::SetupTrm
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idCollisionModelManagerLocal::SetupTrm( cm_traceWork_t* tw, const idTraceModel* trm )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i, j;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// vertices
|
|
|
|
tw->numVerts = trm->numVerts;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < trm->numVerts; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw->vertices[i].p = trm->verts[i];
|
|
|
|
tw->vertices[i].used = false;
|
|
|
|
}
|
|
|
|
// edges
|
|
|
|
tw->numEdges = trm->numEdges;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 1; i <= trm->numEdges; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw->edges[i].vertexNum[0] = trm->edges[i].v[0];
|
|
|
|
tw->edges[i].vertexNum[1] = trm->edges[i].v[1];
|
|
|
|
tw->edges[i].used = false;
|
|
|
|
}
|
|
|
|
// polygons
|
|
|
|
tw->numPolys = trm->numPolys;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < trm->numPolys; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw->polys[i].numEdges = trm->polys[i].numEdges;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( j = 0; j < trm->polys[i].numEdges; j++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw->polys[i].edges[j] = trm->polys[i].edges[j];
|
|
|
|
}
|
|
|
|
tw->polys[i].plane.SetNormal( trm->polys[i].normal );
|
|
|
|
tw->polys[i].used = false;
|
|
|
|
}
|
|
|
|
// is the trace model convex or not
|
|
|
|
tw->isConvex = trm->isConvex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idCollisionModelManagerLocal::SetupTranslationHeartPlanes
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idCollisionModelManagerLocal::SetupTranslationHeartPlanes( cm_traceWork_t* tw )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idVec3 dir, normal1, normal2;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// calculate trace heart planes
|
|
|
|
dir = tw->dir;
|
|
|
|
dir.Normalize();
|
|
|
|
dir.NormalVectors( normal1, normal2 );
|
|
|
|
tw->heartPlane1.SetNormal( normal1 );
|
|
|
|
tw->heartPlane1.FitThroughPoint( tw->start );
|
|
|
|
tw->heartPlane2.SetNormal( normal2 );
|
|
|
|
tw->heartPlane2.FitThroughPoint( tw->start );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idCollisionModelManagerLocal::Translation
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
#ifdef _DEBUG
|
|
|
|
static int entered = 0;
|
|
|
|
#endif
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void idCollisionModelManagerLocal::Translation( trace_t* results, const idVec3& start, const idVec3& end,
|
|
|
|
const idTraceModel* trm, const idMat3& trmAxis, int contentMask,
|
|
|
|
cmHandle_t model, const idVec3& modelOrigin, const idMat3& modelAxis )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
int i, j;
|
|
|
|
float dist;
|
|
|
|
bool model_rotated, trm_rotated;
|
|
|
|
idVec3 dir1, dir2, dir;
|
|
|
|
idMat3 invModelAxis, tmpAxis;
|
2012-11-28 15:47:07 +00:00
|
|
|
cm_trmPolygon_t* poly;
|
|
|
|
cm_trmEdge_t* edge;
|
|
|
|
cm_trmVertex_t* vert;
|
2012-11-26 18:58:24 +00:00
|
|
|
ALIGN16( static cm_traceWork_t tw );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
assert( ( ( byte* )&start ) < ( ( byte* )results ) || ( ( byte* )&start ) >= ( ( ( byte* )results ) + sizeof( trace_t ) ) );
|
|
|
|
assert( ( ( byte* )&end ) < ( ( byte* )results ) || ( ( byte* )&end ) >= ( ( ( byte* )results ) + sizeof( trace_t ) ) );
|
|
|
|
assert( ( ( byte* )&trmAxis ) < ( ( byte* )results ) || ( ( byte* )&trmAxis ) >= ( ( ( byte* )results ) + sizeof( trace_t ) ) );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
memset( results, 0, sizeof( *results ) );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( model < 0 || model > MAX_SUBMODELS || model > idCollisionModelManagerLocal::maxModels )
|
|
|
|
{
|
|
|
|
common->Printf( "idCollisionModelManagerLocal::Translation: invalid model handle\n" );
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !idCollisionModelManagerLocal::models[model] )
|
|
|
|
{
|
|
|
|
common->Printf( "idCollisionModelManagerLocal::Translation: invalid model\n" );
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if case special position test
|
2012-11-28 15:47:07 +00:00
|
|
|
if( start[0] == end[0] && start[1] == end[1] && start[2] == end[2] )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idCollisionModelManagerLocal::ContentsTrm( results, start, trm, trmAxis, contentMask, model, modelOrigin, modelAxis );
|
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
bool startsolid = false;
|
|
|
|
// test whether or not stuck to begin with
|
2012-11-28 15:47:07 +00:00
|
|
|
if( cm_debugCollision.GetBool() )
|
|
|
|
{
|
|
|
|
if( !entered && !idCollisionModelManagerLocal::getContacts )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
entered = 1;
|
|
|
|
// if already messed up to begin with
|
2012-11-28 15:47:07 +00:00
|
|
|
if( idCollisionModelManagerLocal::Contents( start, trm, trmAxis, -1, model, modelOrigin, modelAxis ) & contentMask )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
startsolid = true;
|
|
|
|
}
|
|
|
|
entered = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idCollisionModelManagerLocal::checkCount++;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
tw.trace.fraction = 1.0f;
|
|
|
|
tw.trace.c.contents = 0;
|
|
|
|
tw.trace.c.type = CONTACT_NONE;
|
|
|
|
tw.contents = contentMask;
|
|
|
|
tw.isConvex = true;
|
|
|
|
tw.rotation = false;
|
|
|
|
tw.positionTest = false;
|
|
|
|
tw.quickExit = false;
|
|
|
|
tw.getContacts = idCollisionModelManagerLocal::getContacts;
|
|
|
|
tw.contacts = idCollisionModelManagerLocal::contacts;
|
|
|
|
tw.maxContacts = idCollisionModelManagerLocal::maxContacts;
|
|
|
|
tw.numContacts = 0;
|
|
|
|
tw.model = idCollisionModelManagerLocal::models[model];
|
|
|
|
tw.start = start - modelOrigin;
|
|
|
|
tw.end = end - modelOrigin;
|
|
|
|
tw.dir = end - start;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
model_rotated = modelAxis.IsRotated();
|
2012-11-28 15:47:07 +00:00
|
|
|
if( model_rotated )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
invModelAxis = modelAxis.Transpose();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if optimized point trace
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !trm || ( trm->bounds[1][0] - trm->bounds[0][0] <= 0.0f &&
|
|
|
|
trm->bounds[1][1] - trm->bounds[0][1] <= 0.0f &&
|
|
|
|
trm->bounds[1][2] - trm->bounds[0][2] <= 0.0f ) )
|
|
|
|
{
|
|
|
|
|
|
|
|
if( model_rotated )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// rotate trace instead of model
|
|
|
|
tw.start *= invModelAxis;
|
|
|
|
tw.end *= invModelAxis;
|
|
|
|
tw.dir *= invModelAxis;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// trace bounds
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < 3; i++ )
|
|
|
|
{
|
|
|
|
if( tw.start[i] < tw.end[i] )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw.bounds[0][i] = tw.start[i] - CM_BOX_EPSILON;
|
|
|
|
tw.bounds[1][i] = tw.end[i] + CM_BOX_EPSILON;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw.bounds[0][i] = tw.end[i] - CM_BOX_EPSILON;
|
|
|
|
tw.bounds[1][i] = tw.start[i] + CM_BOX_EPSILON;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tw.extents[0] = tw.extents[1] = tw.extents[2] = CM_BOX_EPSILON;
|
|
|
|
tw.size.Zero();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// setup trace heart planes
|
|
|
|
idCollisionModelManagerLocal::SetupTranslationHeartPlanes( &tw );
|
|
|
|
tw.maxDistFromHeartPlane1 = CM_BOX_EPSILON;
|
|
|
|
tw.maxDistFromHeartPlane2 = CM_BOX_EPSILON;
|
|
|
|
// collision with single point
|
|
|
|
tw.numVerts = 1;
|
|
|
|
tw.vertices[0].p = tw.start;
|
|
|
|
tw.vertices[0].endp = tw.vertices[0].p + tw.dir;
|
|
|
|
tw.vertices[0].pl.FromRay( tw.vertices[0].p, tw.dir );
|
|
|
|
tw.numEdges = tw.numPolys = 0;
|
|
|
|
tw.pointTrace = true;
|
|
|
|
// trace through the model
|
|
|
|
idCollisionModelManagerLocal::TraceThroughModel( &tw );
|
|
|
|
// store results
|
|
|
|
*results = tw.trace;
|
2012-11-28 15:47:07 +00:00
|
|
|
results->endpos = start + results->fraction * ( end - start );
|
2012-11-26 18:58:24 +00:00
|
|
|
results->endAxis = mat3_identity;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( results->fraction < 1.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// rotate trace plane normal if there was a collision with a rotated model
|
2012-11-28 15:47:07 +00:00
|
|
|
if( model_rotated )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
results->c.normal *= modelAxis;
|
|
|
|
results->c.point *= modelAxis;
|
|
|
|
}
|
|
|
|
results->c.point += modelOrigin;
|
|
|
|
results->c.dist += modelOrigin * results->c.normal;
|
|
|
|
}
|
|
|
|
idCollisionModelManagerLocal::numContacts = tw.numContacts;
|
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// the trace fraction is too inaccurate to describe translations over huge distances
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tw.dir.LengthSqr() > Square( CM_MAX_TRACE_DIST ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
results->fraction = 0.0f;
|
|
|
|
results->endpos = start;
|
|
|
|
results->endAxis = trmAxis;
|
|
|
|
results->c.normal = vec3_origin;
|
|
|
|
results->c.material = NULL;
|
|
|
|
results->c.point = start;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( common->RW() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
common->RW()->DebugArrow( colorRed, start, end, 1 );
|
|
|
|
}
|
|
|
|
common->Printf( "idCollisionModelManagerLocal::Translation: huge translation\n" );
|
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
tw.pointTrace = false;
|
|
|
|
tw.size.Clear();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// setup trm structure
|
|
|
|
idCollisionModelManagerLocal::SetupTrm( &tw, trm );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
trm_rotated = trmAxis.IsRotated();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// calculate vertex positions
|
2012-11-28 15:47:07 +00:00
|
|
|
if( trm_rotated )
|
|
|
|
{
|
|
|
|
for( i = 0; i < tw.numVerts; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// rotate trm around the start position
|
|
|
|
tw.vertices[i].p *= trmAxis;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < tw.numVerts; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// set trm at start position
|
|
|
|
tw.vertices[i].p += tw.start;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( model_rotated )
|
|
|
|
{
|
|
|
|
for( i = 0; i < tw.numVerts; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// rotate trm around model instead of rotating the model
|
|
|
|
tw.vertices[i].p *= invModelAxis;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// add offset to start point
|
2012-11-28 15:47:07 +00:00
|
|
|
if( trm_rotated )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
dir = trm->offset * trmAxis;
|
|
|
|
tw.start += dir;
|
|
|
|
tw.end += dir;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw.start += trm->offset;
|
|
|
|
tw.end += trm->offset;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( model_rotated )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// rotate trace instead of model
|
|
|
|
tw.start *= invModelAxis;
|
|
|
|
tw.end *= invModelAxis;
|
|
|
|
tw.dir *= invModelAxis;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// rotate trm polygon planes
|
2012-11-28 15:47:07 +00:00
|
|
|
if( trm_rotated & model_rotated )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tmpAxis = trmAxis * invModelAxis;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( poly = tw.polys, i = 0; i < tw.numPolys; i++, poly++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
poly->plane *= tmpAxis;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( trm_rotated )
|
|
|
|
{
|
|
|
|
for( poly = tw.polys, i = 0; i < tw.numPolys; i++, poly++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
poly->plane *= trmAxis;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( model_rotated )
|
|
|
|
{
|
|
|
|
for( poly = tw.polys, i = 0; i < tw.numPolys; i++, poly++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
poly->plane *= invModelAxis;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// setup trm polygons
|
2012-11-28 15:47:07 +00:00
|
|
|
for( poly = tw.polys, i = 0; i < tw.numPolys; i++, poly++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// if the trm poly plane is facing in the movement direction
|
|
|
|
dist = poly->plane.Normal() * tw.dir;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( dist > 0.0f || ( !trm->isConvex && dist == 0.0f ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// this trm poly and it's edges and vertices need to be used for collision
|
|
|
|
poly->used = true;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( j = 0; j < poly->numEdges; j++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
edge = &tw.edges[abs( poly->edges[j] )];
|
|
|
|
edge->used = true;
|
|
|
|
tw.vertices[edge->vertexNum[0]].used = true;
|
|
|
|
tw.vertices[edge->vertexNum[1]].used = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// setup trm vertices
|
2012-11-28 15:47:07 +00:00
|
|
|
for( vert = tw.vertices, i = 0; i < tw.numVerts; i++, vert++ )
|
|
|
|
{
|
|
|
|
if( !vert->used )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// get axial trm size after rotations
|
|
|
|
tw.size.AddPoint( vert->p - tw.start );
|
|
|
|
// calculate the end position of each vertex for a full trace
|
|
|
|
vert->endp = vert->p + tw.dir;
|
|
|
|
// pluecker coordinate for vertex movement line
|
|
|
|
vert->pl.FromRay( vert->p, tw.dir );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// setup trm edges
|
2012-11-28 15:47:07 +00:00
|
|
|
for( edge = tw.edges + 1, i = 1; i <= tw.numEdges; i++, edge++ )
|
|
|
|
{
|
|
|
|
if( !edge->used )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// edge start, end and pluecker coordinate
|
|
|
|
edge->start = tw.vertices[edge->vertexNum[0]].p;
|
|
|
|
edge->end = tw.vertices[edge->vertexNum[1]].p;
|
|
|
|
edge->pl.FromLine( edge->start, edge->end );
|
|
|
|
// calculate normal of plane through movement plane created by the edge
|
|
|
|
dir = edge->start - edge->end;
|
|
|
|
edge->cross[0] = dir[0] * tw.dir[1] - dir[1] * tw.dir[0];
|
|
|
|
edge->cross[1] = dir[0] * tw.dir[2] - dir[2] * tw.dir[0];
|
|
|
|
edge->cross[2] = dir[1] * tw.dir[2] - dir[2] * tw.dir[1];
|
|
|
|
// bit for vertex sidedness bit cache
|
|
|
|
edge->bitNum = i;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// set trm plane distances
|
2012-11-28 15:47:07 +00:00
|
|
|
for( poly = tw.polys, i = 0; i < tw.numPolys; i++, poly++ )
|
|
|
|
{
|
|
|
|
if( poly->used )
|
|
|
|
{
|
|
|
|
poly->plane.FitThroughPoint( tw.edges[abs( poly->edges[0] )].start );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// bounds for full trace, a little bit larger for epsilons
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < 3; i++ )
|
|
|
|
{
|
|
|
|
if( tw.start[i] < tw.end[i] )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw.bounds[0][i] = tw.start[i] + tw.size[0][i] - CM_BOX_EPSILON;
|
|
|
|
tw.bounds[1][i] = tw.end[i] + tw.size[1][i] + CM_BOX_EPSILON;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw.bounds[0][i] = tw.end[i] + tw.size[0][i] - CM_BOX_EPSILON;
|
|
|
|
tw.bounds[1][i] = tw.start[i] + tw.size[1][i] + CM_BOX_EPSILON;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( idMath::Fabs( tw.size[0][i] ) > idMath::Fabs( tw.size[1][i] ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw.extents[i] = idMath::Fabs( tw.size[0][i] ) + CM_BOX_EPSILON;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw.extents[i] = idMath::Fabs( tw.size[1][i] ) + CM_BOX_EPSILON;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// setup trace heart planes
|
|
|
|
idCollisionModelManagerLocal::SetupTranslationHeartPlanes( &tw );
|
|
|
|
tw.maxDistFromHeartPlane1 = 0;
|
|
|
|
tw.maxDistFromHeartPlane2 = 0;
|
|
|
|
// calculate maximum trm vertex distance from both heart planes
|
2012-11-28 15:47:07 +00:00
|
|
|
for( vert = tw.vertices, i = 0; i < tw.numVerts; i++, vert++ )
|
|
|
|
{
|
|
|
|
if( !vert->used )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dist = idMath::Fabs( tw.heartPlane1.Distance( vert->p ) );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( dist > tw.maxDistFromHeartPlane1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw.maxDistFromHeartPlane1 = dist;
|
|
|
|
}
|
|
|
|
dist = idMath::Fabs( tw.heartPlane2.Distance( vert->p ) );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( dist > tw.maxDistFromHeartPlane2 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw.maxDistFromHeartPlane2 = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// for epsilons
|
|
|
|
tw.maxDistFromHeartPlane1 += CM_BOX_EPSILON;
|
|
|
|
tw.maxDistFromHeartPlane2 += CM_BOX_EPSILON;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// trace through the model
|
|
|
|
idCollisionModelManagerLocal::TraceThroughModel( &tw );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if we're getting contacts
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tw.getContacts )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// move all contacts to world space
|
2012-11-28 15:47:07 +00:00
|
|
|
if( model_rotated )
|
|
|
|
{
|
|
|
|
for( i = 0; i < tw.numContacts; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw.contacts[i].normal *= modelAxis;
|
|
|
|
tw.contacts[i].point *= modelAxis;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( modelOrigin != vec3_origin )
|
|
|
|
{
|
|
|
|
for( i = 0; i < tw.numContacts; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tw.contacts[i].point += modelOrigin;
|
|
|
|
tw.contacts[i].dist += modelOrigin * tw.contacts[i].normal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
idCollisionModelManagerLocal::numContacts = tw.numContacts;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// store results
|
|
|
|
*results = tw.trace;
|
|
|
|
results->endpos = start + results->fraction * ( end - start );
|
|
|
|
results->endAxis = trmAxis;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( results->fraction < 1.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// if the fraction is tiny the actual movement could end up zero
|
2012-11-28 15:47:07 +00:00
|
|
|
if( results->fraction > 0.0f && results->endpos.Compare( start ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
results->fraction = 0.0f;
|
|
|
|
}
|
|
|
|
// rotate trace plane normal if there was a collision with a rotated model
|
2012-11-28 15:47:07 +00:00
|
|
|
if( model_rotated )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
results->c.normal *= modelAxis;
|
|
|
|
results->c.point *= modelAxis;
|
|
|
|
}
|
|
|
|
results->c.point += modelOrigin;
|
|
|
|
results->c.dist += modelOrigin * results->c.normal;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
// test for missed collisions
|
2012-11-28 15:47:07 +00:00
|
|
|
if( cm_debugCollision.GetBool() )
|
|
|
|
{
|
|
|
|
if( !entered && !idCollisionModelManagerLocal::getContacts )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
entered = 1;
|
|
|
|
// if the trm is stuck in the model
|
2012-11-28 15:47:07 +00:00
|
|
|
if( idCollisionModelManagerLocal::Contents( results->endpos, trm, trmAxis, -1, model, modelOrigin, modelAxis ) & contentMask )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
trace_t tr;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// test where the trm is stuck in the model
|
|
|
|
idCollisionModelManagerLocal::Contents( results->endpos, trm, trmAxis, -1, model, modelOrigin, modelAxis );
|
|
|
|
// re-run collision detection to find out where it failed
|
|
|
|
idCollisionModelManagerLocal::Translation( &tr, start, end, trm, trmAxis, contentMask, model, modelOrigin, modelAxis );
|
|
|
|
}
|
|
|
|
entered = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|