1 /*
2 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty.  In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18 
19 #ifndef DISTANCE_TEST_H
20 #define DISTANCE_TEST_H
21 
22 class DistanceTest : public Test
23 {
24 public:
DistanceTest()25     DistanceTest()
26     {
27         {
28             m_transformA.SetIdentity();
29             m_transformA.p.Set(0.0f, -0.2f);
30             m_polygonA.SetAsBox(10.0f, 0.2f);
31         }
32 
33         {
34             m_positionB.Set(12.017401f, 0.13678508f);
35             m_angleB = -0.0109265f;
36             m_transformB.Set(m_positionB, m_angleB);
37 
38             m_polygonB.SetAsBox(2.0f, 0.1f);
39         }
40     }
41 
Create()42     static Test* Create()
43     {
44         return new DistanceTest;
45     }
46 
Step(Settings * settings)47     void Step(Settings* settings)
48     {
49         Test::Step(settings);
50 
51         b2DistanceInput input;
52         input.proxyA.Set(&m_polygonA, 0);
53         input.proxyB.Set(&m_polygonB, 0);
54         input.transformA = m_transformA;
55         input.transformB = m_transformB;
56         input.useRadii = true;
57         b2SimplexCache cache;
58         cache.count = 0;
59         b2DistanceOutput output;
60         b2Distance(&output, &cache, &input);
61 
62         m_debugDraw.DrawString(5, m_textLine, "distance = %g", output.distance);
63         m_textLine += 15;
64 
65         m_debugDraw.DrawString(5, m_textLine, "iterations = %d", output.iterations);
66         m_textLine += 15;
67 
68         {
69             b2Color color(0.9f, 0.9f, 0.9f);
70             b2Vec2 v[b2_maxPolygonVertices];
71             for (int32 i = 0; i < m_polygonA.m_vertexCount; ++i)
72             {
73                 v[i] = b2Mul(m_transformA, m_polygonA.m_vertices[i]);
74             }
75             m_debugDraw.DrawPolygon(v, m_polygonA.m_vertexCount, color);
76 
77             for (int32 i = 0; i < m_polygonB.m_vertexCount; ++i)
78             {
79                 v[i] = b2Mul(m_transformB, m_polygonB.m_vertices[i]);
80             }
81             m_debugDraw.DrawPolygon(v, m_polygonB.m_vertexCount, color);
82         }
83 
84         b2Vec2 x1 = output.pointA;
85         b2Vec2 x2 = output.pointB;
86 
87         b2Color c1(1.0f, 0.0f, 0.0f);
88         m_debugDraw.DrawPoint(x1, 4.0f, c1);
89 
90         b2Color c2(1.0f, 1.0f, 0.0f);
91         m_debugDraw.DrawPoint(x2, 4.0f, c2);
92     }
93 
Keyboard(unsigned char key)94     void Keyboard(unsigned char key)
95     {
96         switch (key)
97         {
98         case 'a':
99             m_positionB.x -= 0.1f;
100             break;
101 
102         case 'd':
103             m_positionB.x += 0.1f;
104             break;
105 
106         case 's':
107             m_positionB.y -= 0.1f;
108             break;
109 
110         case 'w':
111             m_positionB.y += 0.1f;
112             break;
113 
114         case 'q':
115             m_angleB += 0.1f * b2_pi;
116             break;
117 
118         case 'e':
119             m_angleB -= 0.1f * b2_pi;
120             break;
121         }
122 
123         m_transformB.Set(m_positionB, m_angleB);
124     }
125 
126     b2Vec2 m_positionB;
127     float32 m_angleB;
128 
129     b2Transform m_transformA;
130     b2Transform m_transformB;
131     b2PolygonShape m_polygonA;
132     b2PolygonShape m_polygonB;
133 };
134 
135 #endif
136