Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CSopcodecollider.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2000 by Jorrit Tyberghein
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public
15  License along with this library; if not, write to the Free
16  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 
19 /*
20 -------------------------------------------------------------------------
21 * OPCODE library was written by Pierre Terdiman
22 * ported to CS by Charles Quarra
23 * ported to VS from CS by Ed Sweetman
24 -------------------------------------------------------------------------
25 */
26 
27 #ifndef __VS_OPCODECOL_H__
28 #include "Stdafx.h"
29 #define __VS_OPCODECOL_H__
30 #include "csgeom2/opmatrix3.h"
31 #include "csgeom2/opvector3.h"
32 #include "csgeom2/opbox.h"
33 #include "csgeom2/optransfrm.h"
34 #include "opcodegarray.h"
35 #include "basecollider.h"
36 #include "gfx/mesh.h"
37 
38 /*
39  How to use Collider.
40 
41  The next two calls happen usually once when you first create a unit.
42 
43  Create an instance of the collider sending it the appropriate geometry
44  csOPCODECollider(vector<mesh_polygon>);
45 
46  Optionally set if you want to return on first contact or not.
47  It defaults to not.
48  csOPCODECollider.SetOneHitOnly(bool);
49 
50  The rest of the calls occur in your physics loops
51 
52  Reset our list of collided pairs of vectors.
53  csOPCODECollider.ResetCollisionPairs();
54 
55  Check if a collision occurred, sending the other collider and transforms for
56  both colliders. Returns true if we collided.
57  csOPCODECollider.Collide(csOPCODECollider&, const csReversibleTransform* first,
58  const csReversibleTransform* second);
59 
60  If true, retrieve the vectors that collided so we can act upon them.
61  csOPCODECollider.GetCollisions();
62 
63  We also need the number of collided vectors in case we dont have
64  first hit set to true.
65  csOPCodeCollider.GetCollisionPairCount();
66 */
67 
68 
69 // Low level collision detection using Opcode library.
71 {
72  private:
73  /* does what it says. Takes our mesh_polygon vector and turns it into
74  * a linear list of vertexes that we reference in collision trees
75  * radius is set in here as well
76  */
77  void GeometryInitialize (const std::vector <mesh_polygon> &polygons);
78 
79  /* callback used to return vertex points when requested from opcode*/
80  static void MeshCallback (udword triangle_index,
81  Opcode::VertexPointers& triangle, void* user_data);
82 
83  /* returns face of mesh where ray collided */
84  static void RayCallback(const Opcode::CollisionFace &, void*);
85 
86  /* Radius around unit using center of unit and furthest part of unit */
87  float radius;
88 
89  /* Array of Point's corresponding to vertices of triangles given by mesh_polygon */
90  Opcode::Point *vertholder;
91 
92  /* OPCODE interfaces. */
93  Opcode::Model* m_pCollisionModel;
94  Opcode::MeshInterface opcMeshInt;
95  Opcode::BVTCache ColCache;
96  Opcode::CollisionFace collFace;
97  /* Collider type: Tree - Used primarily for mesh on mesh collisions */
98  Opcode::AABBTreeCollider TreeCollider;
99 
100  /* Collider type: Ray - used to check if a ray collided with the tree collider above */
101  Opcode::RayCollider rCollider;
102 
103  /* We have to copy our Points to csVector3's because opcode likes Point
104  * and VS likes Vector. */
105  void CopyCollisionPairs (csOPCODECollider* col1, csOPCODECollider* col2);
106 
107  public:
108  csOPCODECollider (const std::vector <mesh_polygon> &polygons);
110 
111  /* Not used in 0.5 */
112  int inline GetColliderType () const {return CS_MESH_COLLIDER;}
113 
114  /* Collides the bolt or beam with this collider, returning true if it occurred */
115  bool rayCollide (const Opcode::Ray &boltbeam, Vector&norm, float&distance);
116 
117 
118 
119  /* Collides the argument collider with this collider, returning true if it occurred */
120  bool Collide (csOPCODECollider &pOtherCollider,
121  const csReversibleTransform *pThisTransform = 0,
122  const csReversibleTransform *pOtherTransform = 0);
123 
124  /* Returns the pair array, as of 0.5 this is a global static var
125  * The pair array contains the vertices that have collided as returned
126  * by the last collision. This is concatenated, meaning, if it's not
127  * cleared by the client code, the collisions just get pushed onto the
128  * array indefinitely. It should be cleared between collide calls */
129  static csCollisionPair *GetCollisions();
130 
131  /* clears the pair array */
132  static void ResetCollisionPairs ();
133 
134  /* Returns the size of the pair array */
135  static size_t GetCollisionPairCount();
136 
137  /* Sets First contact to argument.
138  * This means that Collide will return true as soon as the first
139  * contact is detected, rather than return the contacts for all
140  * detected vertex collisions */
141  void SetOneHitOnly (bool fh);
142  inline bool GetOneHitOnly () const { return(TreeCollider.FirstContactEnabled());}
143 
144  /* Returns the radius of our collision mesh. This is the max radius
145  * of the mesh we were initialized with */
146  inline float GetRadius () const {return radius;};
147 
148  /* Function that returns the Vector given the vertex index
149  * Used for displaying the annoying damage particles */
150  Vector getVertex (unsigned int which) const;
151 
152  /* Returns number of vertexes in model */
153  inline unsigned int getNumVertex() const { return(m_pCollisionModel->GetMeshInterface()->GetNbVertices());}
154 };
155 
156 #endif