Solved

Opengl moving object

Posted on 2011-03-13
5
971 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:satsumo
ID: 35122707
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
ID: 35132260
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
ID: 35132945
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
ID: 35136912
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
ID: 35137487
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP/VB email question 4 72
Why is enum singleton a better approach than static factory 3 67
asp Google Map 2 93
Eclipse neon2 "Java build path" correctness 7 44
In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

751 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