Solved

Opengl moving object

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 65
Looking for example pivot year code used in Y2K 4 61
Java Loop 6 48
"Black Box" Testing of Control System Software 2 21
This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

914 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

17 Experts available now in Live!

Get 1:1 Help Now