Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Opengl moving object

Posted on 2011-03-13
5
Medium Priority
?
999 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
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 2000 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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Make the most of your online learning experience.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

580 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