aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ode-0.9/ode/src/collision_cylinder_sphere.cpp
blob: 964c53196d124f6310bbc71058e543d8c2fd6d01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*************************************************************************
*                                                                       *
* Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith.       *
* All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
*                                                                       *
* 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 LICENSE.TXT.                                               *
*   (2) The BSD-style license that is included with this library in     *
*       the file LICENSE-BSD.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    *
* LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
*                                                                       *
*************************************************************************/


/*******************************************************************
 *                                                                 *
 * cylinder-sphere collider by Christoph Beyer (boernerb@web.de)   *
 *                                                                 *
 * In Cylinder/Sphere-collisions, there are three possibilies:     *
 * 1. collision with the cylinder's nappe                          *
 * 2. collision with one of the cylinder's disc                    *
 * 3. collision with one of the disc's border                      *
 *                                                                 *
 * This collider computes two distances (s, t) and based on them,  *
 * it decides, which collision we have.                            *
 * This collider always generates 1 (or 0, if we have no collison) *
 * contacts.                                                       *
 * It is able to "separate" cylinder and sphere in all             *
 * configurations, but it never pays attention to velocity.        *
 * So, in extrem situations, "tunneling-effect" is possible.       *
 *                                                                 *
 *******************************************************************/

#include <ode/collision.h>
#include <ode/matrix.h>
#include <ode/rotation.h>
#include <ode/odemath.h>
#include <ode/objects.h>
#include "collision_kernel.h"	// for dxGeom
#include "collision_util.h"

int dCollideCylinderSphere(dxGeom* Cylinder, dxGeom* Sphere, 
                           int flags, dContactGeom *contact, int skip)
{
	dIASSERT (skip >= (int)sizeof(dContactGeom));
	dIASSERT (Cylinder->type == dCylinderClass);
	dIASSERT (Sphere->type == dSphereClass);
	dIASSERT ((flags & NUMC_MASK) >= 1);

	unsigned char* pContactData = (unsigned char*)contact;
	int GeomCount = 0; // count of used contacts

#ifdef dSINGLE
	const dReal toleranz = REAL(0.0001);
#endif
#ifdef dDOUBLE
	const dReal toleranz = REAL(0.0000001);
#endif

	// get the data from the geoms
	dReal radius, length;
	dGeomCylinderGetParams(Cylinder, &radius, &length);
    dVector3 &cylpos = Cylinder->final_posr->pos;
	const dReal* pfRot1 = dGeomGetRotation(Cylinder);

	dReal radius2;
	radius2 = dGeomSphereGetRadius(Sphere);
	const dReal* SpherePos = dGeomGetPosition(Sphere);

	// G1Pos1 is the middle of the first disc
	// G1Pos2 is the middle of the second disc
	// vDir1 is the unit direction of the cylinderaxis
	dVector3 G1Pos1, G1Pos2, vDir1;
	vDir1[0] = Cylinder->final_posr->R[2];
	vDir1[1] = Cylinder->final_posr->R[6];
	vDir1[2] = Cylinder->final_posr->R[10];

	dReal s;
	s = length * REAL(0.5); // just a precomputed factor
	G1Pos2[0] = vDir1[0] * s + cylpos[0];
	G1Pos2[1] = vDir1[1] * s + cylpos[1];
	G1Pos2[2] = vDir1[2] * s + cylpos[2];

	G1Pos1[0] = vDir1[0] * -s + cylpos[0];
	G1Pos1[1] = vDir1[1] * -s + cylpos[1];
	G1Pos1[2] = vDir1[2] * -s + cylpos[2];

	dVector3 C;
	dReal t;
	// Step 1: compute the two distances 's' and 't'
	// 's' is the distance from the first disc (in vDir1-/Zylinderaxis-direction), the disc with G1Pos1 in the middle
	s = (SpherePos[0] - G1Pos1[0]) * vDir1[0] - (G1Pos1[1] - SpherePos[1]) * vDir1[1] - (G1Pos1[2] - SpherePos[2]) * vDir1[2];
	if(s < (-radius2) || s > (length + radius2) )
	{
		// Sphere is too far away from the discs
		// no collision
		return 0;
	}

	// C is the direction from Sphere-middle to the cylinder-axis (vDir1); C is orthogonal to the cylinder-axis
	C[0] = s * vDir1[0] + G1Pos1[0] - SpherePos[0];
	C[1] = s * vDir1[1] + G1Pos1[1] - SpherePos[1];
	C[2] = s * vDir1[2] + G1Pos1[2] - SpherePos[2];
	// t is the distance from the Sphere-middle to the cylinder-axis!
	t = dVector3Length(C);
	if(t > (radius + radius2) )
	{
		// Sphere is too far away from the cylinder axis!
		// no collision
		return 0;
	}

	// decide which kind of collision we have:
	if(t > radius && (s < 0 || s > length) )
	{
		// 3. collision
		if(s <= 0)
		{
			contact->depth = radius2 - dSqrt( (s) * (s) + (t - radius) * (t - radius) );
			if(contact->depth < 0)
			{
				// no collision!
				return 0;
			}
			contact->pos[0] = C[0] / t * -radius + G1Pos1[0];
			contact->pos[1] = C[1] / t * -radius + G1Pos1[1];
			contact->pos[2] = C[2] / t * -radius + G1Pos1[2];
			contact->normal[0] = (contact->pos[0] - SpherePos[0]) / (radius2 - contact->depth);
			contact->normal[1] = (contact->pos[1] - SpherePos[1]) / (radius2 - contact->depth);
			contact->normal[2] = (contact->pos[2] - SpherePos[2]) / (radius2 - contact->depth);
			contact->g1 = Cylinder;
			contact->g2 = Sphere;
			GeomCount++;
			return GeomCount;
		}
		else
		{
			// now s is bigger than length here!
			contact->depth = radius2 - dSqrt( (s - length) * (s - length) + (t - radius) * (t - radius) );
			if(contact->depth < 0)
			{
				// no collision!
				return 0;
			}
			contact->pos[0] = C[0] / t * -radius + G1Pos2[0];
			contact->pos[1] = C[1] / t * -radius + G1Pos2[1];
			contact->pos[2] = C[2] / t * -radius + G1Pos2[2];
			contact->normal[0] = (contact->pos[0] - SpherePos[0]) / (radius2 - contact->depth);
			contact->normal[1] = (contact->pos[1] - SpherePos[1]) / (radius2 - contact->depth);
			contact->normal[2] = (contact->pos[2] - SpherePos[2]) / (radius2 - contact->depth);
			contact->g1 = Cylinder;
			contact->g2 = Sphere;
			GeomCount++;
			return GeomCount;
		}
	}
	else if( (radius - t) <= s && (radius - t) <= (length - s) )
	{
		// 1. collsision
		if(t > (radius2 + toleranz))
		{
			// cylinder-axis is outside the sphere
			contact->depth = (radius2 + radius) - t;
			if(contact->depth < 0)
			{
				// should never happen, but just for safeness
				return 0;
			}
			else
			{
				C[0] /= t;
				C[1] /= t;
				C[2] /= t;
				contact->pos[0] = C[0] * radius2 + SpherePos[0];
				contact->pos[1] = C[1] * radius2 + SpherePos[1];
				contact->pos[2] = C[2] * radius2 + SpherePos[2];
				contact->normal[0] = C[0];
				contact->normal[1] = C[1];
				contact->normal[2] = C[2];
				contact->g1 = Cylinder;
				contact->g2 = Sphere;
				GeomCount++;
				return GeomCount;
			}
		}
		else
		{
			// cylinder-axis is outside of the sphere
			contact->depth = (radius2 + radius) - t;
			if(contact->depth < 0)
			{
				// should never happen, but just for safeness
				return 0;
			}
			else
			{
				contact->pos[0] = C[0] + SpherePos[0];
				contact->pos[1] = C[1] + SpherePos[1];
				contact->pos[2] = C[2] + SpherePos[2];
				contact->normal[0] = C[0] / t;
				contact->normal[1] = C[1] / t;
				contact->normal[2] = C[2] / t;
				contact->g1 = Cylinder;
				contact->g2 = Sphere;
				GeomCount++;
				return GeomCount;
			}
		}
	}
	else
	{
		// 2. collision
		if(s <= (length * REAL(0.5)) )
		{
			// collsision with the first disc
			contact->depth = s + radius2;
			if(contact->depth < 0)
			{
				// should never happen, but just for safeness
				return 0;
			}
			contact->pos[0] = radius2 * vDir1[0] + SpherePos[0];
			contact->pos[1] = radius2 * vDir1[1] + SpherePos[1];
			contact->pos[2] = radius2 * vDir1[2] + SpherePos[2];
			contact->normal[0] = vDir1[0];
			contact->normal[1] = vDir1[1];
			contact->normal[2] = vDir1[2];
			contact->g1 = Cylinder;
			contact->g2 = Sphere;
			GeomCount++;
			return GeomCount;
		}
		else
		{
			// collsision with the second disc
			contact->depth = (radius2 + length - s);
			if(contact->depth < 0)
			{
				// should never happen, but just for safeness
				return 0;
			}
			contact->pos[0] = radius2 * -vDir1[0] + SpherePos[0];
			contact->pos[1] = radius2 * -vDir1[1] + SpherePos[1];
			contact->pos[2] = radius2 * -vDir1[2] + SpherePos[2];
			contact->normal[0] = -vDir1[0];
			contact->normal[1] = -vDir1[1];
			contact->normal[2] = -vDir1[2];
			contact->g1 = Cylinder;
			contact->g2 = Sphere;
			GeomCount++;
			return GeomCount;
		}
	}
	return GeomCount;
}