/** * Perform the actual movement */ void Organism::move() { // Do not move more than once in an update if (hasMoved || !active) return; double angle, translation = 0; // Calculate the combined angle and speed of the robots combineVectors(angle, translation); // Move all the agents using the combined angle // and speed. moveOrganism(angle, translation); // Did we collide with anything? if (detectCollisions()) { // Then roll the movement back rollbackMove(); } else { updateWorldModels(); } // Anyway, we are done moving this movement this->hasMoved = true; // So update the sensors updateSensors(); }
/* manipulated here */ void updateGame(HWND hwnd){ /********************** * * * Move The Ball * * * **********************/ /** MS's Offset Rectangle function make moving the rectangle easy **/ OffsetRect(&ballRect, ballInfo.vx, ballInfo.vy); /** copy new ball rectangle **/ ballRectInvaild = ballRect; /************************************************************************* * Adjust the RECT used to invalidate the part of the screen we need * * to redraw for the ball. The region needed to be redrawn is where * * the ball is and where it is going to be on the next screen redraw * *************************************************************************/ if(ballInfo.vx > 0){ ballRectInvaild.left = ballRect.left - ballInfo.vx; ballRectInvaild.right = ballRect.right + ballInfo.vx; } else{ ballRectInvaild.left = ballRect.left + ballInfo.vx; ballRectInvaild.right = ballRect.right - ballInfo.vx; } if(ballInfo.vy > 0){ ballRectInvaild.top = ballRect.top - ballInfo.vy; ballRectInvaild.bottom = ballRect.bottom + ballInfo.vy; } else{ ballRectInvaild.top = ballRect.top + ballInfo.vy; ballRectInvaild.bottom = ballRect.bottom - ballInfo.vy; } /****************************** * * * Detect Ball Collisions * * * ******************************/ detectCollisions(hwnd); }
void PlayScene::update(Engine& ctx) { if(m_status == Playing) { if(m_player -> reachedExit()) { endGame(ctx); m_status = Playing; ctx.setScene("end"); return; } m_player -> update(ctx); if(m_player -> x() - m_threat -> x() > 512) { m_threat -> setX(m_player -> x() - 512); } m_threat -> update(ctx); m_threat->setY(m_player->y()); // Just to make sure it can always hit the player updateCamera(ctx); m_player->setMinX(m_cam -> x()); // Player can't walk past the left edge of the screen // The youngling eater follows the camera. If any younglings go off-screen, they'll get omnomnommed. m_eater->setX(m_cam -> x()); m_eater->setY(m_cam -> y()); // Update objects on platforms updatePlatforms(ctx); // Do collision detection detectCollisions(ctx); // Game over if the player has lost all the lifes if(m_player -> lives() <= 0) die("THE MEANIES GOT YOU!", true); } if(m_status == GameEnding && !m_gameOverSound -> isPlaying()) { endGame(ctx); } }
void GameManager::update(int frameTime) { static bool levelComplete = false; if(pause) { return; } for(auto &entity : uiEntities) { entity->update(frameTime); } //std::cout << "Updating" << std::endl; for(std::vector<GameEntity*>::iterator it=physicsEntities.begin(); it!=physicsEntities.end();) { //Update the item (*it)->update(frameTime); //Delete it if it's deletable if((*it)->isDeletable()) { delete * it; //std::cout << "Deleted" << std::endl; it = physicsEntities.erase(it); //std::cout << "Erased" << std::endl; } else { ++it; } } if(blockCount <= 0) { gameWon(); } detectCollisions(); }
static void gameScreen_Process( void ) { getLaunchToPosition( ); processTroops( ); // check all collisions detectAllCollisions( panjandrumColliders, troopColliders, drumTroopCollision ); detectAllCollisions( panjandrumColliders, explosionColliders, drumExplosionCollision ); detectAllCollisions( explosionColliders, troopColliders, explosionTroopCollision ); detectAllCollisions( panjandrumColliders, barbedWireColliders, drumWireCollision ); detectAllCollisions( panjandrumColliders, mineColliders, drumMineCollision ); detectAllCollisions( explosionColliders, mineColliders, explosionMineCollision ); detectAllCollisions( barbedWireColliders, troopColliders, wireTroopCollision ); detectAllCollisions( mineColliders, troopColliders, mineTroopCollision ); detectAllCollisions( pitColliders, troopColliders, pitTroopCollision ); detectAllCollisions( sideColliders, troopColliders, sideTroopCollision ); if( introMode ) { detectCollisions( &startCollider, explosionColliders.firstCollider, explosionColliders.stride, explosionColliders.count, startExplosionCollision, 0 ); } }
void GameState::update(int milliseconds) { if (m_balls <= 0) { return; } // update ball position m_ball.position += milliseconds * m_ball.velocity; // have we lost? if (m_ball.position.y >= 1) { resetBall(); --m_balls; if (m_balls == 0) { g_sound_engine.playSound(LOSE_GAME); } else { g_sound_engine.playSound(DIE); } return; } detectCollisions(); }
void PhysicsSimulator::cycle(float delta) { integrate(delta); std::vector<PhysicsCollisionTuple> collisions = detectCollisions(delta); respondToCollisions(collisions, delta); }
void PhysicsWorld::move(int paddleTranslate[], int paddleRotate[], btScalar frameTime) { dynamicsWorld->stepSimulation(frameTime); detectCollisions(); //std::cout << collisionShapes.size() << std::endl; for(int i = 0; i < collisionShapes.size(); ++i) { btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[i]; btRigidBody* body = btRigidBody::upcast(obj); if(body && body->getMotionState()) { btTransform transform; body->getMotionState()->getWorldTransform(transform); void* userPointer = body->getUserPointer(); if(userPointer) { Ogre::SceneNode* sn = static_cast<Ogre::SceneNode*>(userPointer); btVector3 origin = transform.getOrigin(); btQuaternion orientation = transform.getRotation(); bool paddle1 = (sn->getName() == "paddle"); bool paddle2 = (sn->getName() == "paddle2"); if( ( multiplayer && ((server && paddle1) || (!server && paddle2)) ) || (!multiplayer && (paddle1 || paddle2))) { int paddleMult = 0; if(sn->getName() == "paddle2") paddleMult = 1; float paddleMoveSpeed = 0.06f; btScalar rotationSpeed = .005f; btTransform newTrans = transform; //TRANSLATION // up if(paddleTranslate[0]) { if(newTrans.getOrigin().getY() < (100.0f - 30.0f/2.0f + paddleMult*200.0f)) { newTrans.getOrigin() += (btVector3(0, paddleMoveSpeed * (1000.0f * frameTime), 0)); } } // left if(paddleTranslate[1]) { if(newTrans.getOrigin().getX() < (100.0f - 40.0f/2.0f)) { newTrans.getOrigin() += (btVector3(paddleMoveSpeed * (1000.0f * frameTime), 0, 0)); } } // down if(paddleTranslate[2]) { if(newTrans.getOrigin().getY() > (-100.0f + 30.0f/2.0f + paddleMult*200.0f)) { newTrans.getOrigin() += (btVector3(0, -paddleMoveSpeed * (1000.0f * frameTime), 0)); } } //right if(paddleTranslate[3]) { if(newTrans.getOrigin().getX() > (-100.0f + 40.0f/2.0f)) { newTrans.getOrigin() += (btVector3(-paddleMoveSpeed * (1000.0f * frameTime), 0, 0)); } } btScalar roll, pitch, yaw; btQuaternion rotation = newTrans.getRotation(); btMatrix3x3(rotation).getEulerYPR(yaw,pitch,roll); // ROTATION // up if(paddleRotate[0] && roll < 1) { roll += rotationSpeed * (1000.0f * frameTime); } // left if(paddleRotate[1] && pitch > -1) { pitch -= rotationSpeed * (1000.0f * frameTime); } // down if(paddleRotate[2] && roll > -1) { roll -= rotationSpeed * (1000.0f * frameTime); } //right if(paddleRotate[3] && pitch < 1){ pitch += rotationSpeed * (1000.0f * frameTime); } //reset if(paddleRotate[4]) { pitch = 0; roll = 0; yaw = 0; } btQuaternion tmp; tmp.setEulerZYX(yaw,pitch,roll); newTrans.setRotation(tmp); body->getMotionState()->setWorldTransform(newTrans); body->translate(newTrans.getOrigin() - body->getCenterOfMassPosition()); body->setCenterOfMassTransform(newTrans); if(server) { paddle1Pitch = pitch; paddle1Yaw = yaw; paddle1Roll = roll; } else { paddle2Pitch = pitch; paddle2Yaw = yaw; paddle2Roll = roll; } //std::cout << "Moving paddle transform to (" << tempOrigin.getX() << ", " << tempOrigin.getY() << ", " << tempOrigin.getZ() << ")\n"; //std::cout << "Paddle rigid body is now at (" << body->getCenterOfMassPosition().getX() << ", " << body->getCenterOfMassPosition().getY() << ", " << body->getCenterOfMassPosition().getZ() << ")\n"; } Ogre::Vector3 pos(origin.getX(), origin.getY(), origin.getZ()); Ogre::Quaternion quat(orientation.getW(), orientation.getX(), orientation.getY(), orientation.getZ()); if(multiplayer) { if(server) { if(paddle1) { //set the values for paddle1 paddle1Position = Ogre::Vector3(origin.getX(), origin.getY(), origin.getZ()); } else if (paddle2) { //get the values for paddle2 btTransform newTrans = transform; newTrans.setOrigin(btVector3(paddle2Position.x, paddle2Position.y, paddle2Position.z)); btQuaternion tmp; tmp.setEulerZYX(paddle2Yaw, paddle2Pitch, paddle2Roll); newTrans.setRotation(tmp); //newTrans.setRotation(btQuaternion(paddle2Quaternion.w, paddle2Quaternion.x, paddle2Quaternion.y, paddle2Quaternion.z)); body->getMotionState()->setWorldTransform(newTrans); body->translate(newTrans.getOrigin() - body->getCenterOfMassPosition()); body->setCenterOfMassTransform(newTrans); pos = paddle2Position; // quat.x = (float)tmp.x; // quat.y = (float)tmp.y; // quat.z = (float)tmp.z; // quat.w = (float)tmp.w; } } else { if(paddle2) { //set the values for paddle2 paddle2Position = Ogre::Vector3(origin.getX(), origin.getY(), origin.getZ()); pos = paddle2Position; } else if (paddle1) { //get the values for paddle1 btTransform newTrans = transform; newTrans.setOrigin(btVector3(paddle1Position.x, paddle1Position.y, paddle1Position.z)); btQuaternion tmp; tmp.setEulerZYX(paddle1Yaw, paddle1Pitch, paddle1Roll); newTrans.setRotation(tmp); //newTrans.setRotation(btQuaternion(paddle1Quaternion.w, paddle1Quaternion.x, paddle1Quaternion.y, paddle1Quaternion.z)); body->getMotionState()->setWorldTransform(newTrans); body->translate(newTrans.getOrigin() - body->getCenterOfMassPosition()); body->setCenterOfMassTransform(newTrans); pos = paddle1Position; // quat.x = (float)tmp.x; // quat.y = (float)tmp.y; // quat.z = (float)tmp.z; // quat.w = (float)tmp.w; } } } sn->setPosition(pos); sn->setOrientation(quat); // sn->setPosition(paddle1Position); // paddle1Quaternion = Ogre::Quaternion( // orientation.getW(), orientation.getX(), // orientation.getY(), orientation.getZ()); // sn->setOrientation(paddle1Quaternion); } } } }
// Run by GLUT every [tickspeed] miliseconds void tick(int in) { // Local handles to objects Level *currentLevel = levelController->getCurrentLevel(); Ball *ball = currentLevel->getBall(); Physics *physics = ball->getPhysics(); Tile* currentTile = currentLevel->getTile(ball->getCurrentTileID()); vector<int> borderIDs = currentTile->getNeighborIDs(); vector<Shape*> borderShapes = currentTile->getBorders()->getInwardShapes(); vec3 ballPosition = physics->getPosition(); // Debug -- Highlight current tile if (DEBUG_TILE_PAINT) { currentTile->getShapes()[0]->changeColor(TILE_HIGHLIGHT_COLOR); currentTile->getShapes()[0]->reload(); } // Collision with cup glm::vec3 ballPos = physics->getPosition(); glm::vec3 cupPos = currentLevel->getCup()->getPhysics()->getPosition(); float cupPlaneDist = sqrt(((ballPos.x - cupPos.x)*(ballPos.x - cupPos.x)) + ((ballPos.z - cupPos.z)*(ballPos.z - cupPos.z))); if(cupPlaneDist < (CUP_RADIUS - (0.8 * BALL_RADIUS)) && abs(cupPos.y - ballPos.y) <= 1.1*BALL_OFFSET){ // allow for slight error //Play SFX for falling in hole sound->getEngine()->play2D("sfx/retro_cup.wav"); //----------------CHANGE TO NEXT HOLE----------------// nextHole(); } // Physics and collision calculations vec3 newDirection = physics->getDirection(); // used for tile transitions if(ballMoving) { // Check for collision if(detectCollisions(currentTile, physics, sound)){ newDirection = physics->getDirection(); } // Update ball direction newDirection = updateBallDirection(levelController, sound); // Update current tile currentTile = currentLevel->getTile(ball->getCurrentTileID()); // Update ball speed double ballSpeed = physics->getSpeed(); if(currentTile->getShapes().at(0)->normals()[0] == glm::vec3(0.0,1.0,0.0)){ // flat tile if(ballSpeed > 0.01){ physics->setSpeed(ballSpeed - TILE_DEFAULT_FRICTION); ballSpeed = physics->getSpeed(); } else{ physics->setSpeed(0.0); ballStopped(); } } else if(newDirection.y > 0){ // going up hill //cout << endl << "UP"; //cout << "Direction y-value: " << newDirection.y << endl; if(ballSpeed > 0.01){ physics->setSpeed(ballSpeed - (TILE_DEFAULT_FRICTION + (2.0*currentTile->getSlope()*TILE_DEFAULT_FRICTION))); ballSpeed = physics->getSpeed(); } } else{ // going down hill //cout << endl << "DOWN"; //cout << "Direction y-value: " << newDirection.y << endl; if(ballSpeed <= (100.0/100.1)){ physics->setSpeed(ballSpeed + TILE_DEFAULT_FRICTION); } else{ // clmap speed ballSpeed = (100.0/100.1); } } // Update ballPosition physics->updatePosition(); // Update shape using velocity Shape* ballShape = ball->getShapes().at(0); ballShape->translate(physics->getVelocity()); // Update ball direction physics->setDirection(newDirection); // Snap ball to correct y value -- hacky if(currentTile->getShapes().at(0)->normals()[0] == glm::vec3(0.0,1.0,0.0)){ // flat tile if(physics->getPosition().y != (currentTile->getPhysics()->getPosition().y + BALL_OFFSET)){ // Snap ball shape ball->getShapes()[0]->translate(vec3(0.0, -(physics->getPosition().y), 0.0)); ball->getShapes()[0]->translate(vec3(0.0, (currentTile->getPhysics()->getPosition().y + BALL_OFFSET), 0.0)); // Update ball physics physics->setPosition(vec3(physics->getPosition().x, (currentTile->getPhysics()->getPosition().y + BALL_OFFSET), physics->getPosition().z)); } } else{// sloped tile float correctY = currentTile->getShapes()[0]->yValueAtPoint(physics->getPosition().x, physics->getPosition().z); if(physics->getPosition().y != correctY){ // Snap ball shape ball->getShapes()[0]->translate(vec3(0.0, -(physics->getPosition().y), 0.0)); ball->getShapes()[0]->translate(vec3(0.0, correctY, 0.0)); // Update ball physics physics->setPositionY(correctY); } } // Reload shape ballShape->reload(); } // Update HUD currentLevel = levelController->getCurrentLevel(); updateHUD(currentLevel->getLevelName(), fileIO->getNumHoles(), currentHoleScore, currentLevel->getPar()); //If controls are enabled (ball not yet launched), then make ball direction equal to launchVector if (angleSpinner->enabled) { float launchAngleRadians = launchAngle * (PI/180); launchVector = normalize(vec3(sin(launchAngleRadians), 0.0, cos(launchAngleRadians))); updateCamera(physics->getPosition(), launchVector, false); } else { updateCamera(physics->getPosition(), physics->getDirection(), true); } glutTimerFunc(tickSpeed, tick, 0); }
inline void Player::draw(Coordinate unused) { glColor3f(1,1,1); string s; s.assign("Score: "); s.append(int2String(score)); renderString(412, 354, s); int currentFps = Fps::getFps(); if (currentFps < 1) { currentFps = 1; } if (l) { if (dTheta <= 1.1) { dTheta += 0.02; } } else if (r) { if (dTheta >= -1.1) { dTheta -= 0.02; } } else { dTheta /= 1.1; if (dTheta < 0.3 && dTheta > -0.3) { dTheta = 0; } } direction += dTheta; if (direction >= 360) { direction -= 360; } if (direction < 0) { direction += 360; } if (f) { dX = acceleration*cos(D2R(direction)); dY = acceleration*sin(D2R(direction)); velocity.vx += dX; velocity.vy += dY; } else if (b) { dX = -(acceleration*cos(D2R(direction))); dY = -(acceleration*sin(D2R(direction))); velocity.vx += dX; velocity.vy += dY; } else { velocity.vx *= ((1-deceleration)); velocity.vy *= ((1-deceleration)); } velocity.magnitude = sqrt(velocity.vx*velocity.vx + velocity.vy*velocity.vy); if(velocity.magnitude > 2) { float tempDecel = (2/velocity.magnitude); velocity.vx *= tempDecel; velocity.vy *= tempDecel; } if(this->cell != -1) { Collision collision = detectCollisions(); if(collision.happened) { velocity.vx = collision.vx; velocity.vy = collision.vy; } } if(sucked) { Coordinate dw; dw.x = wpLoc.x - location.x; dw.y = wpLoc.y - location.y; if(abs(dw.x) < 10 && abs(dw.y) < 10) { velocity.vx = 0; velocity.vy = 0; location.x = wpLoc.x; location.y = wpLoc.y; finishLevel(); } else { velocity.vx = dw.x * 0.2; velocity.vy = dw.y * 0.2; } } location.x += velocity.vx; location.y += velocity.vy; if(attacking) { int atkspeed = 6; atkspin += atkspeed; if(atkspeed < 0) { attacking = false; } if (atkspin >= 180) { atkspeed -= 0.0001; } if (atkspin > 360) { atkspin -= 360; atkspeed -= 0.0001; } bbox.hheight = 42; } else { bbox.hheight = 25; } if(hit) { if(glutGet(GLUT_ELAPSED_TIME) - safetimer > 3000) { hit = false; safe = false; } if(!flash) { flash = true; } else { drawPlayer(); flash = false; } } else { drawPlayer(); } };