void LetterDisplayAnimation::Animate() { unsigned int elapsed = GetTicks() - mStartTime; float t = elapsed/(float)mDuration; if(t > 1.0f) { GameMessage msg; msg.mEventName = "FINISHED"; msg.mSender = mID; SendOutMessage(msg); return; } IvVector3 pos = Lerp(t, mStartPos, mEndPos); //Add noise to movement IvVector3 normDir = mEndPos - mStartPos; normDir.z = 0; normDir.Normalize(); float x = normDir.x; float y = normDir.y; normDir.x = -y; normDir.y = x; float noise = Perlin::Noise(pos.x*0.01f,pos.y*0.01f); //ramp noise based on proximity to endpoints float s = 1.0f; if(t <= 0.1f) s = Perlin::lerp(t*10.0f,0.0f,1.0f); else if( t >= 0.9f) s = Perlin::lerp((t-0.9f)*10.0f,1.0f,0.0f); normDir *= noise*5.0f * s; pos += normDir; IvVector3 intPos; intPos.x = (int)pos.x; intPos.y = (int)pos.y; intPos.z = mStartPos.z; SetPosition(intPos); IvVector3 vel(0,201,0); mMetaballGrid->ObjectMoved(mID, intPos,vel); }
//------------------------------------------------------------------------------- // @ IvQuat::Set() //------------------------------------------------------------------------------- // Set quaternion based on start and end vectors //------------------------------------------------------------------------------- void IvQuat::Set( const IvVector3& from, const IvVector3& to ) { // Ensure that our vectors are unit ASSERT( from.IsUnit() && to.IsUnit() ); // get axis of rotation IvVector3 axis = from.Cross( to ); // get cos of angle between vectors float costheta = from.Dot( to ); // if vectors are 180 degrees apart if ( costheta <= -1.0f ) { // find orthogonal vector IvVector3 orthoVector; orthoVector.Normalize(); w = 0.0f; x = orthoVector.x; y = orthoVector.y; z = orthoVector.z; return; } // use trig identities to get the values we want float factor = IvSqrt( 2.0f*(1.0f + costheta) ); float scaleFactor = 1.0f/factor; // set values w = 0.5f*factor; x = scaleFactor*axis.x; y = scaleFactor*axis.y; z = scaleFactor*axis.z; } // End of IvQuat::Set()
//------------------------------------------------------------------------------- // @ Player::Update() //------------------------------------------------------------------------------- // Main update loop //------------------------------------------------------------------------------- void Player::Update( float dt ) { // update based on input if (IvGame::mGame->mEventHandler->IsKeyPressed(' ')) { if (mRun) { mTime = 0.0f; mRun = false; } else { mRun = true; } } if (IvGame::mGame->mEventHandler->IsKeyPressed('m')) { mMode = (mMode + 1)%4; } if (mRun) { mTime += dt; // stop just before end so we don't end up with zero vector // when we look ahead if ( mTime > 11.5f ) mTime = 11.5f; } // now we set up the camera // get eye position IvVector3 eye = mCurve.Evaluate( mTime ); IvVector3 viewDir; IvVector3 viewSide; IvVector3 viewUp; // set parameters depending on what mode we're in // Frenet frame if ( mMode == 3 ) { IvVector3 T = mCurve.Velocity( mTime ); IvVector3 a = mCurve.Acceleration( mTime ); IvVector3 B = T.Cross( a ); B.Normalize(); T.Normalize(); IvVector3 N = B.Cross( T ); viewDir = T; viewSide = -N; // have to negate to get from RH frame to LH frame viewUp = B; } else { IvVector3 lookAt; // look same direction all the time if ( mMode == 2 ) { viewDir = IvVector3::xAxis; } // look along tangent else if ( mMode == 1 ) { viewDir = mCurve.Velocity( mTime ); } // look ahead .5 in parameter else if ( mMode == 0 ) { viewDir = mCurve.Evaluate( mTime+0.5f ) - eye; } // compute view vectors viewDir.Normalize(); viewUp = IvVector3::zAxis - IvVector3::zAxis.Dot(viewDir)*viewDir; viewUp.Normalize(); viewSide = viewDir.Cross(viewUp); } // now set up matrices // build transposed rotation matrix IvMatrix33 rotate; if ( IvRenderer::mRenderer->GetAPI() == kOpenGL ) { rotate.SetRows( viewSide, viewUp, -viewDir ); } else { rotate.SetRows( viewSide, viewUp, viewDir ); } // transform translation IvVector3 eyeInv = -(rotate*eye); // build 4x4 matrix IvMatrix44 matrix(rotate); matrix(0,3) = eyeInv.x; matrix(1,3) = eyeInv.y; matrix(2,3) = eyeInv.z; ::IvSetViewMatrix( matrix ); } // End of Player::Update()