// Function to update properties (i.e. acceleration, velocity and position) in particles void Patch::updateProperties() { int i; BigReal powTen, powFteen, realTimeDelta, invMassParticle; powTen = pow(10.0, -10); powFteen = pow(10.0, -15); realTimeDelta = timeDelta * powFteen; for(i = 0; i < particles.length(); i++) { // applying kinetic equations invMassParticle = (AVAGADROS_NUMBER / (particles[i].mass * powTen)); particles[i].ax = particles[i].fx * invMassParticle; particles[i].ay = particles[i].fy * invMassParticle; particles[i].az = particles[i].fz * invMassParticle; particles[i].vx = particles[i].vx + particles[i].ax * realTimeDelta; particles[i].vy = particles[i].vy + particles[i].ay * realTimeDelta; particles[i].vz = particles[i].vz + particles[i].az * realTimeDelta; limitVelocity(particles[i]); particles[i].x = particles[i].x + particles[i].vx * realTimeDelta; particles[i].y = particles[i].y + particles[i].vy * realTimeDelta; particles[i].z = particles[i].z + particles[i].vz * realTimeDelta; particles[i].fx = 0.0; particles[i].fy = 0.0; particles[i].fz = 0.0; } }
void Bird::update( float dt ) { const float limited = 3.0f; this->setPosition(_body->GetPosition().x * PTM_RATIO, _body->GetPosition().y * PTM_RATIO); b2Vec2 vel = _body->GetLinearVelocity(); if (_awake && vel.x < limited) _body->SetLinearVelocity(b2Vec2(limited + 1, 0)); limitVelocity(); CCLOG("Position: %f", this->getPositionX()); //CCLOG("Angle: %f | %f", vel.x, vel.y); b2Vec2 weightedVel = vel; const int NUM_PREV_VELS = 5; queue<b2Vec2> quue; for(int i = 0; i < _prevVels.size(); ++i) { weightedVel += _prevVels[i]; } weightedVel = b2Vec2(weightedVel.x/NUM_PREV_VELS, weightedVel.y/NUM_PREV_VELS); _prevVels.push_back(vel); if (_prevVels.size() >= NUM_PREV_VELS) _prevVels.clear(); vel = weightedVel; if (vel.x < 0) { vel.x = - vel.x; vel.y = - vel.y; } float angle = -1 * CC_RADIANS_TO_DEGREES(ccpToAngle(ccp(vel.x, vel.y))); angle = MIN(90, angle); angle = MAX(-90, angle); if (_awake) this->setRotation(angle); }
// Clip velocities over the limit and print ROS warnings void limitVelocity(vpColVector &qdot, double velLimit) { std::vector<double> qdotVec = pal::vispToStdVector(qdot); limitVelocity(qdotVec, velLimit); qdot = pal::stdVectorToVisp(qdotVec); }
void DummyCharacter::Update() { ApplyCommands(); limitVelocity(); //fun resize float resizefactor = fabs(body.v.y/3.0f); float wid = ONE_BLOCK_SIZE - resizefactor; float hig = TWO_BLOCK_SIZE + resizefactor; //rectsprite.setSize(sf::Vector2f(wid, hig)); //rectsprite.setOrigin(wid / 2.0f, hig); }
void Jumpman::simulateSelf(double deltaTime) { mDeltaTime = deltaTime; //Build acceleration vector. mAcceleration = glm::vec3(0.0f, 0.0f, 0.0f); //Get speed velocity components float forwardVelocity = glm::dot(mVelocity, mOrientation.getForward()); float rightVelocity = glm::dot(mVelocity, mOrientation.getRight()); float upVelocity = glm::dot(mVelocity, mOrientation.getUp()); float forwardAccel = 0.0; float rightAccel = 0.0; float upAccel = 0.0; float aimAccel = 0.0; //If the direction we want to move is the opposite of the direction we are currently moving, then acceleration is increased. int currentForwardMovement = 0; int currentRightMovement = 0; //Don't bother accelerating if we are already going too fast. bool canMoveForward = true; if(mForwardMovement == 1) { if(forwardVelocity < -sGroundMaxSpeed || !mCanMoveForward) canMoveForward = false; } else if(mForwardMovement == -1) { if(forwardVelocity > sGroundMaxSpeed || !mCanMoveBackward) canMoveForward = false; } bool canMoveRight = true; if(mRightMovement == 1) { if(rightVelocity > sGroundMaxSpeed || !mCanMoveRight) canMoveRight = false; } else if(mRightMovement == -1 ) { if(rightVelocity < -sGroundMaxSpeed || !mCanMoveLeft) canMoveRight = false; } if(forwardVelocity < 0) currentForwardMovement = 1; if(forwardVelocity > 0) currentForwardMovement = -1; if(canMoveForward) std::cout << "TRUE" << std::endl; else std::cout << "FALSE" << std::endl; if(mForwardMovement != 0 && canMoveForward) { if(mForwardMovement != 0 && currentForwardMovement != 0 && mForwardMovement != currentForwardMovement) { forwardAccel = sGroundDeaccel; } else { forwardAccel = sGroundAccel; } forwardAccel *= mForwardMovement; } else if(mForwardMovement == 0) { forwardAccel = 0.0f; } if(rightVelocity > 0) currentRightMovement = 1; if(rightVelocity < 0) currentRightMovement = -1; if(mRightMovement != 0 && canMoveRight) { if(mRightMovement != 0 && currentRightMovement != 0 && mRightMovement != currentRightMovement) rightAccel = sGroundDeaccel; else rightAccel = sGroundAccel; rightAccel *= mRightMovement; } else if(mRightMovement == 0) { rightAccel = 0; } if(mJumpTimerAll > 0.0) { mJumpTimerAll -= deltaTime; } if((mIsGrounded || mAttached) && mCanJump && mJumpTimerAll <= 0) { if(mJumpTimerGround > 0.0 && upVelocity <= 0.0f) { mJumpTimerGround -= deltaTime; if(mJumpTimerGround < 0.0) mJumpTimerGround = 0.0; } if(mJumping && mJumpTimerGround <= 0.0) { mVelocity += sJumpAccel * mOrientation.getUp() ; mJumpTimerGround = sJumpCooldownGround; mJumpTimerAll = sJumpCooldownAir; mAttached = false; mIsGrounded = false; } else if(mLeaping && mJumpTimerGround <= 0.0) { mVelocity -= sLeapAccel * mAim; mJumpTimerGround = sJumpCooldownGround; mJumpTimerAll = sJumpCooldownAir; mAttached = false; mIsGrounded = false; } } //Climbing Code glm::vec3 myWorldPos = mOrientation.getPos(); if(mClimbing) { //If not already attached to something, attempt to climb. if(mClimable && !mAttached && !mAttemptingToClimb) { glm::vec3 vectorToClimbPoint = mClimableCoord - myWorldPos; float distanceToClimbPoint = glm::length(vectorToClimbPoint); if(distanceToClimbPoint <= sLungeRange) { //Lunge towards the climbpoint if out of range. if(distanceToClimbPoint > sGrabRange) { mVelocity += glm::normalize(vectorToClimbPoint) * sLungeVelocity; } std::cout << "Attempting to Climb" << std::endl; mAttemptingToClimb = true; mTimeAttemptingToClimb = 0.0; mClimbTarget = mClimableCoord; } } //Detach if attached. else if(mAttached) { mAttached = false; } } if(mAttemptingToClimb && !mClimbing) { glm::vec3 vectorToClimbPoint = mClimbTarget - myWorldPos; float speedTowardsPoint = glm::dot(vectorToClimbPoint, mVelocity); float distanceToClimbPoint = glm::length(vectorToClimbPoint); std::cout << distanceToClimbPoint << std::endl; if(distanceToClimbPoint <= sGrabRange) { //if(!(speedTowardsPoint < 0)) { mVelocity = glm::vec3(0.0f); //Kill all movement mAcceleration = glm::vec3(0.0f); if(distanceToClimbPoint < sGrabRange) //Make sure don't get sucked into wall { float rollbackDistance = sGrabRange - distanceToClimbPoint; glm::vec3 rollbackVector = rollbackDistance * (-glm::normalize(vectorToClimbPoint)); mOrientation.translate(rollbackVector); } mAttached = true; mAttemptingToClimb = false; //mOrientation.translate(mBounceVelocity.x*deltaTime, mBounceVelocity.y*deltaTime, mBounceVelocity.z*deltaTime); mBounceVelocity = glm::vec3(0.0f); } } else { mTimeAttemptingToClimb += deltaTime; } } if(mTimeAttemptingToClimb >= sMaxTimeAttemptingToClimb) { mAttemptingToClimb = false; std::cout << "Give up" << std::endl; } mAcceleration = (mOrientation.getForward() * -forwardAccel) + (mOrientation.getRight() * rightAccel); //std::cout << "ACCEL " << mAcceleration.x << ", " << mAcceleration.y << ", " << mAcceleration.z << std::endl; if(!mAttached) { applyAcceleration(deltaTime); applyGravity(deltaTime); limitVelocity(deltaTime); } mOrientation.translate(mVelocity.x*deltaTime, mVelocity.y*deltaTime, mVelocity.z*deltaTime); //Re-orient camera and aim vector. mAzimuth += mDeltaAzi;// * deltaTime; mAltitude += mDeltaAlt;// * deltaTime; if(mAltitude > sMaxAltitude) mAltitude = sMaxAltitude; if(mAltitude < sMinAltitude) mAltitude = sMinAltitude; //Only change forward and right vectors. mOrientation.resetRotation(); mOrientation.yaw(mAzimuth); //Never roll. mFPCamera->orientation()->resetRotation(); mFPCamera->orientation()->pitch(mAltitude); //Aim vector is the same as camera forward. mAim = glm::rotate(mOrientation.getForward(), float(mAltitude), mOrientation.getRight()); glm::vec3 forward = mFPCamera->orientation()->getForward(); forward.x = -(forward.x); forward.z = -(forward.z); mCrosshairRay->setDirection(mFPCamera->orientation()->getForward() * -sLungeRange); //mCrosshairRay->setDirection(forward * sGrabRange); if(myWorldPos.y < sFallLimit) { mOrientation.setPos(mSpawnPos); mVelocity = glm::vec3(0.0f); mAcceleration = glm::vec3(0.0f); } mTransform = mOrientation.getOrientationMatrix(); mIsGrounded = false; mCanMoveForward = true; mCanMoveBackward = true; mCanMoveLeft = true; mCanMoveRight = true; mCanJump = true; }