1 /***************************************************************************
2                           bound.cpp  -  Plane-based collision model
3                              -------------------
4     begin                : vr jan 24 2003
5     copyright            : (C) 2003 by CJP
6     email                : cornware-cjp@users.sourceforge.net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include <cmath>
19 
20 #include "bound.h"
21 #include "datafile.h"
22 
CBound(CDataManager * manager)23 CBound::CBound(CDataManager *manager) : CCollisionModel(manager)
24 {
25 	m_DataType = CDataObject::eBound;
26 }
27 
~CBound()28 CBound::~CBound()
29 {}
30 
load(const CString & filename,const CParamList & list)31 bool CBound::load(const CString &filename, const CParamList &list)
32 {
33 	CCollisionModel::load(filename, list);
34 
35 	m_Points.clear();
36 	for(unsigned int i=0; i < m_Faces.size()-2; i++)
37 		for(unsigned int j=i+1; j < m_Faces.size()-1; j++)
38 		{
39 			CCollisionFace &fi = m_Faces[i];
40 			CCollisionFace &fj = m_Faces[j];
41 			float dotpr = fi.nor.dotProduct(fj.nor);
42 			if(dotpr > -0.99 && dotpr < 0.99) //not parallel
43 			{
44 				CVector dr = fi.nor.crossProduct(fj.nor);
45 				CVector r0 = ( (fi.d*fi.nor + fj.d*fj.nor) - dotpr*(fj.d*fi.nor + fi.d*fj.nor) ) / (1.0 - dotpr*dotpr);
46 				for(unsigned int k=j+1; k < m_Faces.size(); k++)
47 				{
48 					CCollisionFace &fk = m_Faces[k];
49 					dotpr = dr.dotProduct(fk.nor);
50 					if(dotpr > 0.01 || dotpr < -0.01) //not parallel
51 					{
52 						CVector p = r0 + ((fk.d - r0.dotProduct(fk.nor)) / dotpr)*dr;
53 
54 						bool inside = true;
55 						for(unsigned int l=0; l < m_Faces.size(); l++)
56 							if(l != i && l != j && l != k &&
57 								p.dotProduct(m_Faces[l].nor) > m_Faces[l].d)
58 								{inside = false; break;}
59 
60 						if(inside)
61 							m_Points.push_back(p);
62 					}
63 				}
64 			}
65 		}
66 
67 	//set cylinder parameters
68 	m_CylinderRadius = 0.5 * fabs(m_OBB_max.y - m_OBB_min.y);
69 	//m_CylinderWidth = fabs(m_OBB_max.x - m_OBB_min.x);
70 
71 	return true;
72 }
73