1. Easing Code
Many games and App use Easing Algorithm like scrolling, character moving, etc. Blow show c++ code of Easing Algorithm.
start_value : 0 frame's value.
change_value : End frame's value - start_value
inline float easeLinear(float current_frame, float start_value, float end_frame, float change_value) { return change_value*current_frame/end_frame + start_value; } inline float easeQuadraticIn(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame; return change_value*current_frame*current_frame + start_value; } inline float easeQuadraticOut(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame; return -change_value * current_frame*(current_frame-2) + start_value; } inline float easeQuadraticInOut(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame/2; if (current_frame < 1) return change_value/2*current_frame*current_frame + start_value; current_frame--; return -change_value/2 * (current_frame*(current_frame-2) - 1) + start_value; } inline float easeCubicIn(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame; return change_value*current_frame*current_frame*current_frame + start_value; } inline float easeCubicOut(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame; current_frame--; return change_value*(current_frame*current_frame*current_frame + 1) + start_value; } inline float easeCubicInOut(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame/2; if (current_frame < 1) return change_value/2*current_frame*current_frame*current_frame + start_value; current_frame -= 2; return change_value/2*(current_frame*current_frame*current_frame + 2) + start_value; } inline float easeQuarticIn(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame; return change_value*current_frame*current_frame*current_frame*current_frame + start_value; } inline float easeQuarticOut(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame; current_frame--; return -change_value * (current_frame*current_frame*current_frame*current_frame - 1) + start_value; } inline float easeQuarticInOut(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame/2; if (current_frame < 1) return change_value/2*current_frame*current_frame*current_frame*current_frame + start_value; current_frame -= 2; return -change_value/2 * (current_frame*current_frame*current_frame*current_frame - 2) + start_value; } inline float easeQuinticIn(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame; return change_value*current_frame*current_frame*current_frame*current_frame*current_frame + start_value; } inline float easeQuinticOut(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame; current_frame--; return change_value*(current_frame*current_frame*current_frame*current_frame*current_frame + 1) + start_value; } inline float easeQuinticInOut(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame/2; if (current_frame < 1) return change_value/2*current_frame*current_frame*current_frame*current_frame*current_frame + start_value; current_frame -= 2; return change_value/2 * (current_frame*current_frame*current_frame*current_frame*current_frame + 2) + start_value; } inline float easeSinIn(float current_frame, float start_value, float end_frame, float change_value) { return -change_value * cosf(current_frame/end_frame * (Pi/2)) + change_value + start_value; } inline float easeSinOut(float current_frame, float start_value, float end_frame, float change_value) { return change_value * sinf(current_frame/end_frame * (Pi/2)) + start_value; } inline float easeSinInOut(float current_frame, float start_value, float end_frame, float change_value) { return -change_value/2 * (cosf(Pi*current_frame/end_frame) - 1) + start_value; } inline float easeExponentialIn(float current_frame, float start_value, float end_frame, float change_value) { return change_value * powf( 2, 10 * (current_frame/end_frame - 1) ) + start_value; } inline float easeExponentialOut(float current_frame, float start_value, float end_frame, float change_value) { return change_value * ( -powf( 2, -10 * current_frame/end_frame ) + 1 ) + start_value; } inline float easeExponentialInOut(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame/2; if (current_frame < 1) return change_value/2 * powf( 2, 10 * (current_frame - 1) ) + start_value; current_frame --; return change_value/2 * ( -powf( 2, -10 * current_frame) + 2 ) + start_value; } inline float easeCircularIn(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame; return -change_value * (sqrtf(1 - current_frame*current_frame) - 1) + start_value; } inline float easeCircularOut(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame; current_frame--; return change_value * sqrtf(1 - current_frame*current_frame) + start_value; } inline float easeCircularInOut(float current_frame, float start_value, float end_frame, float change_value) { current_frame /= end_frame/2; if (current_frame < 1) return -change_value/2 * (sqrtf(1 - current_frame*current_frame) - 1) + start_value; current_frame -= 2; return change_value/2 * (sqrtf(1 - current_frame*current_frame) + 1) + start_value; }
2. Example App
Test each codes.
'게임을 만들자 > C++' 카테고리의 다른 글
c++11 min, max 범위, 난수 생성 (1) | 2020.04.07 |
---|---|
c++ string tokenizer (0) | 2015.01.09 |
cJSON parsing error using window utf-8 txt file, Remove UTF-8 BOM (0) | 2015.01.08 |
c++ Builder 패턴 (0) | 2014.04.16 |
c++ const (0) | 2014.04.15 |