We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Rotational Navigation with the Mouse + KeyPress
Page Index Toggle Pages: 1
Rotational Navigation with the Mouse + KeyPress (Read 809 times)
Rotational Navigation with the Mouse + KeyPress
Nov 27th, 2007, 2:03am
 
I'm trying to work out a navigation system for a 3d script that I'm writing.  I've successfully been able to use the mouse position to control radial rotation.  

Now, I only want to enable mouse rotation when the 'r' key is pressed.  I have been able to get this to work while the 'r' key is pressed down, but as soon as it is released, the object returns to its original position.  The strange thing is, when I press 'r' down again, the object goes back to the orientation it was in when 'r' was released.

Why aren't these transformations carried over?

Here is a simplified version with just a sphere and the keypress function.  

The code:


import processing.opengl.*;

boolean rotateMode;

void setup() {
 size(600, 600, OPENGL);
}

void keyPressed() {
 if (key == 'r') {
   rotateMode = true;
 }
}

void keyReleased() {
 if (key == 'r') {
   rotateMode = false;
 }
}



void draw() {
 background(0);
 fill(150);
 translate(width/2, height/2, -width);

 if (rotateMode) {

   rotateY(map(mouseX, 0, width, -PI, PI));
   rotateX(map(mouseY, 0, height, -PI, PI));
 }

 sphere(400);

}
Re: Rotational Navigation with the Mouse + KeyPres
Reply #1 - Nov 27th, 2007, 3:31am
 
You're only applying 'rotate' when the key is pressed, instead of only allowing the mouse to change the rotation when the key is pressed. As the 'draw' resets everything, you're basically returning to 0 at the beginning.

Try changing your 'draw' to (you'll need to declare two additional floats - rX and rY):

void draw() {
 background(0);
 fill(150);
 translate(width/2, height/2, -width);
 if (rotateMode) {
   rY = map(mouseX, 0, width, -PI, PI);
   rX = map(mouseY, 0, height, -PI, PI);
 }
 rotateY(rY);
 rotateX(rX);
 sphere(400);
}
Re: Rotational Navigation with the Mouse + KeyPres
Reply #2 - Nov 27th, 2007, 4:30am
 
Thanks! That totally worked.

I was getting thrown off because i *thought* that the rotated state was preserved and could be returned to when i pressed 'r'.  The real story is that i wasn't moving my mouse at that point, so the script was using the same mouseX and mouseY values to determine the rotation.  Silly me.
Page Index Toggle Pages: 1