# How to find the coordinate offset of a rotation?

Page 1 of 1

## 12 Replies - 3997 Views - Last Post: 07 July 2012 - 05:39 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=284981&amp;s=4379f95c7981def5e6df7b68212d37c1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dartoscoder

• D.I.C Regular

Reputation: 0
• Posts: 313
• Joined: 15-May 09

# How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 02:31 PM

I need to find how to get the offset of a rotation for my OpenGL test. The code they give here
```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;
}
```
for the rotation of the triangle won't work for some reason. The xOffset is always .5 and the yOffset always 0. So since I can't get the current location for x or for y or x' or y', I need to find a way to calculate the offset of the rotation.

This post has been edited by dartoscoder: 06 July 2012 - 02:44 PM

Is This A Good Question/Topic? 1

## Replies To: How to find the coordinate offset of a rotation?

### #2 Skydiver

• Code herder

Reputation: 4265
• Posts: 13,661
• Joined: 05-May 12

## Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 02:52 PM

Are you sure that values are fixed?

It looks to me that in
```fXOffset = cosf(fCurrTimeThroughLoop * fScale) * 0.5f;
fYOffset = sinf(fCurrTimeThroughLoop * fScale) * 0.5f;

```

fCurrTimeThroughLoop will have a value from [0.0f, 5.0f) which will give you varying values for the offsets (unless there is a bug in glutGet(GLUT_ELAPSED_TIME)).

This post has been edited by Skydiver: 06 July 2012 - 02:53 PM

### #3 dartoscoder

• D.I.C Regular

Reputation: 0
• Posts: 313
• Joined: 15-May 09

## Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 02:57 PM

They are supposed to be changing. It is trying to make a triangle rotate. Sorry I probably should have put the website I got that from. Here you go.

Also I don't think there is a bug in glutGet (I'm sure you said that as a joke but) because I tried to do the same thing in sfml with sf::Clock and sf::Time and I got the same result.

### #4 Skydiver

• Code herder

Reputation: 4265
• Posts: 13,661
• Joined: 05-May 12

## Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 04:40 PM

Yeah, I meant it partly as a joke. The other part was a just a subtle hint to watch multiple passes through the code if you had attached a debugger and was inspecting the values.

Anyway to get the offset of the rotation, you can a subtle refactoring. Change the code to:
```void ComputePositionOffsetsForTime(int time, float &fXOffset, float &fYOffset)
{
const float fLoopDuration = 5.0f;
const float fScale = 3.14159f * 2.0f / fLoopDuration;

float fElapsedTime = time / 1000.0f;

float fCurrTimeThroughLoop = fmodf(fElapsedTime, fLoopDuration);

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

void ComputePositionOffsets(float &fXOffset, float &fYOffset)
{
ComputePositionOffsetsForTime(glutGet(GLUT_ELAPSED_TIME), fXOffset, fYOffset);
}

```

So if you call:
```ComputePositionOffsetsForTime(t0, x0, y0);
ComputePositionOffsetsForTime(t1, x1, y1);

```

You can get the deltas between (x0, y0) and (x1, y1) between the times t0 and t1.

### #5 dartoscoder

• D.I.C Regular

Reputation: 0
• Posts: 313
• Joined: 15-May 09

## Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 05:53 PM

I tried something like that with sfml. I passed in the sf::Time object from the sf::Clock.getElapsedTime() function. Then I did the same operations but the result was the same. The value of fCurrTimeThroughLoop always ends up too small to be changed into a number that would cause cos to be lower than one or sin to be higher than 0. I don't understand why though.

### #6 Skydiver

• Code herder

Reputation: 4265
• Posts: 13,661
• Joined: 05-May 12

## Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 06:12 PM

Are you sure you are linking the correct C runtime library to compute sinf() and cosf()?

With the following code:
```    for(int i = 0; i < 5000; i += 250)
{
float x, y;
ComputePositionOffsetsForTime(i, x, y);
cout << i << ": " << x << ", " << y << endl;
}

```

I'm seeing these values:
```0: 0.5, 0
250: 0.475528, 0.154508
500: 0.404509, 0.293892
750: 0.293893, 0.404508
1000: 0.154509, 0.475528
1250: 6.33795e-007, 0.5
1500: -0.154508, 0.475529
1750: -0.293892, 0.404509
2000: -0.404508, 0.293893
2250: -0.475528, 0.15451
2500: -0.5, 1.26759e-006
2750: -0.475529, -0.154507
3000: -0.404509, -0.293891
3250: -0.293894, -0.404507
3500: -0.15451, -0.475528
3750: -1.90139e-006, -0.5
4000: 0.154507, -0.475529
4250: 0.293891, -0.40451
4500: 0.404507, -0.293895
4750: 0.475528, -0.154511

```

### #7 dartoscoder

• D.I.C Regular

Reputation: 0
• Posts: 313
• Joined: 15-May 09

## Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 07:34 PM

I am using #include <math.h>

but i am not getting those values. xOffset is .5 except for like once every 10 seconds where it becomes .4972 and yOffset is always 0 except for early in the beginning of the program where it is like .3.

I really don't get it.

### #8 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 07:53 PM

I don't get why you are not using OpenGL functionality like glRotate3f*(), etc. The problem that you are having generally is that OpenGL is using matrices and vectors to compute transformations, and as far as I can see you're not.

You're having so many problems with graphics programming that I personally think it would be best that you actually learn some theory around graphics. You'll never learn cutting and pasting code.

### #9 Skydiver

• Code herder

Reputation: 4265
• Posts: 13,661
• Joined: 05-May 12

## Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 07:54 PM

So either you keep on calling glutInit() every 5 seconds or less, or ComputePositionOffsets() is only being called every 5 seconds.

### #10 dartoscoder

• D.I.C Regular

Reputation: 0
• Posts: 313
• Joined: 15-May 09

## Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 08:09 PM

ButchDean, on 06 July 2012 - 08:53 PM, said:

You're having so many problems with graphics programming that I personally think it would be best that you actually learn some theory around graphics. You'll never learn cutting and pasting code.

Thanks for the input BUT I am not just copying and pasting code. I am taking a class on directX that is teaching how these graphics pipelines work. I am learning OpenGL on my own. This is the only part of all of the code (except for the GLSL stuff) that I am actually copying line for line. I am not having so many problems just many questions that you D.I.C. people are so kind to answer.

I am not using glRotatef because I know how that works and I am just doing these tutorials to learn more about modern OpenGL programming.

This post has been edited by dartoscoder: 06 July 2012 - 08:11 PM

### #11 dartoscoder

• D.I.C Regular

Reputation: 0
• Posts: 313
• Joined: 15-May 09

## Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 08:18 PM

Skydiver, on 06 July 2012 - 08:54 PM, said:

So either you keep on calling glutInit() every 5 seconds or less, or ComputePositionOffsets() is only being called every 5 seconds.

The elapsed time is always below 1. So I know it is being called more than once per second. But it still won't work correctly.

I'll post the whole program code here.
```#include "stdafx.h"

GLuint program;
GLuint bufferPos[2];
GLuint offsetLocation;
sf::Window* window;

const float vertex[] =
{
0.0f,    0.5f, 0.0f, 1.0f,
0.5f, -0.366f, 0.0f, 1.0f,
-0.5f, -0.366f, 0.0f, 1.0f,
1.0f,    0.0f, 0.0f, 1.0f,
0.0f,    1.0f, 0.0f, 1.0f,
0.0f,    0.0f, 1.0f, 1.0f
};

(
"#version 330\n"
"layout(location = 0) in vec4 position;\n"
""
"layout(location = 1) in vec4 color;\n"
"smooth out vec4 theColor;\n"
"uniform vec4 offset;\n"
"void main()\n"
"{\n"
"	vec4 finalOffset = vec4(offset.x, offset.y, 0, 0);\n"
"	gl_Position = position + finalOffset;\n"
"	theColor = color;\n"
"}\n"
);

"#version 330\n"
"smooth in vec4 theColor;\n"
"out vec4 mess;\n"
"void main()\n"
"{\n"
"	mess = theColor;\n"
"}\n"
);

void positionOffset(float& xOffset, float& yOffset, sf::Time& time)
{
const float loop = 5.f;
const float fscale = 3.14159f * 2.f / loop;

float yirm = time.asMilliseconds() / 1000.f;

float currTime = fmodf(yirm, loop);

xOffset = cosf(currTime * fscale) * .5f;
yOffset = sinf(currTime * fscale) * .5f;
}

{
std::vector<float> newData((sizeof(vertex) / sizeof(float)));
memcpy(&newData[0], vertex, sizeof(vertex));

for(int i = 0; i < (sizeof(vertex) / sizeof(float)); i += 4)
{
newData[i] += xOffset;
newData[i + 1] += yOffset;
}

glBindBuffer(GL_ARRAY_BUFFER, bufferPos[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertex), &newData[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

{
std::fstream stream;
stream.open(filename.c_str());
try
{
if(!stream.is_open())
{
throw e;
}
}
catch (std::exception& e)
{
fprintf(stderr, "%s", e.what());
throw;
}
std::string file;
stream >> file;
stream.close();

try
{
}
catch (std::exception& e)
{
fprintf(stderr, "%s", e.what());

throw;
}
}

{

GLint status;
if(status == GL_FALSE)
{
GLint infoLogLength;

GLchar* log = new GLchar[infoLogLength + 1];
{
fprintf(stderr, "Vertex: ");
}
{
fprintf(stderr, "Frag: ");
}
fprintf(stderr, "Comple Shader Error:\n %s", log);
delete[] log;
}

}

{
GLuint theProgram = glCreateProgram();

for(int i = 0; i < shaderList.size(); i++)
{
}

GLint status;
if (status == GL_FALSE)
{
GLint infoLogLength;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);

GLchar *strInfoLog = new GLchar[infoLogLength + 1];
glGetProgramInfoLog(program, infoLogLength, NULL, strInfoLog);
delete[] strInfoLog;
}

for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)

return theProgram;
}

void initProgram()
{

offsetLocation = glGetUniformLocation(program, "offset");
}

void initBuffer()
{
glGenBuffers(1, bufferPos);

glBindBuffer(GL_ARRAY_BUFFER, bufferPos[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void draw(sf::Time& time)
{
float yOffset = 0.f;
float xOffset = 0.f;
positionOffset(xOffset, yOffset, time);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);

glUseProgram(program);

std::cout << xOffset << std::endl;

glUniform4f(offsetLocation, xOffset, yOffset, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, bufferPos[0]);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)48);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glUseProgram(0);
}

void init()
{
initProgram();
initBuffer();
}

int main()
{

sf::ContextSettings settings;
settings.antialiasingLevel = 8;
settings.depthBits = 12;
window = new sf::Window(sf::VideoMode(500, 500, 32), "Hello", sf::Style::Close, settings);
window->setActive();

init();

std::cout << glGetString(GL_VERSION);

glViewport(0, 0, 500, 500);

sf::Clock time;
sf::Event ev;
while(window->isOpen())
{
time.restart();

if(window->pollEvent(ev))
{
if(ev.type == sf::Event::Closed)
{
window->close();
}
}

sf::Time test = time.getElapsedTime();
float a = test.asSeconds();
draw(time.getElapsedTime());

window->display();
}

return 0;
}

```

and here is the stdafx.h file
```#pragma once

#include <Windows.h>
#include <vector>
#include "SFML\Graphics.hpp"
#include <GL/GL.h>
#include <GL\GLU.h>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <math.h>

```

I thought that this would be a simple enough problem for just that snippit but I guess I messed it up worse than that. Also I know the way this is written is not elegant but its just for my learning so that is why I made it like this.

This post has been edited by dartoscoder: 06 July 2012 - 08:20 PM

### #12 Skydiver

• Code herder

Reputation: 4265
• Posts: 13,661
• Joined: 05-May 12

## Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 10:27 PM

Why are you calling time.restart() on line 249 everytime through your render loop?

### #13 dartoscoder

• D.I.C Regular

Reputation: 0
• Posts: 313
• Joined: 15-May 09

## Re: How to find the coordinate offset of a rotation?

Posted 07 July 2012 - 05:39 AM

That restarts the SFML clock. You need to call it or the elapsed time will never reset to 0. It will just keep going up.

I tried taking that out and it worked. I then tried making a float that would just take the time.inMilliseconds() / 1000.f value and just add it every loop with clock.restart() and it worked too.

I guess it wanted total time not deltaTIme.

Thanks for the help everyone.

This post has been edited by dartoscoder: 07 July 2012 - 05:46 AM