void jfBoxesAndBallsEventHandler_x86::handleMouseEvents() { int x,y; if(SDL_GetMouseState(&x, &y) & SDL_BUTTON(1)) { if(! m_WasMouse1Pressed) { m_LastX = x; m_LastY = y; m_WasMouse1Pressed = true; } int diffx= x-m_LastX; int diffy= y-m_LastY; m_LastX=x; m_LastY=y; jfVector3_x86 camRot, yRot, xRot; cam->getRot(&camRot); camRot.add(jfVector3_x86((float) diffy, 0.0, 0.0), &xRot); camRot.add(jfVector3_x86(0.0, (float) diffx, 0.0), &yRot); cam->setRot(xRot); cam->setRot(yRot); }else{ m_WasMouse1Pressed = false; } }
bool jfBigBallisticSimulation_x86::init() { if(! m_WindowManager->createWindow(800, 600, 32, false, "Ballistic Simulation")) { return false; } m_EventHandler->setWindowManager(m_WindowManager); m_3DGraphicsHandler->init(); m_3DGraphicsHandler->setAmmo(&m_Ammo); m_3DGraphicsHandler->setBoxes(&m_Boxes); vector<jfAmmoRound_x86*>::iterator it; for( it = m_Ammo.begin() ; it != m_Ammo.end() ; ++it) { (*it)->setType(UNUSED); } // Initialise the boxes vector<jfBox_x86*>::iterator box; jfReal z = 50.0f; for (box = m_Boxes.begin(); box != m_Boxes.end() ; box++) { jfVector3_x86 pos = jfVector3_x86(0,1.5,z); (*box)->setState(pos); z+=1; } m_Timer->start(); m_MousePressedLast = false; return true; }
void jfBigBallisticSimulation_x86::generateContacts() { // Create the ground plane data jfCollisionPlane_x86 plane; plane.setDirection(jfVector3_x86(0,1,0)); plane.setOffset(0); // Set up the collision data structure m_CollisionData->reset(); m_CollisionData->setFriction((jfReal)0.01); m_CollisionData->setRestitution((jfReal)0.01); m_CollisionData->setTolerance((jfReal)0.001); vector<jfBox_x86*>::iterator box, otherBox; // Check ground plane collisions for (box = m_Boxes.begin(); box != m_Boxes.end() ; box++) { m_CollisionDetector->boxAndHalfSpace((*(*box)), plane, m_CollisionData); // Check for collisions with each shot vector<jfAmmoRound_x86*>::iterator shot; for (shot = m_Ammo.begin(); shot != m_Ammo.end() ; shot++) { if ((*shot)->getType() != UNUSED) { // When we get a collision, remove the shot if (m_CollisionDetector->boxAndSphere((*(*box)), (*(*shot)), m_CollisionData)) { (*shot)->setType(UNUSED); } } } } // NB We aren't checking box-box collisions. }
void jfBoxesAndBallsSimulation_x86::generateContacts() { vector<jfBox *>::iterator box, otherBox; vector<jfBall *>::iterator ball, otherBall; m_Plane->setDirection(jfVector3_x86(0, 1, 0)); m_Plane->setOffset(0); // Set up the collision data structure m_CollisionData->reset(); m_CollisionData->setFriction((jfReal)0.4); m_CollisionData->setRestitution((jfReal)0.0001); m_CollisionData->setTolerance((jfReal)0.001); //m_CollisionDetector->boxAndBoxBatch(m_Boxes, m_CollisionData); for (box = m_Boxes.begin(); box != m_Boxes.end(); box++) { // Check ground plane collisions m_CollisionDetector->boxAndHalfSpace((*(*box)), (*m_Plane), m_CollisionData); // Check for collisions with each ball for (ball = m_Balls.begin(); ball != m_Balls.end(); ball++) { if (m_CollisionDetector->boxAndSphere((*(*box)), (*(*ball)), m_CollisionData)) { cout << "Box and Sphere Collision." << endl; } } /* //Check for collisions with other boxes for(otherBox = m_Boxes.begin(); otherBox != m_Boxes.end();otherBox++) { if(box != otherBox) { m_CollisionDetector->boxAndBox(*(*box), (*(*otherBox)), m_CollisionData); } } */ } m_CollisionDetector->boxAndBoxBatch(m_Boxes, m_CollisionData); m_CollisionDetector->sphereAndSphereBatch(m_Balls, m_CollisionData); // Check for collisions with each ball for (ball = m_Balls.begin(); ball != m_Balls.end(); ball++) { //Check for collisions with ground m_CollisionDetector->sphereAndHalfSpace((*(*ball)), (*m_Plane), m_CollisionData); /* //Check for collisions with other balls for(otherBall = m_Balls.begin(); otherBall != m_Balls.end(); otherBall++) { if(ball != otherBall) { if (m_CollisionDetector->sphereAndSphere((*(*ball)), (*(*otherBall)), m_CollisionData)) { cout<<"Sphere and Sphere Collision."<<endl; } } } */ } }
bool jfBoxesAndBallsSimulation_x86::init() { if (!m_WindowManager->createWindow(800, 600, 32, false, "Ballistic Simulation")) { return false; } m_Cam->setPos(jfVector3_x86(0, 10, 50)); m_Cam->setRot(jfVector3_x86(0, 0, 0)); m_EventHandler->setWindowManager(m_WindowManager); // m_EventHandler->setSimulation(this); m_EventHandler->setCam(m_Cam); m_3DGraphicsHandler->init(); m_3DGraphicsHandler->setBalls(&m_Balls); m_3DGraphicsHandler->setBoxes(&m_Boxes); m_3DGraphicsHandler->setCam(m_Cam); vector<jfBall*>::iterator ball; jfReal y = 1.f; for (ball = m_Balls.begin(); ball != m_Balls.end(); ball++) { jfVector3_x86 pos = jfVector3_x86(y * 0.1, y, 0); (*ball)->setState(JF_BALL_HEAVY, pos); (*ball)->setRadius(1.0); (*ball)->getBody()->setAccel(jfVector3_x86(0, -9.8, 0)); y += 3; //(*ball)->setType(JF_BALL_UNUSED); } y = 10; // Initialise the boxes vector<jfBox*>::iterator box; for (box = m_Boxes.begin(); box != m_Boxes.end(); box++) { (*box)->init(); jfVector3_x86 pos = jfVector3_x86(0, y, 3); (*box)->setState(JF_BOX_HEAVY, pos); y += 3; } m_Timer->start(); m_MousePressedLast = false; #ifdef PERF_TIMING //Remove performance timing file from before. remove(timingFilename); #endif return true; }
jfBigBallisticSimulation_x86::jfBigBallisticSimulation_x86() : m_CurrentShotType(LASER) { m_EventHandler = new jfBigBallisticEventHandler_x86(); m_WindowManager = new jfSDL(); m_3DGraphicsHandler = new jfBigBallistic3DGraphicsHandler_x86(); m_Timer = new jfTimer(); unsigned i; for(i=0;i<MAX_AMMO_ROUNDS;i++) { m_Ammo.push_back(new jfAmmoRound_x86()); } for(i=0;i<m_MaxBoxes;i++) { jfBox_x86* newBox = new jfBox_x86(); newBox->setHalfSize(jfVector3_x86(2,2,2)); m_Boxes.push_back(newBox); } m_CollisionData = new jfCollisionData(); m_ContactResolver = new jfContactResolver_x86(m_MaxContacts); m_CollisionDetector = new jfCollisionDetector_x86(); }