void Box2DVehicleBody::Tire::updateDrive(float desiredSpeed) { //find current speed in forward direction b2Vec2 currentForwardNormal = m_body->GetWorldVector( b2Vec2(0,1) ); float currentSpeed = b2Dot( getForwardVelocity(), currentForwardNormal ); //apply necessary force float force = 0; if ( desiredSpeed > currentSpeed ) force = m_maxDriveForce; else if ( desiredSpeed < currentSpeed ) force = -m_maxDriveForce; else return; m_body->ApplyForce( m_currentTraction * force * currentForwardNormal, m_body->GetWorldCenter() , true); }
void Box2DVehicleBody::Tire::updateFriction() { //lateral linear velocity b2Vec2 impulse = m_body->GetMass() * -getLateralVelocity(); if ( impulse.Length() > m_maxLateralImpulse ) impulse *= m_maxLateralImpulse / impulse.Length(); m_body->ApplyLinearImpulse( m_currentTraction * impulse, m_body->GetWorldCenter() , true); //angular velocity m_body->ApplyAngularImpulse( m_currentTraction * 0.1f * m_body->GetInertia() * -m_body->GetAngularVelocity() , true); //forward linear velocity b2Vec2 currentForwardNormal = getForwardVelocity(); float currentForwardSpeed = currentForwardNormal.Normalize(); float dragForceMagnitude = -2 * currentForwardSpeed; m_body->ApplyForce( m_currentTraction * dragForceMagnitude * currentForwardNormal, m_body->GetWorldCenter() , true); }
void CarModel::updateFriction() { //calculate the counter lateral impulse based on drift parameters b2Vec2 impulse = m_body->GetMass() * -getLateralVelocity(); if (impulse.Length() > m_maxLateralImpulse) impulse *= m_maxLateralImpulse / impulse.Length(); // apply the impulse m_body->ApplyLinearImpulse(m_driftFriction * impulse, m_body->GetWorldCenter(), true); //angular velocity m_body->ApplyAngularImpulse(m_angularFriction * m_body->GetInertia() * -m_body->GetAngularVelocity(), true); //forward linear velocity b2Vec2 currentForwardNormal = getForwardVelocity(); float currentForwardSpeed = currentForwardNormal.Normalize(); float dragForceMagnitude = -2 * currentForwardSpeed * m_dragModifier; m_body->ApplyForce(m_currentTraction * dragForceMagnitude * currentForwardNormal, m_body->GetWorldCenter(), true); }
void GameState::update() { updateControl(window); updateCamera(camera); double curTime = glfwGetTime(); double elapsedTime = curTime - prevTime; prevTime = curTime; for (int i = 0; i < actors.size(); i++) { actors[i].step(elapsedTime); } if (curTime > timeToNextSphere) { timeToNextSphere = curTime + 0.5; spawnSphere(); } checkCollisions(); camera->center += camera->direction * (float) elapsedTime * getForwardVelocity(); glm::vec3 right = glm::cross(camera->direction, glm::vec3(0.0, 1.0, 0.0)); camera->center += right * (float) elapsedTime * getStrafeVelocity(); if(camera->center.y <= 0){ camera->center.y = 0; } if(camera->center.x >= XMAX){ camera->center.x = XMAX; } if(camera->center.x <= -XMAX){ camera->center.x = -XMAX; } if(camera->center.z >= ZMAX){ camera->center.z = ZMAX; } if(camera->center.z <= -ZMAX){ camera->center.z = -ZMAX; } printf("Current num of spheres is %d and Current num hit is %d\n", numCurSpheres, numSpheresHit); }
void CarModel::updateDrive(float* controlState) { //wake the body, it could be in a sleep state m_body->SetAwake(true); //find desired speed float desiredSpeed = 0; if (controlState[OA_UP] > 0.0f) desiredSpeed = m_maxForwardSpeed; if (controlState[OA_DOWN] > 0.0f) desiredSpeed = m_maxBackwardSpeed; //find current speed in forward direction b2Vec2 currentForwardNormal = m_body->GetWorldVector(b2Vec2(0, 1)); float currentSpeed = b2Dot(getForwardVelocity(), currentForwardNormal); //apply necessary force float force = (desiredSpeed > currentSpeed) ? m_maxDriveForce : -m_maxDriveForce; if (desiredSpeed != currentSpeed) { m_body->ApplyForce(m_currentTraction * force * currentForwardNormal, m_body->GetWorldCenter(), true); } }