void CCSceneMoveable::updateMovement(const float delta) { if( moveable ) { const float movementMagnitude = applyMovementDirection( delta ); velocity = movementVelocity; const float additionalMagnitude = CCVector3LengthSquared( additionalVelocity ); if( additionalMagnitude > 0.0f ) { velocity.add( additionalVelocity ); const float deceleration = decelerationSpeed * delta; CCToTarget( additionalVelocity.x, 0.0f, deceleration ); CCToTarget( additionalVelocity.y, 0.0f, deceleration ); CCToTarget( additionalVelocity.z, 0.0f, deceleration ); } const float velocityMagnitude = CCVector3LengthSquared( velocity ); if( velocityMagnitude > 0.0f ) { applyVelocity( delta, movementMagnitude ); dirtyModelMatrix(); updateCollisions = true; CCOctreeRefreshObject( this ); } } }
bool CCVector3::toTarget(const CCVector3 &target, const float speedX, const float speedY, const float speedZ) { bool updating = CCToTarget( x, target.x, speedX ); updating |= CCToTarget( y, target.y, speedY ); updating |= CCToTarget( z, target.z, speedZ ); return updating; }
bool CCVector3::toTarget(const float target, const float speed) { bool updating = CCToTarget( x, target, speed ); updating |= CCToTarget( y, target, speed ); updating |= CCToTarget( z, target, speed ); return updating; }
bool CCColour::toTarget(const CCColour &target, const float amount) { bool interpolating = CCToTarget( red, target.red, amount ); interpolating |= CCToTarget( green, target.green, amount ); interpolating |= CCToTarget( blue, target.blue, amount ); interpolating |= CCToTarget( alpha, target.alpha, amount ); return interpolating; }
bool CCInterpolatorSin2Curve::incrementAmount(const float delta) { if( CCToTarget( amount, 1.0f, delta * speed ) ) { return true; } return false; }
void CCSceneMoveable::applyVelocity(const float delta, const float movementMagnitude) { const float velocityX = velocity.x * delta; const float velocityZ = velocity.z * delta; if( velocityX != 0.0f || velocityZ != 0.0f ) { const CCSceneCollideable *collidedWith = NULL; const float velocityVsBoundingX = velocityX * inverseBoundsLength.x; const float velocityVsBoundingZ = velocityZ * inverseBoundsLength.x; const float absVelocityVsBoundingX = fabsf( velocityVsBoundingX ); const float absVelocityVsBoundingZ = fabsf( velocityVsBoundingZ ); if( absVelocityVsBoundingX > 1.0f || absVelocityVsBoundingZ > 1.0f ) { const float furthestIncrement = absVelocityVsBoundingX > absVelocityVsBoundingZ ? absVelocityVsBoundingX : absVelocityVsBoundingZ; const uint numberOfIncrements = roundf( furthestIncrement + 0.5f ); const float inverseNumberOfIncrements = 1.0f / numberOfIncrements; const float incrementsX = velocityX * inverseNumberOfIncrements; const float incrementsZ = velocityZ * inverseNumberOfIncrements; uint i = 0; do { collidedWith = applyHorizontalVelocity( incrementsX, incrementsZ ); i++; } while( i < numberOfIncrements && collidedWith == NULL ); } else { collidedWith = applyHorizontalVelocity( velocityX, velocityZ ); } // Deceleration if( movementMagnitude == 0.0f ) { const float movementDeceleration = decelerationSpeed * delta; CCToTarget( movementVelocity.x, 0.0f, movementDeceleration ); CCToTarget( movementVelocity.z, 0.0f, movementDeceleration ); } } // Gravity if( gravity ) { const CCSceneCollideable *collidedWith = NULL; movementVelocity.y -= gravityForce * delta; const float velocityY = velocity.y * delta; if( velocityY > 0.0f ) { const float velocityVsBoundingY = velocityY * inverseBoundsLength.y; const float absVelocityVsBoundingY = fabsf( velocityVsBoundingY ); if( absVelocityVsBoundingY > 1.0f ) { uint numberOfIncrements = roundf( absVelocityVsBoundingY + 0.5f ); const float velocityIncrements = velocityY / numberOfIncrements; uint i = 0; do { collidedWith = applyVerticalVelocity( velocityIncrements ); i++; } while( i < numberOfIncrements && collidedWith == NULL ); } else { collidedWith = applyVerticalVelocity( velocityY ); } } else { const float velocityVsBoundingY = velocityY * inverseBoundsLength.y; const float absVelocityVsBoundingY = fabsf( velocityVsBoundingY ); if( absVelocityVsBoundingY > 1.0f ) { uint numberOfIncrements = roundf( absVelocityVsBoundingY + 0.5f ); const float velocityIncrements = velocityY / numberOfIncrements; uint i = 0; do { collidedWith = applyVerticalVelocity( velocityIncrements ); i++; } while( i < numberOfIncrements && collidedWith == NULL ); } else { collidedWith = applyVerticalVelocity( velocityY ); } } reportVerticalCollision( collidedWith ); // Ensure we have a velocity so we're checked for movement next frame if( collidedWith == NULL && movementVelocity.y == 0.0f ) { movementVelocity.y = CC_SMALLFLOAT; } } }
const bool CCWidgetBase::update(const CCTime &time) { if( enabled ) { const bool positionInterpolation = position.x != positionTarget.x || position.y != positionTarget.y; const bool sizeInterpolation = size.width != sizeTarget.width || size.height != sizeTarget.height; const bool colourInterpolation = colour != NULL && colourTarget != NULL && colour->equals( *colourTarget ) == false; if( positionInterpolation || sizeInterpolation || colourInterpolation ) { if( positionInterpolation || sizeInterpolation ) { if( positionInterpolation ) { const float deltaSpeed = time.delta * movementSpeed; CCToTarget( position.x, positionTarget.x, deltaSpeed ); CCToTarget( position.y, positionTarget.y, deltaSpeed ); } if( sizeInterpolation ) { const float deltaSpeed = time.delta * resizeSpeed; CCToTarget( size.width, sizeTarget.width, deltaSpeed ); CCToTarget( size.height, sizeTarget.height, deltaSpeed ); } refreshDimensions(); } if( colourInterpolation ) { colour->toTarget( *colourTarget, time.delta * colourSpeed ); } if( interpolating() == false ) { if( onInterpolated.routine != NULL ) { onInterpolated.routine( onInterpolated.parameter1, onInterpolated.parameter2 ); } } } if( inView && distortEffect ) { const float deltaSpeed = time.delta * distortSpeed; if( CCToTarget( distort.x, distortTarget.x, deltaSpeed ) == false ) { int random = rand(); distortTarget.x = ( random % 10 ) * distortAmount * size.width; } if( CCToTarget( distort.y, distortTarget.y, deltaSpeed ) == false ) { int random = rand(); distortTarget.y = ( random % 10 ) * distortAmount * size.width; } } return true; } return false; }