1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #include "test.h"
24 
25 class ContinuousTest : public Test
26 {
27 public:
28 
ContinuousTest()29 	ContinuousTest()
30 	{
31 		{
32 			b2BodyDef bd;
33 			bd.position.Set(0.0f, 0.0f);
34 			b2Body* body = m_world->CreateBody(&bd);
35 
36 			b2EdgeShape edge;
37 
38 			edge.SetTwoSided(b2Vec2(-10.0f, 0.0f), b2Vec2(10.0f, 0.0f));
39 			body->CreateFixture(&edge, 0.0f);
40 
41 			b2PolygonShape shape;
42 			shape.SetAsBox(0.2f, 1.0f, b2Vec2(0.5f, 1.0f), 0.0f);
43 			body->CreateFixture(&shape, 0.0f);
44 		}
45 
46 #if 1
47 		{
48 			b2BodyDef bd;
49 			bd.type = b2_dynamicBody;
50 			bd.position.Set(0.0f, 20.0f);
51 			//bd.angle = 0.1f;
52 
53 			b2PolygonShape shape;
54 			shape.SetAsBox(2.0f, 0.1f);
55 
56 			m_body = m_world->CreateBody(&bd);
57 			m_body->CreateFixture(&shape, 1.0f);
58 
59 			m_angularVelocity = RandomFloat(-50.0f, 50.0f);
60 			//m_angularVelocity = 46.661274f;
61 			m_body->SetLinearVelocity(b2Vec2(0.0f, -100.0f));
62 			m_body->SetAngularVelocity(m_angularVelocity);
63 		}
64 #else
65 		{
66 			b2BodyDef bd;
67 			bd.type = b2_dynamicBody;
68 			bd.position.Set(0.0f, 2.0f);
69 			b2Body* body = m_world->CreateBody(&bd);
70 
71 			b2CircleShape shape;
72 			shape.m_p.SetZero();
73 			shape.m_radius = 0.5f;
74 			body->CreateFixture(&shape, 1.0f);
75 
76 			bd.bullet = true;
77 			bd.position.Set(0.0f, 10.0f);
78 			body = m_world->CreateBody(&bd);
79 			body->CreateFixture(&shape, 1.0f);
80 			body->SetLinearVelocity(b2Vec2(0.0f, -100.0f));
81 		}
82 #endif
83 
84 		extern B2_API int32 b2_gjkCalls, b2_gjkIters, b2_gjkMaxIters;
85 		extern B2_API int32 b2_toiCalls, b2_toiIters;
86 		extern B2_API int32 b2_toiRootIters, b2_toiMaxRootIters;
87 		extern B2_API float b2_toiTime, b2_toiMaxTime;
88 
89 		b2_gjkCalls = 0; b2_gjkIters = 0; b2_gjkMaxIters = 0;
90 		b2_toiCalls = 0; b2_toiIters = 0;
91 		b2_toiRootIters = 0; b2_toiMaxRootIters = 0;
92 		b2_toiTime = 0.0f; b2_toiMaxTime = 0.0f;
93 	}
94 
Launch()95 	void Launch()
96 	{
97 		extern B2_API int32 b2_gjkCalls, b2_gjkIters, b2_gjkMaxIters;
98 		extern B2_API int32 b2_toiCalls, b2_toiIters;
99 		extern B2_API int32 b2_toiRootIters, b2_toiMaxRootIters;
100 		extern B2_API float b2_toiTime, b2_toiMaxTime;
101 
102 		b2_gjkCalls = 0; b2_gjkIters = 0; b2_gjkMaxIters = 0;
103 		b2_toiCalls = 0; b2_toiIters = 0;
104 		b2_toiRootIters = 0; b2_toiMaxRootIters = 0;
105 		b2_toiTime = 0.0f; b2_toiMaxTime = 0.0f;
106 
107 		m_body->SetTransform(b2Vec2(0.0f, 20.0f), 0.0f);
108 		m_angularVelocity = RandomFloat(-50.0f, 50.0f);
109 		m_body->SetLinearVelocity(b2Vec2(0.0f, -100.0f));
110 		m_body->SetAngularVelocity(m_angularVelocity);
111 	}
112 
Step(Settings & settings)113 	void Step(Settings& settings) override
114 	{
115 		Test::Step(settings);
116 
117 		extern B2_API int32 b2_gjkCalls, b2_gjkIters, b2_gjkMaxIters;
118 
119 		if (b2_gjkCalls > 0)
120 		{
121 			g_debugDraw.DrawString(5, m_textLine, "gjk calls = %d, ave gjk iters = %3.1f, max gjk iters = %d",
122 				b2_gjkCalls, b2_gjkIters / float(b2_gjkCalls), b2_gjkMaxIters);
123 			m_textLine += m_textIncrement;
124 		}
125 
126 		extern B2_API int32 b2_toiCalls, b2_toiIters;
127 		extern B2_API int32 b2_toiRootIters, b2_toiMaxRootIters;
128 		extern B2_API float b2_toiTime, b2_toiMaxTime;
129 
130 		if (b2_toiCalls > 0)
131 		{
132 			g_debugDraw.DrawString(5, m_textLine, "toi calls = %d, ave [max] toi iters = %3.1f [%d]",
133 								b2_toiCalls, b2_toiIters / float(b2_toiCalls), b2_toiMaxRootIters);
134 			m_textLine += m_textIncrement;
135 
136 			g_debugDraw.DrawString(5, m_textLine, "ave [max] toi root iters = %3.1f [%d]",
137 				b2_toiRootIters / float(b2_toiCalls), b2_toiMaxRootIters);
138 			m_textLine += m_textIncrement;
139 
140 			g_debugDraw.DrawString(5, m_textLine, "ave [max] toi time = %.1f [%.1f] (microseconds)",
141 				1000.0f * b2_toiTime / float(b2_toiCalls), 1000.0f * b2_toiMaxTime);
142 			m_textLine += m_textIncrement;
143 		}
144 
145 		if (m_stepCount % 60 == 0)
146 		{
147 			//Launch();
148 		}
149 	}
150 
Create()151 	static Test* Create()
152 	{
153 		return new ContinuousTest;
154 	}
155 
156 	b2Body* m_body;
157 	float m_angularVelocity;
158 };
159 
160 static int testIndex = RegisterTest("Continuous", "Continuous Test", ContinuousTest::Create);
161