PDA

View Full Version : Balls not getting generated in proper cordinates



centurion
04-26-2011, 10:56 AM
I was trying to make a game in which the balls would start falling down from a fixed height( y cordinate = 2.5f ) but x cordinate being generated randomly. also the balls which are reaching a fixed ycordinate( -2.5f) should disappear.
My observation was that at the starting a few balls would fulfill the above conditions and after that the balls are generated along the yaxis randomly whereas it should start at fixed location. the x cordinate shoulld be between -3.0f to 3.0f . But it is not within these limits . Also the speed of the ball keeps on increasing whereas it should be a constant.

Can anyone suggest wats wrong in my code?


#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <Windows.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <list>
using namespace std;
float x_max_boundry = 3.0f;
float x_min_boundry = -3.0f;
float y_max_boundry = 2.25f;
float y_min_boundry = -2.25f;
float radius = 0.02f;
int counter=0;
class paratrooper
{
private:
float x_pos;
float y_pos;
public:
paratrooper()
{
x_pos = (rand()%60 - 30.0f)/10;
y_pos = 2.25f;
}
void setycord()
{
y_pos -= 0.01;
}
float getxcord()
{
return x_pos;
}
float getycord()
{
return y_pos;
}
};

list<paratrooper*> list_para;

void reshape (int width, int height)
{
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60,(GLfloat)width / (GLfloat)height,1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void display (void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f );
glPushMatrix();
glColor3f(1.0f, 1.0f, 1.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_LINES);
glVertex3d(x_max_boundry,y_max_boundry,0);
glVertex3d(x_max_boundry,y_min_boundry,0);
glVertex3d(x_min_boundry,y_max_boundry,0);
glVertex3d(x_min_boundry,y_min_boundry,0);
glVertex3d(x_max_boundry,y_max_boundry,0);
glVertex3d(x_min_boundry,y_max_boundry,0);
glVertex3d(x_max_boundry,y_min_boundry,0);
glVertex3d(x_min_boundry,y_min_boundry,0);
glEnd();
list<paratrooper*>::iterator it1;
for(it1=list_para.begin(); it1!=list_para.end(); it1++)
{
paratrooper* para = *it1;
glTranslatef( para->getxcord(), para->getycord(), 0.0f);
glutSolidSphere(.02, 20, 20);
}
glPopMatrix();
glutSwapBuffers();
}
void update ( int value )
{
if(counter==20)
{
paratrooper* para = new paratrooper();
list_para.push_back(para);
counter=0;
}
else
{
counter++;
}
list<paratrooper*>::iterator it1;
for(it1=list_para.begin(); it1!=list_para.end(); it1++)
{
paratrooper* para = *it1;
para->setycord();
if(para->getycord()<y_min_boundry)
{
delete para;
it1=list_para.erase(it1);
}
}
glutTimerFunc(50,update,0);
glutPostRedisplay();
}
int main (int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Paratroopers");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(50,update,0);
glutMainLoop();
}



I creating these objects dynamically and storing them in a list. whenever a ball crosses the lower limit the object is deleted from the list.

carsten neumann
04-26-2011, 02:20 PM
please use "[ code], [ /code]" (no space after '[') around code snippets.



void display (void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f );
glPushMatrix();

// SNIP - rectangle drawing

list<paratrooper*>::iterator it1;
for(it1=list_para.begin(); it1!=list_para.end(); it1++)
{
paratrooper* para = *it1;
glTranslatef( para->getxcord(), para->getycord(), 0.0f);
glutSolidSphere(.02, 20, 20);
}
glPopMatrix();
glutSwapBuffers();
}


you only use glPushMatrix/glPopMatrix at the very beginning/end of your display function. If you want to position objects independently from each other push the matrix before applying transformations (glTranslate, glRotate, etc.) and pop it after drawing the object, i.e. your for loop should look like:



list<paratrooper*>::iterator it1;
for(it1=list_para.begin(); it1!=list_para.end(); it1++)
{
glPushMatrix();

paratrooper* para = *it1;
glTranslatef( para->getxcord(), para->getycord(), 0.0f);
glutSolidSphere(.02, 20, 20);

glPopMatrix();
}

centurion
04-27-2011, 12:39 AM
thanks