PDA

View Full Version : Moving triangle/ looping vertices



d1ff1cul1010
10-11-2014, 12:00 PM
Hello guys. I'm learning about how to make a triangle move in a circle and I'm trying to decipher the coding, but having a hard time understanding the math used in these functions.
.arcsynthesis.org/gltut/Positioning/Tutorial%2003. www and com

first:


void ComputePositionOffsets(float &fXOffset, float &fYOffset)
{
const float fLoopDuration = 5.0f;
const float fScale = 3.14159f * 2.0f / fLoopDuration;

float fElapsedTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;

float fCurrTimeThroughLoop = fmodf(fElapsedTime, fLoopDuration);

fXOffset = cosf(fCurrTimeThroughLoop * fScale) * 0.5f;
fYOffset = sinf(fCurrTimeThroughLoop * fScale) * 0.5f;
}

I dont understand what the purpose of fScale is, and I don't get what the purpose of finding the modulus of the the "fElapsedTime and the fLoopDuration, do the milliseconds keep counting upwards indefinitely and the fCurrentTimeThroughLoop is just calculating what part of each current "five second cycle" we are at?
I have a somewhat decent understanding of it, probably enough to move on in the book assuming it will go into more detail on these types of functions later, but I feel I can understand more, if anyone could elaborate more on some of the code here that would be great.
thanks

Ruggero Visitnin
10-11-2014, 12:44 PM
concerning "fScale" it seems like something to scale the distance of the rotation, in fact fScale is converted from degrees to radians (3.14159 is the approximation of PI), i don't know what fmodf does, but this is what i think:
imagine that you have your loopDuration to 5, and you are taking elapsed time every cycle, fmodf could be a way to avoid an "if" statement to check when fElapsedTime is greater than fLoopDuration or something like that.


float fmodf(float elapsedTime, float maxTime)
{
return elapsedTime % maxTime;
}

this is what i could think fmodf (float module float) does.
Hope it will help