Jump to content
  • Advertisement
Sign in to follow this  
Osiris990

OpenGL Click and Drag rotating cube

This topic is 3838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm attempting to program a click and drag rotating cube using OpenGL and the GLUT library but when I run the program, the click and drag doesn't quite work like I'd like. http://pastebin.com/f60a81edb That's my code. If necessary, I have a pre-compiled executable I can post. I'd suggest running the code from the command line so you can see the debug output in the terminal. Anyone have any ideas why it doesn't work like I'd like?

Share this post


Link to post
Share on other sites
Advertisement
Without looking: you aren't modifying some angles when the mouse is dragged and are now confused because the cube seems to be rotating around the wrong axis, right? This just won't work and the second rotation you apply will always look "wrong". On top of it, if you want to rotate the cube, not the view, you need to rotate around the global axes. If you're using a matrix math library just multiplying from the other side will work. Else, using the transposed axes from the cubes matrix as rotation axes should work as well. But you'll probably want to determine the right axis for a single rotation, based on the mouse motion.

Share this post


Link to post
Share on other sites
Unless I'm completely misunderstanding what you said, none of those problems apply to my particular program...

Share this post


Link to post
Share on other sites
Quote:
Original post by Osiris990
Unless I'm completely misunderstanding what you said, none of those problems apply to my particular program...


From what I'm seeing of your code it does, as you are doing exactly what I suspected. You move the mouse, you change two angles, then apply your rotations in a completely different order from how they happened and are surprised that the result is wrong.

Apart from that there is pointless complicatedness and typos:


int deltax, deltay;
deltax = currx - prevx;
deltay = curry - prevy;

if(deltax > 0) {
rotatex += abs(deltax);
if(rotatex > 360.0) {
rotatex -= 360.0;
}
} else if(deltax < 0) {
rotatex -= abs(deltax);
if(rotatex < 0.0) {
rotatex += 360.0;
}
}
[/quote]

When you catch yourself doing something like
'if X is positive, add the abs value and if X is negative, subtract the abs value', you should tell yourself "never code when you're tired". That, or one of has to doubt his understanding of basic mathematics.

First, if x is positive, then doing abs(x) is completely pointless. Second... adding a negative number is the same as subtracting the absolute value.

So in short:

rotatex += currx - prevx;
if(rotatex > 360.0) {
rotatex -= 360.0;
}
else if(rotatex < 0.0) {
rotatex += 360.0;
}


If you insist on the delta-variables, is there any reason that instead of initializing them, you leave them uninitialized just to make an assignment in the next line?

Also, that you add deltax to rotatex AND rotatey seems like an example of two simple rules:
-unnecessarily complicated code means more opportunity for bugs
-copy/paste should always be combined with paying attention.

But neither of that is likely to give you the kind of rotation you want, because rotations are using the objects current axes, not the "worlds" axes.

Share this post


Link to post
Share on other sites
Check this out. The last reply contains the source code of a program (using glut) doing exactly what you wanted.

http://www.gamedev.net/community/forums/topic.asp?topic_id=493530&whichpage=1

Share this post


Link to post
Share on other sites
Quote:
From what I'm seeing of your code it does, as you are doing exactly what I suspected. You move the mouse, you change two angles, then apply your rotations in a completely different order from how they happened and are surprised that the result is wrong.

Okay, then I must have just misunderstood you, which is unsurprising. I don't know a lot about working with three-dimensional environments so admittedly I don't fully understand what you're saying.

Quote:
Apart from that there is pointless complicatedness and typos:

Yeah, not quite sure what I was thinking there. It must have been a case of tired programming.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!