1 /*************************************************************************
3 * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith. *
4 * All rights reserved. Email: russ@q12.org Web: www.q12.org *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of EITHER: *
8 * (1) The GNU Lesser General Public License as published by the Free *
9 * Software Foundation; either version 2.1 of the License, or (at *
10 * your option) any later version. The text of the GNU Lesser *
11 * General Public License is included with this library in the *
13 * (2) The BSD-style license that is included with this library in *
14 * the file LICENSE-BSD.TXT. *
16 * This library is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
19 * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
21 *************************************************************************/
25 internal data structures and functions for collision detection.
29 #ifndef _ODE_COLLISION_KERNEL_H_
30 #define _ODE_COLLISION_KERNEL_H_
32 #include <ode/common.h>
33 #include <ode/contact.h>
34 #include <ode/collision.h>
40 //****************************************************************************
41 // constants and macros
43 // mask for the number-of-contacts field in the dCollide() flags parameter
44 #define NUMC_MASK (0xffff)
46 #define IS_SPACE(geom) \
47 dIN_RANGE((geom)->type, dFirstSpaceClass, dLastSpaceClass + 1)
49 #define CHECK_NOT_LOCKED(space) \
50 dUASSERT ((space) == NULL || (space)->lock_count == 0, \
51 "Invalid operation for locked space")
54 //****************************************************************************
55 // geometry object base class
60 // GEOM_DIRTY means that the space data structures for this geom are
61 // potentially not up to date. NOTE THAT all space parents of a dirty geom
62 // are themselves dirty. this is an invariant that must be enforced.
64 // GEOM_AABB_BAD means that the cached AABB for this geom is not up to date.
65 // note that GEOM_DIRTY does not imply GEOM_AABB_BAD, as the geom might
66 // recalculate its own AABB but does not know how to update the space data
67 // structures for the space it is in. but GEOM_AABB_BAD implies GEOM_DIRTY.
68 // the valid combinations are:
71 // GEOM_DIRTY|GEOM_AABB_BAD
72 // GEOM_DIRTY|GEOM_AABB_BAD|GEOM_POSR_BAD
75 GEOM_DIRTY
= 1, // geom is 'dirty', i.e. position unknown
76 GEOM_POSR_BAD
= 2, // geom's final posr is not valid
77 GEOM_AABB_BAD
= 4, // geom's AABB is not valid
78 GEOM_PLACEABLE
= 8, // geom is placeable
79 GEOM_ENABLED
= 16, // geom is enabled
80 GEOM_ZERO_SIZED
= 32, // geom is zero sized
82 GEOM_ENABLE_TEST_MASK
= GEOM_ENABLED
| GEOM_ZERO_SIZED
,
83 GEOM_ENABLE_TEST_VALUE
= GEOM_ENABLED
,
86 RAY_FIRSTCONTACT
= 0x10000,
87 RAY_BACKFACECULL
= 0x20000,
88 RAY_CLOSEST_HIT
= 0x40000
91 enum dxContactMergeOptions
{
93 MERGE_CONTACT_NORMALS
,
98 // geometry object base class. pos and R will either point to a separately
99 // allocated buffer (if body is 0 - pos points to the dxPosR object) or to
100 // the pos and R of the body (if body nonzero).
101 // a dGeomID is a pointer to this object.
103 struct dxGeom
: public dBase
{
104 int type
; // geom type number, set by subclass constructor
105 int gflags
; // flags used by geom and space
106 void *data
; // user-defined data pointer
107 dBodyID body
; // dynamics body associated with this object (if any)
108 dxGeom
*body_next
; // next geom in body's linked list of associated geoms
109 dxPosR
*final_posr
; // final position of the geom in world coordinates
110 dxPosR
*offset_posr
; // offset from body in local coordinates
112 // information used by spaces
113 dxGeom
*next
; // next geom in linked list of geoms
114 dxGeom
**tome
; // linked list backpointer
115 dxGeom
*next_ex
; // next geom in extra linked list of geoms (for higher level structures)
116 dxGeom
**tome_ex
; // extra linked list backpointer (for higher level structures)
117 dxSpace
*parent_space
;// the space this geom is contained in, 0 if none
118 dReal aabb
[6]; // cached AABB for this space
119 unsigned long category_bits
,collide_bits
;
121 dxGeom (dSpaceID _space
, int is_placeable
);
124 // Set or clear GEOM_ZERO_SIZED flag
125 void updateZeroSizedFlag(bool is_zero_sized
) { gflags
= is_zero_sized
? (gflags
| GEOM_ZERO_SIZED
) : (gflags
& ~GEOM_ZERO_SIZED
); }
126 // Get parent space TLS kind
127 unsigned getParentSpaceTLSKind() const;
129 const dVector3
&buildUpdatedPosition()
131 dIASSERT(gflags
& GEOM_PLACEABLE
);
134 return final_posr
->pos
;
137 const dMatrix3
&buildUpdatedRotation()
139 dIASSERT(gflags
& GEOM_PLACEABLE
);
142 return final_posr
->R
;
145 // recalculate our new final position if needed
148 if (gflags
& GEOM_POSR_BAD
) {
150 gflags
&= ~GEOM_POSR_BAD
;
154 // calculate our new final position from our offset and body
157 bool checkControlValueSizeValidity(void *dataValue
, int *dataSize
, int iRequiresSize
) { return (*dataSize
== iRequiresSize
&& dataValue
!= 0) ? true : !(*dataSize
= iRequiresSize
); } // Here it is the intent to return true for 0 required size in any case
158 virtual bool controlGeometry(int controlClass
, int controlCode
, void *dataValue
, int *dataSize
);
160 virtual void computeAABB()=0;
161 // compute the AABB for this object and put it in aabb. this function
162 // always performs a fresh computation, it does not inspect the
163 // GEOM_AABB_BAD flag.
165 virtual int AABBTest (dxGeom
*o
, dReal aabb
[6]);
166 // test whether the given AABB object intersects with this object, return
167 // 1=yes, 0=no. this is used as an early-exit test in the space collision
168 // functions. the default implementation returns 1, which is the correct
169 // behavior if no more detailed implementation can be provided.
173 // compute the AABB only if it is not current. this function manipulates
174 // the GEOM_AABB_BAD flag.
176 void recomputeAABB() {
177 if (gflags
& GEOM_AABB_BAD
) {
178 // our aabb functions assume final_posr is up to date
181 gflags
&= ~GEOM_AABB_BAD
;
185 inline void markAABBBad();
187 // add and remove this geom from a linked list maintained by a space.
189 void spaceAdd (dxGeom
**first_ptr
) {
192 if (*first_ptr
) (*first_ptr
)->tome
= &next
;
196 if (next
) next
->tome
= tome
;
200 // add and remove this geom from a linked list maintained by a body.
202 void bodyAdd (dxBody
*b
) {
210 //****************************************************************************
211 // the base space class
213 // the contained geoms are divided into two kinds: clean and dirty.
214 // the clean geoms have not moved since they were put in the list,
215 // and their AABBs are valid. the dirty geoms have changed position, and
216 // their AABBs may not be valid. the two types are distinguished by the
217 // GEOM_DIRTY flag. all dirty geoms come *before* all clean geoms in the list.
220 #define dSPACE_TLS_KIND_INIT_VALUE OTK__DEFAULT
221 #define dSPACE_TLS_KIND_MANUAL_VALUE OTK_MANUALCLEANUP
223 #define dSPACE_TLS_KIND_INIT_VALUE 0
224 #define dSPACE_TLS_KIND_MANUAL_VALUE 0
227 struct dxSpace
: public dxGeom
{
228 int count
; // number of geoms in this space
229 dxGeom
*first
; // first geom in list
230 int cleanup
; // cleanup mode, 1=destroy geoms on exit
231 int sublevel
; // space sublevel (used in dSpaceCollide2). NOT TRACKED AUTOMATICALLY!!!
232 unsigned tls_kind
; // space TLS kind to be used for global caches retrieval
234 // cached state for getGeom()
235 int current_index
; // only valid if current_geom != 0
236 dxGeom
*current_geom
; // if 0 then there is no information
238 // locking stuff. the space is locked when it is currently traversing its
239 // internal data structures, e.g. in collide() and collide2(). operations
240 // that modify the contents of the space are not permitted when the space
244 dxSpace (dSpaceID _space
);
249 void setCleanup (int mode
) { cleanup
= (mode
!= 0); }
250 int getCleanup() const { return cleanup
; }
251 void setSublevel(int value
) { sublevel
= value
; }
252 int getSublevel() const { return sublevel
; }
253 void setManulCleanup(int value
) { tls_kind
= (value
? dSPACE_TLS_KIND_MANUAL_VALUE
: dSPACE_TLS_KIND_INIT_VALUE
); }
254 int getManualCleanup() const { return (tls_kind
== dSPACE_TLS_KIND_MANUAL_VALUE
) ? 1 : 0; }
255 int query (dxGeom
*geom
) const { dAASSERT(geom
); return (geom
->parent_space
== this); }
256 int getNumGeoms() const { return count
; }
258 virtual dxGeom
*getGeom (int i
);
260 virtual void add (dxGeom
*);
261 virtual void remove (dxGeom
*);
262 virtual void dirty (dxGeom
*);
264 virtual void cleanGeoms()=0;
265 // turn all dirty geoms into clean geoms by computing their AABBs and any
266 // other space data structures that are required. this should clear the
267 // GEOM_DIRTY and GEOM_AABB_BAD flags of all geoms.
269 virtual void collide (void *data
, dNearCallback
*callback
)=0;
270 virtual void collide2 (void *data
, dxGeom
*geom
, dNearCallback
*callback
)=0;
274 //////////////////////////////////////////////////////////////////////////
277 void dxGeom::markAABBBad() {
278 gflags
|= (GEOM_DIRTY
| GEOM_AABB_BAD
);
279 CHECK_NOT_LOCKED(parent_space
);
283 //****************************************************************************
284 // Initialization and finalization functions
286 void dInitColliders();
287 void dFinitColliders();
289 void dClearPosrCache(void);
290 void dFinitUserClasses();