Attention: iDevGames forum has a new home at http://www.idevgames.com/forums.
Please update your bookmarks and linkage.
This forum has been locked and will be available for archival purposes only.
iDevGames - Mac, iPhone, iPad & iPod Game Developers Forum  


Graphics & Audio Programming All aspects of coding 2D/3D graphics.

 
 
Thread Tools Display Modes
Old
  (#1)
Jones
 
Posts: n/a
OpenGL Program / Loop Help - 2006.01.04, 07:05 PM

Howdy all,

I'm having some trouble making a loop in an OpenGL program. I used "OneSadCookie's" GLUT tutorial to build the "moving box" program. Then I decided I'd try to re-program how the box moved. Originally it used an opengl command that moved everything on the screen one way, using coordinates. (At least, I think that's how it was supposed to work.)

Anyway, I changed it so that it would update all the boxes vertex coordinates relative to the bottom left corner of the box. I added a simple function that would take the variable "boxSpeed", the amount of pixels to move the box diagonally. Then, based on that number and the current position of the box, I'd redraw the box.

However, things didn't work out as planned. The box would move once, at the start of the program, but not again. I realized that I'd forgotten to install some sort of looping mechanism into the main loop. I tried inserting a basic "while 2 > 1" loop, just to test it. Unfortunatly the program would then get stuck during startup, while the app was still bouncing in the dock.

I think the problem here is, I'm just not thinking logically. I'm not realizing exactly which parts (ie: which functions) need to be looped or not.

Here is the code:

Quote:

#include <stdlib.h>

#include <GLUT/glut.h>

// The X and Y coordinates of the bottom-left corner of the square
float box_bottomLeft_X = 0.0;
float box_bottomLeft_Y = 0.0;

void increaseXY(float boxSpeed)
{

// Increase the x and y values by 1 pixel(s)
box_bottomLeft_X = box_bottomLeft_X + boxSpeed;
box_bottomLeft_Y = box_bottomLeft_Y + boxSpeed;

}

void display(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUADS);
glVertex2f(box_bottomLeft_X, box_bottomLeft_Y);
glVertex2f(box_bottomLeft_X + 128.0f, box_bottomLeft_Y);
glVertex2f(box_bottomLeft_X + 128.0f, box_bottomLeft_Y + 128.0f);
glVertex2f(box_bottomLeft_X, box_bottomLeft_Y + 128.0f);
glEnd();

glutSwapBuffers();
}

void reshape(int width, int height)
{
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}

void idle(void)
{
glutPostRedisplay();
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(640, 480);

glutCreateWindow("Moving Square");

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);

increaseXY(5);

glutMainLoop();
return EXIT_SUCCESS;
}
Any help would be nice.

Oh yes, and a special thanks to OneSadCookie, for his wonderful GLUT introduction.

'Jones
   
Old
  (#2)
kodex
Moderator
 
Posts: 710
Join Date: 2004.09
Location: Colorado
2006.01.04, 07:18 PM

The problem you are having is increaseXY(5); is only being called once before you enter your main loop, try moving that command to inside your draw function.


Kyle Richter
DragonForged.com
Twitter: @kylerichter
   
Old
  (#3)
Jones
 
Posts: n/a
2006.01.04, 07:23 PM

Thanks! It works great now!
   
Old
  (#4)
Jones
 
Posts: n/a
2006.01.04, 09:20 PM

Ok, new problem.

I just added a new screen-edge collision detection system and I'm having trouble getting it to work properly. Maybe I'm just really bad at this (), but I'm not sure why it's not working. For some reason, the box just shoots off the left edge of the screen.

Here's the new code.

Quote:

#include <stdlib.h>

#include <GLUT/glut.h>

// The X and Y coordinates of the bottom-left corner of the square
float box_bottomLeft_X = 0.0;
float box_bottomLeft_Y = 0.0;

// These integers are true(1) or false(0) values for "Has the box hit the top/bottom of the screen?"
int box_hit_Top = 0;
int box_hit_Bottom = 0;

// The desired speed (pixel movement) of the box
float boxSpeed;
// The speed after processing
float boxSpeedOut;

// This function updates the box's coordinates
void increaseXY(float boxSpeed)
{

// If the check_boxPosition functions tells us the box has hit the top of the window, reverse the pixel movement value
if (box_hit_Top = 1 && box_hit_Bottom != 1)
{
boxSpeedOut = boxSpeed - (boxSpeed * 2);
}

else if (box_hit_Bottom = 1 && box_hit_Top != 1)
{
boxSpeedOut = boxSpeed;
}
else
{
boxSpeedOut = boxSpeed;
}

// Increase the x and y values by 1 pixel(s)
box_bottomLeft_X = box_bottomLeft_X + boxSpeedOut;
box_bottomLeft_Y = box_bottomLeft_Y + boxSpeedOut;

}

// Checks if the box has hit the top/bottom of the screen
void check_boxPosition()
{

if (box_bottomLeft_Y = 480 - 128.0) {

box_hit_Top = 1;

}

if (box_bottomLeft_Y = 0.0) {

box_hit_Bottom = 1;

}

}

void display(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUADS);
glVertex2f(box_bottomLeft_X, box_bottomLeft_Y);
glVertex2f(box_bottomLeft_X + 128.0f, box_bottomLeft_Y);
glVertex2f(box_bottomLeft_X + 128.0f, box_bottomLeft_Y + 128.0f);
glVertex2f(box_bottomLeft_X, box_bottomLeft_Y + 128.0f);
glEnd();

check_boxPosition();
increaseXY(1);

glutSwapBuffers();
}

void reshape(int width, int height)
{
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}

void idle(void)
{
glutPostRedisplay();
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(640, 480);

glutCreateWindow("Moving Square Application");

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);

glutMainLoop();
return EXIT_SUCCESS;
}
Thanks.
   
Old
  (#5)
kodex
Moderator
 
Posts: 710
Join Date: 2004.09
Location: Colorado
2006.01.04, 09:40 PM

try

void check_boxPosition()
{

if (box_bottomLeft_Y > 480 - 128.0) {
box_hit_Top = 1;
box_hit_Bottom = 0;
}

if (box_bottomLeft_Y < 0.0) {
box_hit_Bottom = 1;
box_hit_Top = 0;
}


Personally I always account for overflows, when dealing with collision detection its good to get into the habit because very very rarely will you have a whole value to do a precise ==. The reason you were having a problem is you were using "=" instead of "==", "=" sets the value to the right side, the "==" compares the two values.

Let me know if that works, if not I will look over the code a bit more closely


Kyle Richter
DragonForged.com
Twitter: @kylerichter
   
Old
  (#6)
Jones
 
Posts: n/a
2006.01.04, 10:36 PM

Doh! Why didn't I remember that "==" stuff.

*smashes head against wall*

It works wonderful now, by the way.

Thanks for your help, kodex!
   
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com
DevServe Network: iDevApps | uDevGames | iDevGames