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 TIME_OF_IMPACT_H
20 #define TIME_OF_IMPACT_H
21 
22 class TimeOfImpact : public Test
23 {
24 public:
TimeOfImpact()25 	TimeOfImpact()
26 	{
27 		m_shapeA.SetAsBox(25.0f, 5.0f);
28 		m_shapeB.SetAsBox(2.5f, 2.5f);
29 	}
30 
Create()31 	static Test* Create()
32 	{
33 		return new TimeOfImpact;
34 	}
35 
Step(Settings * settings)36 	void Step(Settings* settings)
37 	{
38 		Test::Step(settings);
39 
40 		b2Sweep sweepA;
41 		sweepA.c0.Set(24.0f, -60.0f);
42 		sweepA.a0 = 2.95f;
43 		sweepA.c = sweepA.c0;
44 		sweepA.a = sweepA.a0;
45 		sweepA.localCenter.SetZero();
46 
47 		b2Sweep sweepB;
48 		sweepB.c0.Set(53.474274f, -50.252514f);
49 		sweepB.a0 = 513.36676f; // - 162.0f * b2_pi;
50 		sweepB.c.Set(54.595478f, -51.083473f);
51 		sweepB.a = 513.62781f; //  - 162.0f * b2_pi;
52 		sweepB.localCenter.SetZero();
53 
54 		//sweepB.a0 -= 300.0f * b2_pi;
55 		//sweepB.a -= 300.0f * b2_pi;
56 
57 		b2TOIInput input;
58 		input.proxyA.Set(&m_shapeA, 0);
59 		input.proxyB.Set(&m_shapeB, 0);
60 		input.sweepA = sweepA;
61 		input.sweepB = sweepB;
62 		input.tMax = 1.0f;
63 
64 		b2TOIOutput output;
65 
66 		b2TimeOfImpact(&output, &input);
67 
68 		m_debugDraw.DrawString(5, m_textLine, "toi = %g", output.t);
69 		m_textLine += 15;
70 
71 		extern int32 b2_toiMaxIters, b2_toiMaxRootIters;
72 		m_debugDraw.DrawString(5, m_textLine, "max toi iters = %d, max root iters = %d", b2_toiMaxIters, b2_toiMaxRootIters);
73 		m_textLine += 15;
74 
75 		b2Vec2 vertices[b2_maxPolygonVertices];
76 
77 		b2Transform transformA;
78 		sweepA.GetTransform(&transformA, 0.0f);
79 		for (int32 i = 0; i < m_shapeA.m_vertexCount; ++i)
80 		{
81 			vertices[i] = b2Mul(transformA, m_shapeA.m_vertices[i]);
82 		}
83 		m_debugDraw.DrawPolygon(vertices, m_shapeA.m_vertexCount, b2Color(0.9f, 0.9f, 0.9f));
84 
85 		b2Transform transformB;
86 		sweepB.GetTransform(&transformB, 0.0f);
87 
88 		b2Vec2 localPoint(2.0f, -0.1f);
89 		b2Vec2 rB = b2Mul(transformB, localPoint) - sweepB.c0;
90 		float32 wB = sweepB.a - sweepB.a0;
91 		b2Vec2 vB = sweepB.c - sweepB.c0;
92 		b2Vec2 v = vB + b2Cross(wB, rB);
93 
94 		for (int32 i = 0; i < m_shapeB.m_vertexCount; ++i)
95 		{
96 			vertices[i] = b2Mul(transformB, m_shapeB.m_vertices[i]);
97 		}
98 		m_debugDraw.DrawPolygon(vertices, m_shapeB.m_vertexCount, b2Color(0.5f, 0.9f, 0.5f));
99 
100 		sweepB.GetTransform(&transformB, output.t);
101 		for (int32 i = 0; i < m_shapeB.m_vertexCount; ++i)
102 		{
103 			vertices[i] = b2Mul(transformB, m_shapeB.m_vertices[i]);
104 		}
105 		m_debugDraw.DrawPolygon(vertices, m_shapeB.m_vertexCount, b2Color(0.5f, 0.7f, 0.9f));
106 
107 		sweepB.GetTransform(&transformB, 1.0f);
108 		for (int32 i = 0; i < m_shapeB.m_vertexCount; ++i)
109 		{
110 			vertices[i] = b2Mul(transformB, m_shapeB.m_vertices[i]);
111 		}
112 		m_debugDraw.DrawPolygon(vertices, m_shapeB.m_vertexCount, b2Color(0.9f, 0.5f, 0.5f));
113 
114 #if 0
115 		for (float32 t = 0.0f; t < 1.0f; t += 0.1f)
116 		{
117 			sweepB.GetTransform(&transformB, t);
118 			for (int32 i = 0; i < m_shapeB.m_vertexCount; ++i)
119 			{
120 				vertices[i] = b2Mul(transformB, m_shapeB.m_vertices[i]);
121 			}
122 			m_debugDraw.DrawPolygon(vertices, m_shapeB.m_vertexCount, b2Color(0.9f, 0.5f, 0.5f));
123 		}
124 #endif
125 	}
126 
127 	b2PolygonShape m_shapeA;
128 	b2PolygonShape m_shapeB;
129 };
130 
131 #endif
132