## OpenGL Velocity and Grouping of Lines (C++, Xcode)

Nibbie
Posts: 1
Joined: 2010.07
Post: #1
I'm trying to add smooth movement with velocity to this program, but I'm not sure how to go about doing the deceleration. Could someone please point me in the right direction or if I'm doing it completely wrong correct me.

Oh and one more question:
If I were to want to turn this:
[code=c]
glBegin(GL_LINE_LOOP);
glVertex3f(objPosX, objPosY, 0.0f);
glVertex3f(objPosX, objPosY, 0.0f);
glVertex3f(objPosX, objPosY, 0.0f);
glVertex3f(objPosX, objPosY,0.0f);
glEnd();
[/code]

into a single object with a single x and y would creating a class with x, y member variables and a display function with the above code in it be the best way?

[code=C]
#include "../../shared/gltools.h"

GLfloat x = 0.0f;
GLfloat y = 0.0f;
GLfloat rsize = 25;

GLfloat xstep = 0;
GLfloat ystep = 0;

GLfloat windowWidth;
GLfloat windowHeight;

GLboolean firing = false;

GLint KeyDown[256];

void RenderScene(void)
{
x += xstep;
y += ystep;

if(x > (windowWidth-rsize + xstep)) {
x = windowWidth-rsize-xstep;
xstep=0;
}
else if(x < -(windowWidth + xstep)) {
x = -windowWidth -xstep;
xstep=0;
}
if(y > (windowHeight + ystep)) {
y = windowHeight-ystep;
ystep=0;
}
else if(y < -(windowHeight - rsize + ystep)) {
y = -windowHeight + rsize - ystep;
ystep=0;
}

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0f, 0.0f, 0.0f);

glRectf(x, y, x + rsize, y - rsize);

glutPostRedisplay();
glutSwapBuffers();
}
}

void Keyboard(int key, int xv, int yv)
{

switch (key)
{
case GLUT_KEY_LEFT:
xstep -= 0.05;
if (xstep < -0.15)
xstep = -0.15;
break;
case GLUT_KEY_UP:
ystep += 0.05;
if (ystep > 0.15)
ystep = 0.15;
break;
case GLUT_KEY_RIGHT:
xstep += 0.05;
if (xstep > 0.15)
xstep = 0.15;
break;
case GLUT_KEY_DOWN:
ystep -= 0.05;
if (ystep < -0.15)
ystep = -0.15;
break;
case 32:
firing = true;
}
}

void KeyboardUp(int key, int x, int y) {

switch (key)
{
case GLUT_KEY_LEFT:
xstep += 0.05;
if (xstep > 0)
xstep = 0;
break;
case GLUT_KEY_UP:
ystep -= 0.05;
if (ystep < 0)
ystep = 0;
break;
case GLUT_KEY_RIGHT:
xstep -= 0.05;
if (xstep < 0)
xstep = 0;
break;
case GLUT_KEY_DOWN:
ystep += 0.05;
if (ystep > 0)
ystep = 0;
break;
}
}

void Idle(void)
{
glutPostRedisplay();
}

void SetupRC(void)
{

glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}

void ChangeSize(int w, int h)
{
GLfloat aspectRatio;

if(h == 0)
h = 1;

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

aspectRatio = (GLfloat)w / (GLfloat)h;
if (w <= h)
{
windowWidth = 100;
windowHeight = 100 / aspectRatio;
glOrtho (-100.0, 100.0, -windowHeight, windowHeight, 1.0, -1.0);
}
else
{
windowWidth = 100 * aspectRatio;
windowHeight = 100;
glOrtho (-windowWidth, windowWidth, -100.0, 100.0, 1.0, -1.0);
}

glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(800,600);
glutCreateWindow("Velocity");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutIdleFunc(Idle);
glutSpecialFunc(Keyboard);
glutSpecialUpFunc(KeyboardUp);
//glutTimerFunc(33, TimerFunction, 1);

SetupRC();

glutMainLoop();

return 0;
}
[/code]
Member
Posts: 281
Joined: 2009.04
Post: #2

under 'Animation and Timing' you can setup a velocity that stays the same no matter the speed of the computer. This is needed for acceleration.

For acceleration/deceleration you'll want to have something like this: (pseudocode)

Code:
```when (UP ARROW)           yvel += 1.0; when (DOWN ARROW)           yvel -= 1.0; when (LEFT ARROW)           xvel -= 1.0; when (RIGHT ARROW)           xvel += 1.0; playerx += xvel * dt; // see OneSadCookie's tutorial playery += yvel * dt; //  "        "                    " xvel -= (xvel/2); // friction yvel -= (yvel/2); // friction```

As for the function, that looks fine.

~ Bring a Pen ~
⌘-R in Chief
Posts: 1,277
Joined: 2002.05
Post: #3
(Jul 28, 2010 05:10 PM)TheThirdL3g Wrote:  If I were to want to turn this:
[code=c]
glBegin(GL_LINE_LOOP);
glVertex3f(objPosX, objPosY, 0.0f);
glVertex3f(objPosX, objPosY, 0.0f);
glVertex3f(objPosX, objPosY, 0.0f);
glVertex3f(objPosX, objPosY,0.0f);
glEnd();
[/code]

into a single object with a single x and y would creating a class with x, y member variables and a display function with the above code in it be the best way?

It's certainly a valid way, though there's a good debate about whether the display function would be part of the class or not. The quick story is that if your object's class/struct only has information about *how* to draw it, then you can have separate renderer code which is shared.

And the above code won't do anything except make a single point.

The single {x, y} point will be the origin of your object. Where that origin is depends on what makes sense in your case. It could be the dimensional center of the object, the center of mass, or some fixed point of rotation... It's just somewhere.

The coordinates for the vertices in the rest of your object will be relative to that origin. If your object is a cube and the origin is the very center of the cube, then your process would be like:

Code:
```<translate to the origin of the object> <rotate to the orientation of the object> <scale if needed> <draw all 8 points of the cube:     {-0.5, -0.5, -0.5}     {-0.5,  0.5, -0.5}     { 0.5, -0.5, -0.5}     { 0.5,  0.5, -0.5}     {-0.5, -0.5,  0.5}     {-0.5,  0.5,  0.5}     { 0.5, -0.5,  0.5}     { 0.5,  0.5,  0.5} >```

Those points are all implicitly relative to the {x, y} origin, not explicitly done like
glVertex3f(object.x + point.x, object.y + point.y, object.z + point.z)