Solved

Opengl moving object

Posted on 2011-03-13
5
961 Views
Last Modified: 2012-05-11
I am trying to move an object using user input in opengl. I can't seem to get it to work this is how I am trying to do it below.

this is how I draw the shape

static int weapon = 0;

void DrawWeapon(void)
{
   // Draw the Handle
   glPushMatrix();
   glRotatef(90.0f,5.0f,10.0f,5.0f);
   glScalef (2.0, 0.4, 1.0);
   glTranslatef(0.5f,-1.0f,0.6f);
   glutSolidCube (0.6);
   glRotatef ((GLfloat) weapon, 0.0, 0.0, 1.0);
   glPopMatrix();


   //Draw the hammer
   glPushMatrix();
   glTranslatef(0.6f,0.6f,-1.0f);
   glutSolidCube(0.9);
   glRotatef ((GLfloat) weapon, 0.0, 0.0, 1.0);
   glPopMatrix();

}

Here below is how I am trying to animate the weapon on user input.

 if (key=='e')
      {
        weapon = (weapon + 5) % 360;
        glutPostRedisplay();
      }

When I press e it does nothing. What am I doing wrong?  I can post the full code if I need to.
0
Comment
Question by:tango2009
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:satsumo
Comment Utility
It looks like the problem is drawing the object then setting the rotation.  Drawing the handle should look like this.

glPushMatrix();
glRotatef(90.0f,5.0f,10.0f,5.0f);
glScalef (2.0, 0.4, 1.0);
glTranslatef(0.5f,-1.0f,0.6f);
glRotatef ((GLfloat) weapon, 0.0, 0.0, 1.0);
glutSolidCube (0.6);
glPopMatrix();

Open in new window

This rotates the handle by 'weapon' degrees around the z-axis of the origin.
0
 

Author Comment

by:tango2009
Comment Utility
With this part of my code is there anyway to make the shape move back to its original position after it has moved. This is to give the movement of a hammer.

 if (key=='e')
      {
        weapon = (weapon + 5) % 360;
        glutPostRedisplay();
      }
0
 
LVL 12

Expert Comment

by:satsumo
Comment Utility
Yes, though this may be better without direct control.  It would be easier for the user to press the hammer key and have the game do the entire hammer animation, including returning to the start position, without them having to hold down the key.

You'd need to keep two values.  One is the angle of the hammer, the other is frame count through the animation.  I'm assuming that angle starts as zero and the hammer motion takes 20 frames.  It rotates about 90 degrees during the action and it pauses for 3 frames (as though it hit something).  I'm also assuming that the weapon has a boolean that means it is hammering.  You also need the value of PI to be defined and a cosine function (not sure what language you're using for this).

frame = 0;
angle = 0;

void UpdateWeapon (void)
{
   if (bWeaponIsActive)
   {
      ++frame;

      if ((frame < 9) && (frame > 11))
      {
         angle = 90 * (sin (PI / 20 * frame));
      }
      
      if (frame == 20) bWeaponIsActive = false;  // hammer action is finished
   }
}

Open in new window

You need to call UpdateWeapon constantly, while the game is running.  When the user presses the hammer key, set frame to zero and bWeaponIsActive to true.  If you don't want the pause remove the ((frame < 9) && (frame > 11)) line.
0
 

Author Comment

by:tango2009
Comment Utility
I am using c++ what is PI usually defined as. Would 3.14 be acceptable or do you need more values so that it will be accurate enough. Also waht do you mean by a cosine function can't cos be used just like sin is.
0
 
LVL 12

Accepted Solution

by:
satsumo earned 500 total points
Comment Utility
I changed the code after writing about cosine function, it used a cosine originally.  I mention it because I wasn't sure about language.  C/C++ have sin() and cos(), a few languages have Math.sin () and Math.cos ().  There is different syntax again in other languages.

PI as 3.14 is somewhat less acurate than say 3.1415927.  In this code PI is used to approximate an angle so it's not critical.  For some uses the accuracy does matter.  D3D defines it as 3.141592654f, the 'f' makes it a float rather than a double, for speed.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights  challenge 17 59
word0 challenge 4 52
C# code editing and collaboration 3 39
SUM 2 INTEGER ARRAYS INTO 1 10 56
A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This video teaches viewers about errors in exception handling.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now