What's wrong with this code? (Newbie prob)

Started by
8 comments, last by Rulzern 22 years, 9 months ago
Well, what's wrong with this code: int DrawGLScene(GLvoid) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0f,0.0f,-20.0f); if (Up==FALSE) { glTranslatef(0.0f,speed,0.0f); glBegin(GL_TRIANGLE_STRIP); glColor3f(1.0f,0.0f,0.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glColor3f(1.0f,0.0f,0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glColor3f(1.0f,0.0f,0.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glEnd(); speed=speed-sped; sped=sped+0.1f; } else if (Up==TRUE) { glTranslatef(0.0f,-speed,0.0f); glBegin(GL_TRIANGLE_STRIP); glColor3f(1.0f,0.0f,0.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glColor3f(1.0f,0.0f,0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glColor3f(1.0f,0.0f,0.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glEnd(); speed=speed+sped; sped=sped-0.1f; } if (sped==0.0f) { sped=0.1f; Up=FALSE; } There is somthing wrong here (or thats what i think): if (sped==1.0f) { sped=0.9f; Up=TRUE; } return TRUE; } Sorry for not having it commented I am trying to make a triangle strip bounce up and down It won't turn when reaching 1.0f Thx Edit: Edited some code and added what is wrong Edited by - Rulzern on July 1, 2001 3:35:40 PM
Advertisement
There is somthing wrong here (or thats what i think):

if (sped==1.0f)
{
sped=0.9f;
Up=TRUE;
}
return TRUE;
}

It won''t turn when reaching 1.0f

BTW:
else if (Up==TRUE)
{
glTranslatef(0.0f,speed,0.0f);

is supposed to be

else if (Up==TRUE)
{
glTranslatef(0.0f,-speed,0.0f);

thx
if (sped==1.0f)
{
sped=0.9f;
Up=TRUE;
}
return TRUE;
}


avoid that kind of thing, they usually go wrong
if (sped>=1.0f)
will be better
(you can find me on IRC : #opengl on undernet)
One tip for you: when you to use commands like speed=speed-sped or sped=sped+0.1f, turn them to speed-=speed or speed+=5...it''s better!!


Later!!!

"Sex, Programming and Rock n' Roll"
_OdiN_,

Please don''t say such nonsense..

speed=speed-sped != speed-=speed

Thanks guys!
speed=speed-sped == speed-=sped
ARGHHH!
What''s wrong with this code

I think it''s in the change between up and down movement, coz when I run it, the triangle strip sorta "skips" between up and down so that when it goes up it is at the top of the screen and when it goes down it "jumps" to the bottom of the screen



int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-20.0f);
if (Up==FALSE)
{
glTranslatef(0.0f,speed,0.0f);
glBegin(GL_TRIANGLE_STRIP);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
speed-=sped;
sped+=0.01f;
}
else if (Up==TRUE)
{
glTranslatef(0.0f,-speed,0.0f);
glBegin(GL_TRIANGLE_STRIP);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
speed+=sped;
sped-=0.01f;
}
if (sped<=0.00f)
{
sped=0.01f;
Up=FALSE;
}
if (sped>=0.10f)
{
sped=0.09f;
Up=TRUE;
}
return TRUE;
}


Thx fer yer help
Try this, instead

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,speed,-20.0f);
glBegin(GL_TRIANGLE_STRIP);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
speed+=sped;
if(speed>0.5) Up=0; // Turn point
if(speed<-0.5) Up=1;
if(Up) sped+=0.01; else sped-=0.01; // Acc.
if(sped>0.1) sped=0.1; // Max speed
if(sped<-0.1) sped=-0.1;
}
I figured it out:

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-20.0f);
if (Up==FALSE)
{
glTranslatef(0.0f,-speed,0.0f);
glBegin(GL_TRIANGLE_STRIP);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
speed-=sped;
sped+=0.01f;
}
else if (Up==TRUE)
{
glTranslatef(0.0f,-speed,0.0f);
glBegin(GL_TRIANGLE_STRIP);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
speed+=sped;
sped-=0.01f;
}
if (sped<=0.00f)
{
sped=0.01f;
Up=FALSE;
}
if (sped>=0.10f)
{
sped=0.09f;
Up=TRUE;
}
return TRUE;
}

Thx guys!!

Sepiroth2: Were u into click b 4?

This topic is closed to new replies.

Advertisement