View Full Version : Explain me this circular movments

kululu

10-19-2013, 12:08 PM

I'm reading a book about openGL and I got to a part that after I draw a triangle the author taught the reader to make the triangle move in a circular movment

this is the code he was using to calculate the offset for the new x and y of the vertices

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;

}

java version :

final float fLoopDuration = 5.0f;

final float fScale = 3.14159f * 2.0f / fLoopDuration;

float fElapsedTime = System.nanoTime() / 1000.0f;

float fCurrTimeThroughLoop = fmodf(fElapsedTime, fLoopDuration);

fXOffset = cosf(fCurrTimeThroughLoop * fScale) * 0.5f;

fYOffset = sinf(fCurrTimeThroughLoop * fScale) * 0.5f;

the fXOffset and the fYOffset are being added to the vbo using glBufferSubData and after they're being sent again to the vertex shader

now I dont understand how it makes the triangle move in a circular motion

I hope someone could make things a bit more clear to me

kululu

10-20-2013, 07:53 AM

anyone help please ? :o

carsten neumann

10-20-2013, 08:12 AM

One possible reason for not getting an answer can be that the question is not entirely clear. For example I'm wondering if your problem is understanding why fXOffset/fYOffset describe a point on a circle around the origin with radius 0.5? Or is it how these offsets are applied to the vertex positions of the triangle (in which case you did not post the relevant code piece)? Could you clarify please?

kululu

10-20-2013, 12:00 PM

One possible reason for not getting an answer can be that the question is not entirely clear. For example I'm wondering if your problem is understanding why fXOffset/fYOffset describe a point on a circle around the origin with radius 0.5? Or is it how these offsets are applied to the vertex positions of the triangle (in which case you did not post the relevant code piece)? Could you clarify please?

ok ill try to explain again :

I have a VA with defined positions and I'm transfering this data to my VBO obviously.

in the main loop I call 2 methods :

computePositionOffsets();

adjustGlVertexBufferObjectData();

the first one is computing the offsetX and offsetY the second one is adding those values to teach vertices so lets say xOffset yOffset = 3

I do this xVertex1 += xOffset

yVertex1 += yOffset

xVertex2 += xOffset

and so on for the 3 vertices

let me paste you the second methods since i didnt post it here :

private void adjustGlVertexBufferObjectData()

{

FloatBuffer newData = BufferUtils.createFloatBuffer(vertexData.length / 2);

for (int i = 0; i < vertexData.length / 2; i++)

{

newData.put(vertexData[i]);

}

for (int iVertex = 0; iVertex < vertexData.length / 2; iVertex += 4)

{

newData.put(iVertex, newData.get(iVertex) + fXOffset);

newData.put(iVertex + 1, newData.get(iVertex + 1) + fYOffset);

}

newData.flip();

glBindBuffer(GL_ARRAY_BUFFER, glVertexBufferObject);

glBufferSubData(GL_ARRAY_BUFFER,0 , newData);

glBindBuffer(GL_ARRAY_BUFFER, 0);

}

ok so what I dont understand is how the way he calculated the offsets helps to create this circular motion

I mean why calculate them this way

fXOffset = (float) (Math.cos(currTimeThroughLoop * scale) * 0.5f);

fYOffset = (float) (Math.sin(currTimeThroughLoop * scale) * 0.5f);

If someone told me make a triangle and make it move around a point in a circular motion and also so it wont pass the extents of the screen I don't think I'll ever thought of doing it the way he did because I dont really understand what he did

I know whats cos whats sin I see he calculated the time through loop, all that is nice and dandy but whats the logic of using cos on currTimeThroughLoop * scale and sin currTimeThroughLoop * scale and then multiplying it by 0.5 I just dont get how this part works to make the circular movment work

I really hope I explained it better this time and I appriciate your reply :)

carsten neumann

10-20-2013, 12:34 PM

The argument currTimeThroughLoop * scale to sin/cos is a value in [0, 2*PI), so some angle (in radians) that increases as time elapses and wraps around if it would become larger than 2*PI. So the vector (fXOffset, fYOffset) describes the offset from the origin of a point on a circle of radius 0.5. Each frame the offset vector for that frame is added to the original positions of the triangle's vertices, causing the vertices to move in a circle about their original position.

Not sure if that helps. Perhaps make a drawing of the triangle at its unmodified position. Then add to each vertex a small offset (same direction and length for each vertex) and reconnect the vertices. Pick another offset and repeat, you will notice how the triangle moves in a circular pattern.

kululu

10-20-2013, 01:17 PM

The argument currTimeThroughLoop * scale to sin/cos is a value in [0, 2*PI), so some angle (in radians) that increases as time elapses and wraps around if it would become larger than 2*PI. So the vector (fXOffset, fYOffset) describes the offset from the origin of a point on a circle of radius 0.5. Each frame the offset vector for that frame is added to the original positions of the triangle's vertices, causing the vertices to move in a circle about their original position.

Not sure if that helps. Perhaps make a drawing of the triangle at its unmodified position. Then add to each vertex a small offset (same direction and length for each vertex) and reconnect the vertices. Pick another offset and repeat, you will notice how the triangle moves in a circular pattern.

I find it hard to belive that someone decided ok lets make a moving triangle in circular motion, no problem lets add the offset created from the sin/cos of the radians based on the time that the triangle is in the current lap

I get more or less (more like less) what he did but if someone was asking me to do it.. jesus this is so hard of coming with this idea on how to do it :\

Is there something I do do inorder to improve my thinking skills to come up with such ideas on how to perform such tasks ?

I learn math at school but although I know what cos/sin and radians all that I dont belive I could come up with this idea on how to do it unfortunately .. :\

also what is the * 0.5 used for, how does it helps

Powered by vBulletin® Version 4.2.5 Copyright © 2018 vBulletin Solutions Inc. All rights reserved.