1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2015 Google Inc. http://bulletphysics.org
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages 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 freely,
9 subject to the following restrictions:
10 
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15 
16 #include "Chain.h"
17 
18 #include "btBulletDynamicsCommon.h"
19 #include "LinearMath/btVector3.h"
20 #include "LinearMath/btAlignedObjectArray.h"
21 #include "../CommonInterfaces/CommonRigidBodyBase.h"
22 
23 const int TOTAL_BOXES = 10;
24 struct ChainExample : public CommonRigidBodyBase
25 {
ChainExampleChainExample26 	ChainExample(struct GUIHelperInterface* helper)
27 		: CommonRigidBodyBase(helper)
28 	{
29 	}
~ChainExampleChainExample30 	virtual ~ChainExample() {}
31 	virtual void initPhysics();
32 	virtual void renderScene();
resetCameraChainExample33 	void resetCamera()
34 	{
35 		float dist = 41;
36 		float pitch = -35;
37 		float yaw = 52;
38 		float targetPos[3] = {0, 0.46, 0};
39 		m_guiHelper->resetCamera(dist, yaw, pitch, targetPos[0], targetPos[1], targetPos[2]);
40 	}
41 };
42 
initPhysics()43 void ChainExample::initPhysics()
44 {
45 	m_guiHelper->setUpAxis(1);
46 
47 	createEmptyDynamicsWorld();
48 
49 	m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
50 
51 	if (m_dynamicsWorld->getDebugDrawer())
52 		m_dynamicsWorld->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe + btIDebugDraw::DBG_DrawContactPoints);
53 
54 	///create a few basic rigid bodies
55 	btBoxShape* groundShape = createBoxShape(btVector3(btScalar(50.), btScalar(50.), btScalar(50.)));
56 	m_collisionShapes.push_back(groundShape);
57 
58 	btTransform groundTransform;
59 	groundTransform.setIdentity();
60 	groundTransform.setOrigin(btVector3(0, -50, 0));
61 	{
62 		btScalar mass(0.);
63 		createRigidBody(mass, groundTransform, groundShape, btVector4(0, 0, 1, 1));
64 	}
65 
66 	{
67 		//create a few dynamic rigidbodies
68 		// Re-using the same collision is better for memory usage and performance
69 		btBoxShape* colShape = createBoxShape(btVector3(1, 1, 0.25));
70 
71 		m_collisionShapes.push_back(colShape);
72 
73 		/// Create Dynamic Objects
74 		btTransform startTransform;
75 		startTransform.setIdentity();
76 
77 		btScalar mass(1.f);
78 
79 		//rigidbody is dynamic if and only if mass is non zero, otherwise static
80 		bool isDynamic = (mass != 0.f);
81 
82 		btVector3 localInertia(0, 0, 0);
83 		if (isDynamic)
84 			colShape->calculateLocalInertia(mass, localInertia);
85 
86 		btAlignedObjectArray<btRigidBody*> boxes;
87 		int lastBoxIndex = TOTAL_BOXES - 1;
88 		for (int i = 0; i < TOTAL_BOXES; ++i)
89 		{
90 			startTransform.setOrigin(btVector3(
91 				btScalar(0),
92 				btScalar(5 + i * 2),
93 				btScalar(0)));
94 			boxes.push_back(createRigidBody((i == lastBoxIndex) ? 0 : mass, startTransform, colShape));
95 		}
96 
97 		//add N-1 spring constraints
98 		for (int i = 0; i < TOTAL_BOXES - 1; ++i)
99 		{
100 			btRigidBody* b1 = boxes[i];
101 			btRigidBody* b2 = boxes[i + 1];
102 
103 			btPoint2PointConstraint* leftSpring = new btPoint2PointConstraint(*b1, *b2, btVector3(-0.5, 1, 0), btVector3(-0.5, -1, 0));
104 
105 			m_dynamicsWorld->addConstraint(leftSpring);
106 
107 			btPoint2PointConstraint* rightSpring = new btPoint2PointConstraint(*b1, *b2, btVector3(0.5, 1, 0), btVector3(0.5, -1, 0));
108 
109 			m_dynamicsWorld->addConstraint(rightSpring);
110 		}
111 	}
112 
113 	m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
114 }
115 
renderScene()116 void ChainExample::renderScene()
117 {
118 	CommonRigidBodyBase::renderScene();
119 }
120 
ET_ChainCreateFunc(CommonExampleOptions & options)121 CommonExampleInterface* ET_ChainCreateFunc(CommonExampleOptions& options)
122 {
123 	return new ChainExample(options.m_guiHelper);
124 }
125