summaryrefslogtreecommitdiff
path: root/libs/ode-0.16.1/tests/collision.cpp
blob: 6eef4cf389423e4685b9ea6f43e41ffd2aa38fa4 (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
#include <UnitTest++.h>
#include <ode/ode.h>
#include "common.h"

TEST(test_collision_trimesh_sphere_exact)
{
    /*
     * This tests some extreme cases, where a sphere barely touches some triangles
     * with zero depth.
     */
    
    #ifdef dTRIMESH_GIMPACT
    /*
     * Although GIMPACT is algorithmically able to handle this extreme case,
     * the numerical approximation used for the square root produces inexact results.
     */
    return;
    #endif

    {
        const int VertexCount = 4;
        const int IndexCount = 2*3;
        // this is a square on the XY plane
        /*
           3    2
           +----+
           |   /|
           |  / |
           | /  |
           |/   |
           +----+
           0    1
         */
        float vertices[VertexCount * 3] = {
            -1,-1,0,
            1,-1,0,
            1,1,0,
            -1,1,0
        };
        dTriIndex indices[IndexCount] = {
            0,1,2,
            0,2,3
        };
        
        dTriMeshDataID data = dGeomTriMeshDataCreate();
        dGeomTriMeshDataBuildSingle(data,
                                    vertices,
                                    3 * sizeof(float),
                                    VertexCount,
                                    indices,
                                    IndexCount,
                                    3 * sizeof(dTriIndex));
        dGeomID trimesh = dCreateTriMesh(0, data, 0, 0, 0);
        const dReal radius = 4;
        dGeomID sphere = dCreateSphere(0, radius);
        dContactGeom cg[4];
        int nc;
        dVector3 trinormal = { 0, 0, -1 };

        // Test case: sphere touches the diagonal edge
        dGeomSetPosition(sphere, 0,0,radius);
        nc = dCollide(trimesh, sphere, 4, &cg[0], sizeof cg[0]);
        CHECK_EQUAL(2, nc);
        for (int i=0; i<nc; ++i) {
            CHECK_EQUAL(0, cg[i].depth);
            CHECK_ARRAY_EQUAL(trinormal, cg[i].normal, 3);
        }
        
        // now translate both geoms
        dGeomSetPosition(trimesh, 10,30,40);
        dGeomSetPosition(sphere, 10,30,40+radius);
        // check extreme case, again
        nc = dCollide(trimesh, sphere, 4, &cg[0], sizeof cg[0]);
        CHECK_EQUAL(2, nc);
        for (int i=0; i<nc; ++i) {
            CHECK_EQUAL(0, cg[i].depth);
            CHECK_ARRAY_EQUAL(trinormal, cg[i].normal, 3);
        }
        
        // and now, let's rotate the trimesh, 90 degrees on X
        dMatrix3 rot = { 1, 0, 0, 0,
                         0, 0, -1, 0,
                         0, 1, 0, 0 };
        dGeomSetPosition(trimesh, 10,30,40);
        dGeomSetRotation(trimesh, rot);
        
        dGeomSetPosition(sphere, 10,30-radius,40);
        // check extreme case, again
        nc = dCollide(trimesh, sphere, 4, &cg[0], sizeof cg[0]);
        CHECK_EQUAL(2, nc);
        dVector3 rtrinormal = { 0, 1, 0 };
        for (int i=0; i<nc; ++i) {
            CHECK_EQUAL(0, cg[i].depth);
            CHECK_ARRAY_EQUAL(rtrinormal, cg[i].normal, 3);
        }
    }
}



TEST(test_collision_heightfield_ray_fail)
{
    /*
     * This test demonstrated a bug in the AABB handling of the
     * heightfield.
     */
    {
        // Create quick heightfield with dummy data
        dHeightfieldDataID heightfieldData = dGeomHeightfieldDataCreate();
        unsigned char dataBuffer[16+1] = "1234567890123456";
        dGeomHeightfieldDataBuildByte(heightfieldData, dataBuffer, 0, 4, 4, 4, 4, 1, 0, 0, 0);
        dGeomHeightfieldDataSetBounds(heightfieldData, '0', '9');
	    dGeomID height = dCreateHeightfield(0, heightfieldData, 1);

        // Create ray outside bounds
        dGeomID ray = dCreateRay(0, 20);
        dGeomRaySet(ray, 5, 10, 1, 0, -1, 0);
        dContact contactBuf[10];

        // Make sure it does not crash!
        dCollide(ray, height, 10, &(contactBuf[0].geom), sizeof(dContact));

        dGeomDestroy(height);
        dGeomDestroy(ray);
        dGeomHeightfieldDataDestroy(heightfieldData);
    }
}

#include "../ode/demo/convex_prism.h"

TEST(test_collision_ray_convex)
{
    /*
     * Issue 55: ray vs convex collider does not consider the position of the convex geometry.
     */
    {
		dContact contact;

        // Create convex
	    dGeomID convex = dCreateConvex(0, 
            prism_planes, 
            prism_planecount, 
            prism_points, 
            prism_pointcount,
            prism_polygons);
        dGeomSetPosition(convex,0,0,0);

        // Create ray
        dGeomID ray = dCreateRay(0, 20);

        dGeomRaySet(ray, 0, -10, 0, 0, 1, 0);

        int count = dCollide(ray, convex, 1, &contact.geom, sizeof(dContact));

		CHECK_EQUAL(1,count);
		CHECK_CLOSE(0.0,contact.geom.pos[0], dEpsilon);
		CHECK_CLOSE(-1.0,contact.geom.pos[1], dEpsilon);
		CHECK_CLOSE(0.0,contact.geom.pos[2], dEpsilon);
		CHECK_CLOSE(0.0, contact.geom.normal[0], dEpsilon);
		CHECK_CLOSE(-1.0, contact.geom.normal[1], dEpsilon);
		CHECK_CLOSE(0.0, contact.geom.normal[2], dEpsilon);
		CHECK_CLOSE(9.0, contact.geom.depth, dEpsilon);

		// Move Ray
		dGeomRaySet(ray, 5, -10, 0, 0, 1, 0);

		count = dCollide(ray, convex, 1, &contact.geom, sizeof(dContact));

		CHECK_EQUAL(1,count);
		CHECK_CLOSE(5.0, contact.geom.pos[0], dEpsilon);
		CHECK_CLOSE(-1.0, contact.geom.pos[1], dEpsilon);
		CHECK_CLOSE(0.0, contact.geom.pos[2], dEpsilon);
		CHECK_CLOSE(0.0, contact.geom.normal[0], dEpsilon);
		CHECK_CLOSE(-1.0, contact.geom.normal[1], dEpsilon);
		CHECK_CLOSE(0.0, contact.geom.normal[2], dEpsilon);
		CHECK_CLOSE(9.0, contact.geom.depth, dEpsilon);

		// Rotate Convex
		dMatrix3 rotate90z = 
		{
			0,-1,0,0,
			1,0,0,0,
			0,0,1,0 
		};
		dGeomSetRotation(convex, rotate90z);

		count = dCollide(ray, convex, 1, &contact.geom, sizeof(dContact));

		CHECK_EQUAL(0,count);

		// Move Ray
		dGeomRaySet(ray, 10, 0, 0, -1, 0, 0);
		count = dCollide(ray, convex, 1, &contact.geom, sizeof(dContact));

		CHECK_EQUAL(1,count);
		CHECK_CLOSE(1.0, contact.geom.pos[0], dEpsilon);
		CHECK_CLOSE(0.0, contact.geom.pos[1], dEpsilon);
		CHECK_CLOSE(0.0, contact.geom.pos[2], dEpsilon);
		CHECK_CLOSE(1.0, contact.geom.normal[0], dEpsilon);
		CHECK_CLOSE(0.0, contact.geom.normal[1], dEpsilon);
		CHECK_CLOSE(0.0, contact.geom.normal[2], dEpsilon);
		CHECK_CLOSE(9.0,contact.geom.depth, dEpsilon);


		// Move Ray
		dGeomRaySet(ray, 10, 1000, 1000, -1, 0, 0);
		// Move Geom
		dGeomSetPosition(convex, 0, 1000, 1000);

		count = dCollide(ray, convex, 1, &contact.geom, sizeof(dContact));

		CHECK_EQUAL(1, count);
		CHECK_CLOSE(1.0, contact.geom.pos[0], dEpsilon);
		CHECK_CLOSE(1000.0, contact.geom.pos[1], dEpsilon);
		CHECK_CLOSE(1000.0, contact.geom.pos[2], dEpsilon);
		CHECK_CLOSE(1.0, contact.geom.normal[0], dEpsilon);
		CHECK_CLOSE(0.0, contact.geom.normal[1], dEpsilon);
		CHECK_CLOSE(0.0, contact.geom.normal[2], dEpsilon);
		CHECK_CLOSE(9.0, contact.geom.depth, dEpsilon);

		dGeomDestroy(convex);
        dGeomDestroy(ray);
    }
}