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 "SimpleBox.h"
17 
18 #include "btBulletDynamicsCommon.h"
19 #include "LinearMath/btVector3.h"
20 #include "LinearMath/btAlignedObjectArray.h"
21 #include "../CommonInterfaces/CommonRigidBodyBase.h"
22 
23 struct SimpleBoxExample : public CommonRigidBodyBase
24 {
SimpleBoxExampleSimpleBoxExample25 	SimpleBoxExample(struct GUIHelperInterface* helper)
26 		: CommonRigidBodyBase(helper)
27 	{
28 	}
~SimpleBoxExampleSimpleBoxExample29 	virtual ~SimpleBoxExample() {}
30 	virtual void initPhysics();
31 	virtual void renderScene();
resetCameraSimpleBoxExample32 	void resetCamera()
33 	{
34 		float dist = 41;
35 		float pitch = -35;
36 		float yaw = 52;
37 		float targetPos[3] = {0, 0.46, 0};
38 		m_guiHelper->resetCamera(dist, yaw, pitch, targetPos[0], targetPos[1], targetPos[2]);
39 	}
40 };
41 
initPhysics()42 void SimpleBoxExample::initPhysics()
43 {
44 	m_guiHelper->setUpAxis(1);
45 
46 	createEmptyDynamicsWorld();
47 
48 	m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
49 
50 	if (m_dynamicsWorld->getDebugDrawer())
51 		m_dynamicsWorld->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe + btIDebugDraw::DBG_DrawContactPoints);
52 
53 	///create a few basic rigid bodies
54 	btBoxShape* groundShape = createBoxShape(btVector3(btScalar(50.), btScalar(50.), btScalar(50.)));
55 	m_collisionShapes.push_back(groundShape);
56 
57 	btTransform groundTransform;
58 	groundTransform.setIdentity();
59 	groundTransform.setOrigin(btVector3(0, -50, 0));
60 	{
61 		btScalar mass(0.);
62 		createRigidBody(mass, groundTransform, groundShape, btVector4(0, 0, 1, 1));
63 	}
64 
65 	{
66 		//create a few dynamic rigidbodies
67 		// Re-using the same collision is better for memory usage and performance
68 		btBoxShape* colShape = createBoxShape(btVector3(1, 1, 1));
69 
70 		m_collisionShapes.push_back(colShape);
71 
72 		/// Create Dynamic Objects
73 		btTransform startTransform;
74 		startTransform.setIdentity();
75 
76 		btScalar mass(1.f);
77 
78 		//rigidbody is dynamic if and only if mass is non zero, otherwise static
79 		bool isDynamic = (mass != 0.f);
80 
81 		btVector3 localInertia(0, 0, 0);
82 		if (isDynamic)
83 			colShape->calculateLocalInertia(mass, localInertia);
84 
85 		startTransform.setOrigin(btVector3(
86 			btScalar(0),
87 			btScalar(20),
88 			btScalar(0)));
89 		createRigidBody(mass, startTransform, colShape);
90 	}
91 
92 	m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
93 }
94 
renderScene()95 void SimpleBoxExample::renderScene()
96 {
97 	CommonRigidBodyBase::renderScene();
98 }
99 
ET_SimpleBoxCreateFunc(CommonExampleOptions & options)100 CommonExampleInterface* ET_SimpleBoxCreateFunc(CommonExampleOptions& options)
101 {
102 	return new SimpleBoxExample(options.m_guiHelper);
103 }
104