#ifndef GIM_TRI_COLLISION_H_INCLUDED #define GIM_TRI_COLLISION_H_INCLUDED /*! \file gim_tri_collision.h \author Francisco Leon Najera */ /* ----------------------------------------------------------------------------- This source file is part of GIMPACT Library. For the latest info, see http://gimpact.sourceforge.net/ Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371. email: projectileman@yahoo.com This library is free software; you can redistribute it and/or modify it under the terms of EITHER: (1) The GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. The text of the GNU Lesser General Public License is included with this library in the file GIMPACT-LICENSE-LGPL.TXT. (2) The BSD-style license that is included with this library in the file GIMPACT-LICENSE-BSD.TXT. (3) The zlib/libpng license that is included with this library in the file GIMPACT-LICENSE-ZLIB.TXT. 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 files GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details. ----------------------------------------------------------------------------- */ #include "gim_box_collision.h" #include "gim_clip_polygon.h" #ifndef MAX_TRI_CLIPPING #define MAX_TRI_CLIPPING 16 #endif //! Structure for collision struct GIM_TRIANGLE_CONTACT_DATA { GREAL m_penetration_depth; GUINT m_point_count; btVector4 m_separating_normal; btVector3 m_points[MAX_TRI_CLIPPING]; SIMD_FORCE_INLINE void copy_from(const GIM_TRIANGLE_CONTACT_DATA& other) { m_penetration_depth = other.m_penetration_depth; m_separating_normal = other.m_separating_normal; m_point_count = other.m_point_count; GUINT i = m_point_count; while(i--) { m_points[i] = other.m_points[i]; } } GIM_TRIANGLE_CONTACT_DATA() { } GIM_TRIANGLE_CONTACT_DATA(const GIM_TRIANGLE_CONTACT_DATA& other) { copy_from(other); } //! classify points that are closer template SIMD_FORCE_INLINE void mergepoints_generic(const CLASS_PLANE & plane, GREAL margin, const btVector3 * points, GUINT point_count, DISTANCE_FUNC distance_func) { m_point_count = 0; m_penetration_depth= -1000.0f; GUINT point_indices[MAX_TRI_CLIPPING]; GUINT _k; for(_k=0;_k=0.0f) { if(_dist>m_penetration_depth) { m_penetration_depth = _dist; point_indices[0] = _k; m_point_count=1; } else if((_dist+G_EPSILON)>=m_penetration_depth) { point_indices[m_point_count] = _k; m_point_count++; } } } for( _k=0;_k u*axe1[i1] + ((vecproj[i2] - u*axe1[i2])/axe2[i2])*axe2[i1] = vecproj[i1] --> u*axe1[i1] + vecproj[i2]*axe2[i1]/axe2[i2] - u*axe1[i2]*axe2[i1]/axe2[i2] = vecproj[i1] --> u*(axe1[i1] - axe1[i2]*axe2[i1]/axe2[i2]) = vecproj[i1] - vecproj[i2]*axe2[i1]/axe2[i2] --> u*((axe1[i1]*axe2[i2] - axe1[i2]*axe2[i1])/axe2[i2]) = (vecproj[i1]*axe2[i2] - vecproj[i2]*axe2[i1])/axe2[i2] --> u*(axe1[i1]*axe2[i2] - axe1[i2]*axe2[i1]) = vecproj[i1]*axe2[i2] - vecproj[i2]*axe2[i1] --> u = (vecproj[i1]*axe2[i2] - vecproj[i2]*axe2[i1]) /(axe1[i1]*axe2[i2] - axe1[i2]*axe2[i1]) if 0.0<= u+v <=1.0 then they are inside of triangle \return false if the point is outside of triangle.This function doesn't take the margin */ SIMD_FORCE_INLINE bool get_uv_parameters( const btVector3 & point, const btVector3 & tri_plane, GREAL & u, GREAL & v) const { btVector3 _axe1 = m_vertices[1]-m_vertices[0]; btVector3 _axe2 = m_vertices[2]-m_vertices[0]; btVector3 _vecproj = point - m_vertices[0]; GUINT _i1 = (tri_plane.closestAxis()+1)%3; GUINT _i2 = (_i1+1)%3; if(btFabs(_axe2[_i2])G_EPSILON) { return false; } } return true; } //! is point in triangle beam? /*! Test if point is in triangle, with m_margin tolerance */ SIMD_FORCE_INLINE bool is_point_inside(const btVector3 & point, const btVector3 & tri_normal) const { //Test with edge 0 btVector4 edge_plane; this->get_edge_plane(0,tri_normal,edge_plane); GREAL dist = DISTANCE_PLANE_POINT(edge_plane,point); if(dist-m_margin>0.0f) return false; // outside plane this->get_edge_plane(1,tri_normal,edge_plane); dist = DISTANCE_PLANE_POINT(edge_plane,point); if(dist-m_margin>0.0f) return false; // outside plane this->get_edge_plane(2,tri_normal,edge_plane); dist = DISTANCE_PLANE_POINT(edge_plane,point); if(dist-m_margin>0.0f) return false; // outside plane return true; } //! Bidireccional ray collision SIMD_FORCE_INLINE bool ray_collision( const btVector3 & vPoint, const btVector3 & vDir, btVector3 & pout, btVector3 & triangle_normal, GREAL & tparam, GREAL tmax = G_REAL_INFINITY) { btVector4 faceplane; { btVector3 dif1 = m_vertices[1] - m_vertices[0]; btVector3 dif2 = m_vertices[2] - m_vertices[0]; VEC_CROSS(faceplane,dif1,dif2); faceplane[3] = m_vertices[0].dot(faceplane); } GUINT res = LINE_PLANE_COLLISION(faceplane,vDir,vPoint,pout,tparam, btScalar(0), tmax); if(res == 0) return false; if(! is_point_inside(pout,faceplane)) return false; if(res==2) //invert normal { triangle_normal.setValue(-faceplane[0],-faceplane[1],-faceplane[2]); } else { triangle_normal.setValue(faceplane[0],faceplane[1],faceplane[2]); } VEC_NORMALIZE(triangle_normal); return true; } //! one direccion ray collision SIMD_FORCE_INLINE bool ray_collision_front_side( const btVector3 & vPoint, const btVector3 & vDir, btVector3 & pout, btVector3 & triangle_normal, GREAL & tparam, GREAL tmax = G_REAL_INFINITY) { btVector4 faceplane; { btVector3 dif1 = m_vertices[1] - m_vertices[0]; btVector3 dif2 = m_vertices[2] - m_vertices[0]; VEC_CROSS(faceplane,dif1,dif2); faceplane[3] = m_vertices[0].dot(faceplane); } GUINT res = LINE_PLANE_COLLISION(faceplane,vDir,vPoint,pout,tparam, btScalar(0), tmax); if(res != 1) return false; if(!is_point_inside(pout,faceplane)) return false; triangle_normal.setValue(faceplane[0],faceplane[1],faceplane[2]); VEC_NORMALIZE(triangle_normal); return true; } }; #endif // GIM_TRI_COLLISION_H_INCLUDED