void Physics::makeCylinder(Mesh &cylinder) { glm::vec3 halfs; glm::vec3 centers; centers.x = (cylinder.current_position.first.x + cylinder.current_position.second.x)/2.0f; centers.y = (cylinder.current_position.first.y + cylinder.current_position.second.y)/2.0f; centers.z = (cylinder.current_position.first.z + cylinder.current_position.second.z)/2.0f; halfs.x = abs(cylinder.current_position.second.x - centers.x); halfs.y = abs(cylinder.current_position.second.y - centers.y); halfs.z = abs(cylinder.current_position.second.z - centers.z); cylinderShape = new btCylinderShape(btVector3(halfs.x, halfs.y, halfs.z)); btDefaultMotionState* cylinderMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3( cylinder.offset.x, cylinder.offset.y, cylinder.offset.z))); //create the motion state with a starting point btVector3 cylinderInertia(0,0,0); cylinderShape->calculateLocalInertia(10.0, cylinderInertia); btRigidBody::btRigidBodyConstructionInfo cylinderRigidBodyCI( 10.0, //mass cylinderMotionState, //initial position cylinderShape, //collision shape of body cylinderInertia); //local inertia cylinderRigidBodyCI.m_restitution = 0.5; cylinderRigidBodyCI.m_friction = 0.5; simulationCylinder = new btRigidBody(cylinderRigidBodyCI); simulationCylinder->setActivationState(DISABLE_DEACTIVATION); dynamicsWorld->addRigidBody(simulationCylinder); }
void Physics::makePaddle(Mesh &paddle) { glm::vec3 centers; glm::vec3 halfs; centers.x = (paddle.current_position.first.x + paddle.current_position.second.x)/2.0f; centers.y = (paddle.current_position.first.y + paddle.current_position.second.y)/2.0f; centers.z = (paddle.current_position.first.z + paddle.current_position.second.z)/2.0f; halfs.x = abs(paddle.current_position.second.x - centers.x); halfs.y = abs(paddle.current_position.second.y - centers.y); halfs.z = abs(paddle.current_position.second.z - centers.z); std::cout << "Paddle centers: " << glm::to_string(centers) << std::endl; std::cout << "Paddle halfs: " << glm::to_string(halfs) << std::endl; //create collision shape paddleShape = new btCylinderShape(btVector3(halfs.x, halfs.y, halfs.z)); btDefaultMotionState* paddleMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3( halfs.x, halfs.y, halfs.z))); btVector3 cylinderInertia(0,0,0); paddleShape->calculateLocalInertia(6.0, cylinderInertia); //set Cylinder initial parameters btRigidBody::btRigidBodyConstructionInfo cylinderRigidBodyCI( 6.0, //mass paddleMotionState, //initial position paddleShape, //collision shape of body cylinderInertia); //local inertia cylinderRigidBodyCI.m_restitution = 0.0; cylinderRigidBodyCI.m_friction = 0.5; simulationPaddle = new btRigidBody(cylinderRigidBodyCI); simulationPaddle->setActivationState(DISABLE_DEACTIVATION); dynamicsWorld->addRigidBody(simulationPaddle); }
// MAIN FUNCTION int main(int argc, char **argv) { bool init = false; // Initialize glut glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(w, h); // Name and create the Window glutCreateWindow("Dope Lighting Demo"); // Now that the window is created the GL context is fully set up // Because of that we can now initialize GLEW to prepare work with shaders GLenum status = glewInit(); if( status != GLEW_OK) { std::cerr << "[F] GLEW NOT INITIALIZED: "; std::cerr << glewGetErrorString(status) << std::endl; return -1; } // Set all of the callbacks to GLUT that we need glutDisplayFunc(render);// Called when its time to display glutReshapeFunc(reshape);// Called if the window is resized glutIdleFunc(update);// Called if there is nothing else to do glutKeyboardFunc(keyboard);// Called if there is keyboard input glutMouseFunc(mouse);//Called if there is mouse input glutSpecialFunc(ArrowKeys); int index = glutCreateMenu(Menu1); glutAddMenuEntry("Rotate Clockwise", 1); glutAddMenuEntry("Rotate Counterclockwise", 2); glutAddMenuEntry("Don't Rotate", 3); glutCreateMenu(Menu2); glutAddSubMenu("Rotation options", index); glutAddMenuEntry("Exit Program", 2); glutAttachMenu(GLUT_RIGHT_BUTTON); srand(getDT()); // add menus manageMenus( false ); ////////////////////////////////////////////////////////////////////////// //create brodphase btBroadphaseInterface* broadphase = new btDbvtBroadphase(); //create collision configuration btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration(); //create a dispatcher btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration); //create a solver btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver; //create the physics world dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration); //set the gravity dynamicsWorld->setGravity(btVector3(0, -10, 0)); //create a game board which the objects will be on btCollisionShape* ground = new btStaticPlaneShape(btVector3(0, 1, 0), 1); btCollisionShape* wallOne = new btStaticPlaneShape(btVector3(-1, 0, 0), 1); btCollisionShape* wallTwo = new btStaticPlaneShape(btVector3(1, 0, 0), 1); btCollisionShape* wallThree = new btStaticPlaneShape(btVector3(0, 0, 1), 1); btCollisionShape* wallFour = new btStaticPlaneShape(btVector3(0, 0, -1), 1); //create sphere and set radius to 1 btCollisionShape* sphere = new btSphereShape(1); //create cube and set extents to 0.5 each btCollisionShape* cube = new btBoxShape(btVector3(0.5,0.5,0.5)); //create a cylinder and set radius of each axis to 1.0 btCollisionShape* cylinder = new btCylinderShape(btVector3(1.0,1.0,1.0)); /*----------------------this is the gameboard--------------------------------*/ // After we create collision shapes we have to se the default motion state // for the ground btDefaultMotionState* groundMotionState = NULL; groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, -1, 0))); //here we construct the ground using the motion state and shape btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, groundMotionState, ground, btVector3(0, 0, 0)); btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI); //display dynamic body in our world dynamicsWorld->addRigidBody(groundRigidBody); ////make the first wall btDefaultMotionState* wallOneMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(5.6, 0, 0))); //here we construct the first wall using the motion state and shape btRigidBody::btRigidBodyConstructionInfo wallOneRigidBodyCI(0, wallOneMotionState, wallOne, btVector3(0, 0, 0)); btRigidBody* wallOneRigidBody = new btRigidBody(wallOneRigidBodyCI); //display dynamic body in our world dynamicsWorld->addRigidBody(wallOneRigidBody); ////make the second wall btDefaultMotionState* wallTwoMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(-5.6, 0, 0))); //here we construct the second wall using the motion state and shape btRigidBody::btRigidBodyConstructionInfo wallTwoRigidBodyCI(0, wallTwoMotionState, wallTwo, btVector3(0, 0, 0)); btRigidBody* wallTwoRigidBody = new btRigidBody(wallTwoRigidBodyCI); //display dynamic body in our world dynamicsWorld->addRigidBody(wallTwoRigidBody); ////make the third wall FRONTBACK btDefaultMotionState* wallThreeMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, -5.6))); //here we construct the third wall using the motion state and shape btRigidBody::btRigidBodyConstructionInfo wallThreeRigidBodyCI(0, wallThreeMotionState, wallThree, btVector3(0, 0, 0)); btRigidBody* wallThreeRigidBody = new btRigidBody(wallThreeRigidBodyCI); //display dynamic body in our world dynamicsWorld->addRigidBody(wallThreeRigidBody); ////make the fouth wall FRONTBACK btDefaultMotionState* wallFourMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 5.6))); //here we construct the fourth wall using the motion state and shape btRigidBody::btRigidBodyConstructionInfo wallFourRigidBodyCI(0, wallFourMotionState, wallFour, btVector3(0, 0, 0)); btRigidBody* wallFourRigidBody = new btRigidBody(wallFourRigidBodyCI); //display dynamic body in our world dynamicsWorld->addRigidBody(wallFourRigidBody); /*-----------------------------------------------------------------------------*/ /*----------------------this is the sphere--------------------------------*/ // After we create collision shapes we have to se the default motion state // for the sphere btDefaultMotionState* sphereMotionState = NULL; sphereMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(2, 1, 0))); // the sphere must have a mass btScalar mass = 1; //we need the inertia of the sphere and we need to calculate it btVector3 sphereInertia(0, 0, 0); sphere->calculateLocalInertia(mass, sphereInertia); //Here we construct the sphere with a mass, motion state, and inertia btRigidBody::btRigidBodyConstructionInfo sphereRigidBodyCI(mass, sphereMotionState, sphere, sphereInertia); rigidBodySphere = new btRigidBody(sphereRigidBodyCI); rigidBodySphere->setActivationState(DISABLE_DEACTIVATION); //display dynamic body in our world dynamicsWorld->addRigidBody(rigidBodySphere); /*-----------------------------------------------------------------------------*/ /*----------------------this is the cube--------------------------------*/ // After we create collision shapes we have to se the default motion state // for the cube btDefaultMotionState* cubeMotionState = NULL; cubeMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 1, 0))); //the cube must have a mass mass = 0; //we need the inertia of the cube and we need to calculate it btVector3 cubeInertia(0, 0, 0); cube->calculateLocalInertia(mass, cubeInertia); //Here we construct the cube with a mass, motion state, and inertia btRigidBody::btRigidBodyConstructionInfo cubeRigidBodyCI(mass, cubeMotionState, sphere, sphereInertia); rigidBodyCube = new btRigidBody(cubeRigidBodyCI); // this is where we make a static object (like the cube) into a kinematic object rigidBodyCube->setCollisionFlags(rigidBodyCube->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); rigidBodyCube->setActivationState(DISABLE_DEACTIVATION); //display dynamic body in our world dynamicsWorld->addRigidBody(rigidBodyCube); /*-----------------------------------------------------------------------------*/ /*----------------------this is the cylinder--------------------------------*/ // After we create collision shapes we have to se the default motion state // for the cylinder btDefaultMotionState* cylinderMotionState = NULL; cylinderMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(1, 20, 1))); //the cylinder must have a mass mass = 1; //we need the inertia of the cylinder and we need to calculate it btVector3 cylinderInertia(0, 0, 0); cylinder->calculateLocalInertia(mass, cylinderInertia); //Here we construct the cylinder with a mass, motion state, and inertia btRigidBody::btRigidBodyConstructionInfo cylinderRigidBodyCI(mass, cylinderMotionState, cylinder, sphereInertia); rigidBodyCylinder = new btRigidBody(cylinderRigidBodyCI); rigidBodyCylinder->setActivationState(DISABLE_DEACTIVATION); //display dynamic body in our world dynamicsWorld->addRigidBody(rigidBodyCylinder); /*-----------------------------------------------------------------------------*/ ///////////////////////////////////////////////////////////////////////////// // Initialize all of our resources(shaders, geometry) // pass default planet info if not given one if( argc != 3 ) { init = initialize( defaultInfo ); } // or, pass planet info given from command line argument else { init = initialize( argv[1] ); } // if initialized, begin glut main loop if(init) { t1 = std::chrono::high_resolution_clock::now(); glutMainLoop(); } // remove menus manageMenus( true ); //////////// clean up and end program // delete the pointers dynamicsWorld->removeRigidBody(groundRigidBody); delete groundRigidBody->getMotionState(); delete groundRigidBody; dynamicsWorld->removeRigidBody(rigidBodySphere); delete rigidBodySphere->getMotionState(); delete rigidBodySphere; dynamicsWorld->removeRigidBody(rigidBodyCube); delete rigidBodyCube->getMotionState(); delete rigidBodyCube; dynamicsWorld->removeRigidBody(rigidBodyCylinder); delete rigidBodyCylinder->getMotionState(); delete rigidBodyCylinder; dynamicsWorld->removeRigidBody(wallOneRigidBody); delete wallOneRigidBody->getMotionState(); delete wallOneRigidBody; dynamicsWorld->removeRigidBody(wallTwoRigidBody); delete wallTwoRigidBody->getMotionState(); delete wallTwoRigidBody; dynamicsWorld->removeRigidBody(wallThreeRigidBody); delete wallThreeRigidBody->getMotionState(); delete wallThreeRigidBody; dynamicsWorld->removeRigidBody(wallFourRigidBody); delete wallFourRigidBody->getMotionState(); delete wallFourRigidBody; delete ground; delete wallOne; delete wallTwo; delete wallThree; delete wallFour; delete sphere; delete cube; delete cylinder; delete dynamicsWorld; delete solver; delete collisionConfiguration; delete dispatcher; delete broadphase; cleanUp(); return 0; }