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 "settings.h"
24 #include "test.h"
25 #include "imgui/imgui.h"
26 
27 // Test the prismatic joint with limits and motor options.
28 class PrismaticJoint : public Test
29 {
30 public:
PrismaticJoint()31 	PrismaticJoint()
32 	{
33 		b2Body* ground = NULL;
34 		{
35 			b2BodyDef bd;
36 			ground = m_world->CreateBody(&bd);
37 
38 			b2EdgeShape shape;
39 			shape.SetTwoSided(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
40 			ground->CreateFixture(&shape, 0.0f);
41 		}
42 
43 		m_enableLimit = true;
44 		m_enableMotor = false;
45 		m_motorSpeed = 10.0f;
46 
47 		{
48 			b2PolygonShape shape;
49 			shape.SetAsBox(1.0f, 1.0f);
50 
51 			b2BodyDef bd;
52 			bd.type = b2_dynamicBody;
53 			bd.position.Set(0.0f, 10.0f);
54 			bd.angle = 0.5f * b2_pi;
55 			bd.allowSleep = false;
56 			b2Body* body = m_world->CreateBody(&bd);
57 			body->CreateFixture(&shape, 5.0f);
58 
59 			b2PrismaticJointDef pjd;
60 
61 			// Horizontal
62 			pjd.Initialize(ground, body, bd.position, b2Vec2(1.0f, 0.0f));
63 
64 			pjd.motorSpeed = m_motorSpeed;
65 			pjd.maxMotorForce = 10000.0f;
66 			pjd.enableMotor = m_enableMotor;
67 			pjd.lowerTranslation = -10.0f;
68 			pjd.upperTranslation = 10.0f;
69 			pjd.enableLimit = m_enableLimit;
70 
71 			m_joint = (b2PrismaticJoint*)m_world->CreateJoint(&pjd);
72 		}
73 	}
74 
UpdateUI()75 	void UpdateUI() override
76 	{
77 		ImGui::SetNextWindowPos(ImVec2(10.0f, 100.0f));
78 		ImGui::SetNextWindowSize(ImVec2(200.0f, 100.0f));
79 		ImGui::Begin("Joint Controls", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
80 
81 		if (ImGui::Checkbox("Limit", &m_enableLimit))
82 		{
83 			m_joint->EnableLimit(m_enableLimit);
84 		}
85 
86 		if (ImGui::Checkbox("Motor", &m_enableMotor))
87 		{
88 			m_joint->EnableMotor(m_enableMotor);
89 		}
90 
91 		if (ImGui::SliderFloat("Speed", &m_motorSpeed, -100.0f, 100.0f, "%.0f"))
92 		{
93 			m_joint->SetMotorSpeed(m_motorSpeed);
94 		}
95 
96 		ImGui::End();
97 	}
98 
Step(Settings & settings)99 	void Step(Settings& settings) override
100 	{
101 		Test::Step(settings);
102 		float force = m_joint->GetMotorForce(settings.m_hertz);
103 		g_debugDraw.DrawString(5, m_textLine, "Motor Force = %4.0f", force);
104 		m_textLine += m_textIncrement;
105 	}
106 
Create()107 	static Test* Create()
108 	{
109 		return new PrismaticJoint;
110 	}
111 
112 	b2PrismaticJoint* m_joint;
113 	float m_motorSpeed;
114 	bool m_enableMotor;
115 	bool m_enableLimit;
116 };
117 
118 static int testIndex = RegisterTest("Joints", "Prismatic", PrismaticJoint::Create);
119