Skip to main content
gamedev.net
🔒 Locked

Something wrong with my code

Started by subnet_rx Dec 20, 2001 at 5:58 PM 6 replies 550+ views
Original Post
subnet_rx
subnet_rx
I''m typing up the robot from OGLGP book, but something is wrong that I am just not seeing. The arms and legs are rotating 360 degrees instead of moving back and forth. I thought it was my angle checks, but I''ve compared it line by line and found nothing. Does anyone see what''s wrong? //Draw Arm void DrawArm(float xPos, float yPos, float zPos) { glPushMatrix(); glColor3f(0.4f, 0.4f, 0.4f); glTranslatef(xPos, yPos, zPos); glScalef(1.0f, 5.0f, 1.0f); DrawCube(0.0f, 0.0f, 0.0f); glPopMatrix(); } //Draw Head void DrawHead(float xPos, float yPos, float zPos) { glPushMatrix(); glColor3f(0.5f, 0.5f, 0.5f); glTranslatef(xPos, yPos, zPos); glScalef(2.0f, 2.0f, 2.0f); DrawCube(0.0f, 0.0f, 0.0f); glPopMatrix(); } //Draw Torso void DrawTorso(float xPos, float yPos, float zPos) { glPushMatrix(); glColor3f(0.4f, 0.4f, 0.4f); glTranslatef(xPos, yPos, zPos); glScalef(3.0f, 5.0f, 2.0f); DrawCube(0.0f, 0.0f, 0.0f); glPopMatrix(); } //Draw Leg void DrawLeg(float xPos, float yPos, float zPos) { glPushMatrix(); glColor3f(0.4f, 0.4f, 0.4f); glTranslatef(xPos, yPos, zPos); glScalef(1.5f, 6.0f, 2.0f); DrawCube(0.0f, 0.0f, 0.0f); glPopMatrix(); } //Draw Robot void DrawRobot(float xPos, float yPos, float zPos) { static bool leg1 = true; static bool leg2 = false; static bool arm1 = false; static bool arm2 = true; glPushMatrix(); glTranslatef(xPos, yPos, zPos); DrawHead(1.0f, 2.0f, 0.0f); DrawTorso(1.5f, 0.0f, 0.0f); glPushMatrix(); //Define Arm #1 and Movement if (arm1) armAngle[0] = armAngle[0] + 0.5f; else armAngle[0] = armAngle[0] - 0.5f; if (armAngle[0] >= 15.0f) arm1 = false; if (armAngle[0] <= -15.0f) arm1 = true; glTranslatef(0.0f, -0.5f, 0.0f); glRotatef(armAngle[0], 1.0f, 0.0f, 0.0f); DrawArm(2.5f, 0.0f, -0.5f); glPopMatrix(); glPushMatrix(); //Define Arm #2 and Movement if (arm2) armAngle[1] = armAngle[1] + 0.5f; else armAngle[1] = armAngle[1] - 0.5f; if (armAngle[1] >= 15.0f) arm1 = false; if (armAngle[1] <= -15.0f) arm1 = true; glTranslatef(0.0f, -0.5f, 0.0f); glRotatef(armAngle[1], 1.0f, 0.0f, 0.0f); DrawArm(-1.5f, 0.0f, -0.5f); glPopMatrix(); glPushMatrix(); //Define Leg #1 and Movement if(leg1) legAngle[0] = legAngle[0] + 0.5f; else legAngle[0] = legAngle[0] - 0.5f; if (legAngle[0] >= 15.0f) leg1 = false; if (legAngle[0] <= -15.0f) leg1 = true; glTranslatef(0.0f, -0.5f, 0.0f); glRotatef(legAngle[0], 1.0f, 0.0f, 0.0f); DrawLeg(-0.5f, -5.0f, -0.5f); glPopMatrix(); glPushMatrix(); //Define Leg #2 and Movement if(leg2) legAngle[1] = legAngle[1] + 0.5f; else legAngle[1] = legAngle[1] - 0.5f; if (legAngle[1] >= 15.0f) leg1 = false; if (legAngle[1] <= -15.0f) leg1 = true; glTranslatef(0.0f, -0.5f, 0.0f); glRotatef(legAngle[1], 1.0f, 0.0f, 0.0f); DrawLeg(1.5f, -5.0f, -0.5f); glPopMatrix(); glPopMatrix(); } void Render() { glEnable(GL_DEPTH_TEST); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); angle = angle + 1.0f; if (angle >= 360.0f) angle = 0; glPushMatrix(); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -30.0f); glRotatef(angle, 0.0f, 1.0f, 0.0f); DrawRobot(0.0f, 0.0f, 0.0f); glPopMatrix(); glFlush(); SwapBuffers(g_HDC); }
zealouselixir
zealouselixir
Um, the code from my CD works. Be sure you typed it in correctly or copied and pasted all of it.

Later,
ZE.
[twitter]warrenm[/twitter]
subnet_rx
subnet_rx
yes, I loaded the project file from the CD and it works great. I just can''t figure out why mine doesn''t. I could easily move on, but I''d love to know what is causing this behavior. In my experience, you learn more solving problems, than when you don''t have one.

Maybe I don''t understand what I just typed, and if so, I''d like to know. Because I just can''t see it. It''s checking the angles for each arm and leg, but when it hits 15 (or -15), it isn''t reversing. It''s obvious to me that I don''t understand something, since I can''t see the problem.
Vlion
Vlion
Well, offhand I cant see the error.
But what you might do is use brackets to very explixitly block out your code.
That way you can easily get a mental grasp on what happens where.


I came, I saw, I got programmers block.
~V''''lion
~V'lionBugle4d
subnet_rx
subnet_rx
heh, well, I just learned how particular OpenGL is in another example. I declared my angle variable inside the Initialize function instead of as a global variable and it caused nothing to rotate. That tells me there really could be something wrong in any part of my program, not just the parts that define the robot.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.