PDA

View Full Version : Translating an object round in a circle



TimOgunmakin
03-13-2013, 10:35 PM
Hey guys, Im pretty new to OpenGL and I'm currently messing around with Translated, Rotated and Scale.

What I am trying to do is move an object (glutSolidSphere) round in a circle however I have had no luck after hours of trying to figure this out :(

I have so far managed to draw a circle but thats as far as I have gotten.

Here is my Display function:


void display() {



glClear(GL_COLOR_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // reset the matrix
gluLookAt(cam.pos.x, cam.pos.y, cam.pos.z,
cam.lookAt.x, cam.lookAt.y, cam.lookAt.z,
cam.up.x, cam.up.y, cam.up.z);

drawAxes();


glPushMatrix();
glColor3f(255,0,255);

glTranslated(p1->getPosition().posx, p1->getPosition().posy, p1->getPosition().posz);

glRotated(0, 0, 0.5, 1);

newCircle(0, 0, 10);

glScalef(0.5, 0.5, 0.5);

glutSolidSphere(p2->getradius(), p2->getslices(), p2->getstacks());

glPopMatrix();


glColor3f(0.0, 0.0, 0.0);
stringstream ss;
stringstream planet;
ss << "Camera (" << cam.pos.x << ", " << cam.pos.y << ", " << cam.pos.z << ")";
outputText(-1.0, 0.5, ss.str());


planet << p1->getname() << " ( " << planetPos.posx << ", " << planetPos.posy << ", " << planetPos.posz << ")";

outputText(p1->getPosition().posx, p1->getPosition().posy, planet.str());


glFlush();

}

the newcircle() function that I am calling is used to draw the circle and my attempt to move the object. here is the function:


void newCircle(float x, float z, float radius)
{

glBegin(GL_LINE_STRIP);
for (float i = 0.0; i < 2*3.14159; i += 0.05)
{
glVertex3f(x + cos(i)*radius,0, z + sin(i)*radius);
glTranslatef(x + cos(i)*radius,0, z + sin(i)*radius);
}

glutPostRedisplay();
}

V-man
03-15-2013, 06:19 AM
In this
glColor3f(255,0,255);
Your value is too high. Floating point color should be between 0.0 and 1.0. 255 gets clamped to 1.0.
Use glColor3ub or glColor4ub.

In this
glRotated(0, 0, 0.5, 1);
Always use a vector that is normalized.

Now, if you want to have something like an orbit, you should translate first to move the sphere away from its center, then apply a rotation. The order in GL looks reversed :

glRotated(0, 0, 0.5, 1);
glTranslated(p1->getPosition().posx, p1->getPosition().posy, p1->getPosition().posz);

marek-knows.com
03-16-2013, 04:14 PM
You can also cheat and calculate the x,y points on a circle and just place your object there each frame:
x = r * cos( delta_angle )
y = r * sin( delta_angle )