1 /*************************************************************************
3 * Open Dynamics Engine, Copyright (C) 2001,2002 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 *************************************************************************/
29 #include <ode/collision.h>
30 #include <ode/matrix.h>
31 #include <ode/rotation.h>
32 #include <ode/odemath.h>
33 #include "collision_transform.h"
34 #include "collision_util.h"
37 #pragma warning(disable:4291) // for VC++, no complaints about "no matching operator delete found"
40 //****************************************************************************
41 // dxGeomTransform class
43 struct dxGeomTransform
: public dxGeom
{
44 dxGeom
*obj
; // object that is being transformed
45 int cleanup
; // 1 to destroy obj when destroyed
46 int infomode
; // 1 to put Tx geom in dContactGeom g1
48 // cached final object transform (body tx + relative tx). this is set by
49 // computeAABB(), and it is valid while the AABB is valid.
50 dxPosR transform_posr
;
52 dxGeomTransform (dSpaceID space
);
55 void computeFinalTx();
60 printf("sizeof body = %i\n", sizeof(dxBody));
61 printf("sizeof geom = %i\n", sizeof(dxGeom));
62 printf("sizeof geomtransform = %i\n", sizeof(dxGeomTransform));
63 printf("sizeof posr = %i\n", sizeof(dxPosR));
67 dxGeomTransform::dxGeomTransform (dSpaceID space
) : dxGeom (space
,1)
69 type
= dGeomTransformClass
;
73 dSetZero (transform_posr
.pos
,4);
74 dRSetIdentity (transform_posr
.R
);
78 dxGeomTransform::~dxGeomTransform()
80 if (obj
&& cleanup
) delete obj
;
84 void dxGeomTransform::computeAABB()
91 // backup the relative pos and R pointers of the encapsulated geom object
92 dxPosR
* posr_bak
= obj
->final_posr
;
94 // compute temporary pos and R for the encapsulated geom object
96 obj
->final_posr
= &transform_posr
;
100 memcpy (aabb
,obj
->aabb
,6*sizeof(dReal
));
102 // restore the pos and R
103 obj
->final_posr
= posr_bak
;
107 // utility function for dCollideTransform() : compute final pos and R
108 // for the encapsulated geom object
110 void dxGeomTransform::computeFinalTx()
112 dMULTIPLY0_331 (transform_posr
.pos
,final_posr
->R
,obj
->final_posr
->pos
);
113 transform_posr
.pos
[0] += final_posr
->pos
[0];
114 transform_posr
.pos
[1] += final_posr
->pos
[1];
115 transform_posr
.pos
[2] += final_posr
->pos
[2];
116 dMULTIPLY0_333 (transform_posr
.R
,final_posr
->R
,obj
->final_posr
->R
);
119 //****************************************************************************
120 // collider function:
121 // this collides a transformed geom with another geom. the other geom can
122 // also be a transformed geom, but this case is not handled specially.
124 int dCollideTransform (dxGeom
*o1
, dxGeom
*o2
, int flags
,
125 dContactGeom
*contact
, int skip
)
127 dIASSERT (skip
>= (int)sizeof(dContactGeom
));
128 dIASSERT (o1
->type
== dGeomTransformClass
);
130 dxGeomTransform
*tr
= (dxGeomTransform
*) o1
;
131 if (!tr
->obj
) return 0;
132 dUASSERT (tr
->obj
->parent_space
==0,
133 "GeomTransform encapsulated object must not be in a space");
134 dUASSERT (tr
->obj
->body
==0,
135 "GeomTransform encapsulated object must not be attached "
138 // backup the relative pos and R pointers of the encapsulated geom object,
139 // and the body pointer
140 dxPosR
*posr_bak
= tr
->obj
->final_posr
;
141 dxBody
*bodybak
= tr
->obj
->body
;
143 // compute temporary pos and R for the encapsulated geom object.
144 // note that final_pos and final_R are valid if no GEOM_AABB_BAD flag,
145 // because computeFinalTx() will have already been called in
146 // dxGeomTransform::computeAABB()
148 if (tr
->gflags
& GEOM_AABB_BAD
) tr
->computeFinalTx();
149 tr
->obj
->final_posr
= &tr
->transform_posr
;
150 tr
->obj
->body
= o1
->body
;
153 int n
= dCollide (tr
->obj
,o2
,flags
,contact
,skip
);
155 // if required, adjust the 'g1' values in the generated contacts so that
156 // thay indicated the GeomTransform object instead of the encapsulated
159 for (int i
=0; i
<n
; i
++) {
160 dContactGeom
*c
= CONTACT(contact
,skip
*i
);
165 // restore the pos, R and body
166 tr
->obj
->final_posr
= posr_bak
;
167 tr
->obj
->body
= bodybak
;
171 //****************************************************************************
174 dGeomID
dCreateGeomTransform (dSpaceID space
)
176 return new dxGeomTransform (space
);
180 void dGeomTransformSetGeom (dGeomID g
, dGeomID obj
)
182 dUASSERT (g
&& g
->type
== dGeomTransformClass
,
183 "argument not a geom transform");
184 dxGeomTransform
*tr
= (dxGeomTransform
*) g
;
185 if (tr
->obj
&& tr
->cleanup
) delete tr
->obj
;
190 dGeomID
dGeomTransformGetGeom (dGeomID g
)
192 dUASSERT (g
&& g
->type
== dGeomTransformClass
,
193 "argument not a geom transform");
194 dxGeomTransform
*tr
= (dxGeomTransform
*) g
;
199 void dGeomTransformSetCleanup (dGeomID g
, int mode
)
201 dUASSERT (g
&& g
->type
== dGeomTransformClass
,
202 "argument not a geom transform");
203 dxGeomTransform
*tr
= (dxGeomTransform
*) g
;
208 int dGeomTransformGetCleanup (dGeomID g
)
210 dUASSERT (g
&& g
->type
== dGeomTransformClass
,
211 "argument not a geom transform");
212 dxGeomTransform
*tr
= (dxGeomTransform
*) g
;
217 void dGeomTransformSetInfo (dGeomID g
, int mode
)
219 dUASSERT (g
&& g
->type
== dGeomTransformClass
,
220 "argument not a geom transform");
221 dxGeomTransform
*tr
= (dxGeomTransform
*) g
;
226 int dGeomTransformGetInfo (dGeomID g
)
228 dUASSERT (g
&& g
->type
== dGeomTransformClass
,
229 "argument not a geom transform");
230 dxGeomTransform
*tr
= (dxGeomTransform
*) g
;