c++ - How to scale 2D object periodically? -
i attempting have variable scale factor, each frame scaling changes + or - .05, gives growing/shrinking animation aspect.
my scaling factors (scale_x, scale_y, , scale_z) initialized 1.0. want happen each frame rendered, each scale factor grow or shrink fixed value (like .05). ideally, @ first should keep subtracting .05 every frame until reaches fixed value, 0.2. start growing 0.2 1.0 again.
it should give illusion of shrinking , growing on time. i'm struggling coming way this. thinking of using sort of boolean value "grow" tells me whether or not should grow (add .05 each factor), or shrink (subtract .05 each factor).
// these global vars. float rotate_x, rotate_y, rotate_z; float scale_x = 1.0f; float scale_y = 1.0f; float scale_z = 1.0f; ... void update_scale() { // can go here? } void animate() { // calculate new transformation values update_rotation(); update_scale(); //apply transformations glrotatef(rotate_x, 1, 0, 0); glrotatef(rotate_y, 0, 1, 0); glrotatef(rotate_z, 0, 0, 1); glscalef(scale_x, scale_y, scale_z); }
you can keep additional variable tracks current increment apply scale factor, , inverse every time reach upper/lower limit.
based on couple of constants:
const float min_scale = 0.2f; const float max_scale = 1.0f; const float scale_step = 0.05f;
then initialize:
float scalefact = max_scale; float scaleinc = -scale_step;
then on each update:
scalefact += scaleinc; if (scalefact < min_scale + 0.5f * scale_step) { scalefact = min_scale; scaleinc = scale_step; } else if (scalefact > max_scale - 0.5f * scale_step) { scalefact = max_scale; scaleinc = -scale_step; }
the above tweaking tests avoid comparing float values equal. instead of dealing possibly problematic float comparisons, use integer variable keep track of progress, , multiply actual scale factor.
Comments
Post a Comment