Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 29/Sep/2010 Umer Noor
- Edited original BasicDemo file
- Starting point for using Bullet's debugDrawer.
- For now we are not using Bullet for physics.
- */
- /*
- Bullet Continuous Collision Detection and Physics Library
- Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
- This software is provided 'as-is', without any express or implied warranty.
- In no event will the authors be held liable for any damages arising from the use of this software.
- Permission is granted to anyone to use this software for any purpose,
- including commercial applications, and to alter it and redistribute it freely,
- subject to the following restrictions:
- 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.
- 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
- 3. This notice may not be removed or altered from any source distribution.
- */
- #include "BasicDemo.h"
- #include "GlutStuff.h"
- ///btBulletDynamicsCommon.h is the main Bullet include file, contains most common include files.
- #include "btBulletDynamicsCommon.h"
- #include <stdio.h> //printf debugging
- #include <iostream>
- float fRadius = 1.0;
- const float fCoefficientOfRestitution = 1.0;
- const float fMass = 0.5;
- bool isColliding = false;
- btVector3 rotationalVec;
- btVector3 angVel;
- btVector3 upVec = btVector3(0.0, 1.0, 0.0);
- btVector3 Fdrag = btVector3(0.0,0.0,0.0);
- btMatrix3x3 inertiaTensor = btMatrix3x3();
- void BasicDemo::clientMoveAndDisplay()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- //Enter physic code
- float fTimeStep = 1.0 / 100.0;
- btTransform startTransform;
- startTransform.setIdentity();
- inertiaTensor.setValue(btScalar((2.0f / 5.0f) * fMass * (fRadius * fRadius)), 0, 0, 0, btScalar((2.0f / 5.0f) * fMass * (fRadius * fRadius)), 0, 0, 0, btScalar((2.0f / 5.0f) * fMass * (fRadius * fRadius)));
- inertiaTensor.inverse();
- btVector3 distance = vPosition[0] - vPosition[1];
- for (int ii = 0; ii < 2; ii++)
- {
- //Collision Detection
- //Calculate Distance between both vectors
- if (distance.length() < fRadius * 2) {
- isColliding = true;
- std::cout << "Colliding!" << std::endl;
- }
- else {
- isColliding = false;
- std::cout << "Not colliding!" << std::endl;
- }
- if (vVelocity[ii] != btVector3(0, 0, 0)) {
- rotationalVec = upVec.cross(vVelocity[ii]);
- }
- else {
- rotationalVec = btVector3(0.0, 1.0, 0.0);
- }
- if (isColliding)
- {
- //Normalize
- distance.safeNormalize();
- btVector3 collnorm = vPosition[1] - (distance * fRadius);
- float vRel = collnorm.dot(vVelocity[0] - vVelocity[1]);
- float numerator = -vRel * (fCoefficientOfRestitution + 1.0f);
- //Point of collision
- btVector3 r1 = collnorm - vPosition[0];
- btVector3 r2 = collnorm - vPosition[1];
- btVector3 part1 = r1.cross(collnorm);
- part1 = inertiaTensor * part1;
- part1.cross(collnorm);
- float part1final = collnorm.dot(part1);
- btVector3 part2 = r2.cross(collnorm);
- part2 = inertiaTensor * part2;
- part2.cross(collnorm);
- float part2final = collnorm.dot(part2);
- float denominator = (1.0f / fMass) + (1.0f / fMass) + part1final + part2final;
- float impluse = numerator / denominator;
- //float impluse = (-vRel * (fCoefficientOfRestitution + 1)) / ((1.0f / fMass) + (1.0f / fMass) + collnorm.dot((inertiaTensor * (r1.cross(collnorm))).cross(r1)) + collnorm.dot((inertiaTensor * (r2.cross(collnorm))).cross(r2)));
- //Quiz 1
- //vVelocity[ii] = vVelocityInit[ii] - 2 * (vVelocityInit[ii].dot(distance) * distance);
- vVelocity[0] = vVelocity[0] + (impluse * collnorm) / fMass;
- vVelocity[1] = vVelocity[1] + (-impluse * collnorm) / fMass;
- }
- //Quiz 1
- /*Fdrag = -0.3f * vVelocity[ii];
- vAcceleration[ii] = Fdrag;*/
- vVelocityInit[ii] = vVelocity[ii];
- // Update angular velocity
- rotationalVec.safeNormalize();
- float angVelMag = vVelocity[ii].getMagnitude() / fTimeStep;
- angVel = angVelMag * rotationalVec;
- // Update orientation quaternion
- qOrientationInit[ii] = qOrientation[ii];
- qOrientation[ii] = qOrientationInit[ii] + (fTimeStep / 2) * angVel * qOrientationInit[ii] * fTimeStep;
- // Update position (ignore friction)
- vPosition[ii] += vVelocity[ii] * fTimeStep + 0.5f * vAcceleration[ii] * (fTimeStep * fTimeStep);
- vVelocity[ii] += vAcceleration[ii] * fTimeStep;
- // Input data into motionstate for each ball
- startTransform.setOrigin(vPosition[ii]);
- startTransform.setRotation(qOrientation[ii]);
- m_rigidBody[ii]->getMotionState()->setWorldTransform(startTransform);
- }
- // OpenGL calls
- m_dynamicsWorld->debugDrawWorld();
- renderme();
- glFlush();
- glutSwapBuffers();
- }
- void BasicDemo::displayCallback(void) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- renderme();
- //optional but useful: debug drawing to detect problems
- if (m_dynamicsWorld)
- m_dynamicsWorld->debugDrawWorld();
- glFlush();
- glutSwapBuffers();
- }
- void BasicDemo::initPhysics()
- {
- setTexturing(true);
- setShadows(true);
- setCameraDistance(btScalar(10.0));
- ///collision configuration contains default setup for memory, collision setup
- m_collisionConfiguration = new btDefaultCollisionConfiguration();
- ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
- m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
- m_broadphase = new btDbvtBroadphase();
- ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
- btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver;
- m_solver = sol;
- m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
- m_dynamicsWorld->setGravity(btVector3(0,-10,0));
- ///create a few basic rigid bodies
- btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
- m_collisionShapes.push_back(groundShape);
- btTransform groundTransform;
- groundTransform.setIdentity();
- groundTransform.setOrigin(btVector3(0,-50,0));
- //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
- {
- btScalar mass(0.);
- //rigidbody is dynamic if and only if mass is non zero, otherwise static
- bool isDynamic = (mass != 0.f);
- btVector3 localInertia(0,0,0);
- if (isDynamic)
- groundShape->calculateLocalInertia(mass,localInertia);
- //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
- btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
- btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
- btRigidBody* body = new btRigidBody(rbInfo);
- body->setRestitution(btScalar(0.8f));
- //add the body to the dynamics world
- m_dynamicsWorld->addRigidBody(body);
- }
- // Here are the dynamic bodies
- {
- ///btCollisionShape* colShape = new btBoxShape(btVector3(1, 1, 1));
- btCollisionShape* colShape = new btSphereShape(1.0);
- m_collisionShapes.push_back(colShape);
- /// Create Dynamic Objects
- btTransform startTransform;
- startTransform.setIdentity();
- btScalar mass(fMass);
- //rigidbody is dynamic if and only if mass is non zero, otherwise static
- bool isDynamic = (mass != 0.f);
- btVector3 localInertia(0,0,0);
- if (isDynamic)
- colShape->calculateLocalInertia(mass,localInertia);
- float start_x[] = { 0.0, 1.4 }; // collision angle
- float start_y[] = { 1.0, 1.0 };
- float start_z[] = { -4.0, 4.0 };
- // set up 2 rigid bodies and put them into the btAlignedObjectArray called m_rigidBody
- for(int ii = 0; ii < 2; ii++){
- startTransform.setOrigin(btVector3( start_x[ii], start_y[ii], start_z[ii] ));
- //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
- btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
- btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
- btRigidBody* ball = new btRigidBody(rbInfo);
- ball->setActivationState(ISLAND_SLEEPING);
- ball->setRestitution(fCoefficientOfRestitution);
- m_dynamicsWorld->addRigidBody(ball);
- ball->setActivationState(ISLAND_SLEEPING);
- m_rigidBody.push_back(ball);
- }
- float start_velx[] = { 0.0, 0.0 };
- float start_vely[] = { 0.0, 0.0 };
- float start_velz[] = { 3.0, 0.0 };
- for (int ii = 0; ii < 2; ii++) {
- qOrientation[ii].setValue(0.0, 0.0, 0.0, 1.0);
- vAngularVelocity[ii].setValue(0.0, 0.0, 0.0);
- vVelocity[ii].setValue(start_velx[ii], start_vely[ii], start_velz[ii]);
- vPosition[ii].setValue(start_x[ii], start_y[ii], start_z[ii]);
- vAcceleration[ii].setValue(0.0, 0.0, 0.0);
- vTorque[ii].setValue(0.0, 0.0, 0.0);
- }
- }
- clientResetScene();
- }
- void BasicDemo::exitPhysics()
- {
- //cleanup in the reverse order of creation/initialization
- //remove the rigidbodies from the dynamics world and delete them
- int i;
- for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
- {
- btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
- btRigidBody* body = btRigidBody::upcast(obj);
- if (body && body->getMotionState())
- {
- delete body->getMotionState();
- }
- m_dynamicsWorld->removeCollisionObject( obj );
- delete obj;
- }
- //delete collision shapes
- for (int j=0;j<m_collisionShapes.size();j++)
- {
- btCollisionShape* shape = m_collisionShapes[j];
- delete shape;
- }
- delete m_dynamicsWorld;
- delete m_solver;
- delete m_broadphase;
- delete m_dispatcher;
- delete m_collisionConfiguration;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement