Solved

Opengl moving object

Posted on 2011-03-13
5
967 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Please help| Getting the syntax error in below query. 2 70
Advice in Xamarin 21 109
batch file or script 4 47
Problem to copy file 14 43
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…

685 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