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.
Page Index Toggle Pages: 1
Mouse Rotation (Read 499 times)
Mouse Rotation
Jun 28th, 2006, 4:08pm
 
Hi there,

I have written a class that scales and rotates a box in 3D. At the moment there is a variable called 'angle' that simply gets incremented in my 'update' method:

Code:

angle += 0.01;

if(angle > TWO_PI) {
angle = 0.0;
}


Then in my 'render' method, I use that variable to rotate the box:

Code:

void render() {
rectMode(CENTER);
noStroke();
fill(153,75);
pushMatrix();
// position object
translate(x, y, -200);
pushMatrix();
// rotate object using 'angle' variable
rotateX(angle);
rotateY(angle);
rotateZ(90);
// draw box using location vectors
box(loc.x,loc.y,loc.z);
popMatrix();
popMatrix();
}


That all seems to work fine. However, what I am trying to do is get the box to rotate in response to the mouse x/y position, so that the direction of rotation shifts in the opposite direction of the mouse. Kind of like Yugo's 'Rosebox' piece (done in Director): http://yugop.com/ver3/index.asp?id=30

Does anyone know how to do this? I'm sure it involves using mouseX and mouseY in relation to the 'angle' variable - but I can't get it to work. Any help would be much appreciated as always :)

cheers,
Matt
Re: Mouse Rotation
Reply #1 - Jun 28th, 2006, 4:11pm
 
Just to make matters worse, I also meant to mention that the box needs some 'dampening' when it changes direction - rather than abruptly changing.

I don't ask for much do I! He he.
Re: Mouse Rotation
Reply #2 - Jun 28th, 2006, 10:15pm
 
For a direct mouseX/Y to rotation, you can use something like:

Code:
//gives a float between -1 and 1 depending on mouseX
float mx=((float)mouseX-(width/2.0))/(width/2.0);
//as above, but for mouseY
float mx=((float)mouseX-(height/2.0))/(height/2.0);


To dampen it, you'll need to store a speed of change of angle (e.g. -0.1 radians/frame) and change the angle by that ammount, and then work out if the change in mx/my is larger than the change in angle will be, to set the new speed of change of angle.
Re: Mouse Rotation
Reply #3 - Jun 29th, 2006, 9:25am
 
Thanks John, much appreciated. I'll give it a go and see how it works.

Matt Wink
Re: Mouse Rotation
Reply #4 - Jun 29th, 2006, 4:10pm
 
I've applied your code to the following:

Code:


float ax = 0.0;
float ay = 0.0;
float rSize; // rectangle size

void setup()
{
size(200, 200, P3D);
rSize = width/4;
noStroke();
fill(204, 204);
framerate(30);
}

void draw()
{
background(0);

//gives a float between -1 and 1 depending on mouseX
float mx=((float)mouseX-(width/2.0))/(width/2.0);
//as above, but for mouseY
float my=((float)mouseX-(height/2.0))/(height/2.0);

ax += mx;
ay += my;

translate(width/2, height/2);

rotateX(ax);
rotateY(ay);
rect(-rSize, -rSize, rSize*2, rSize*2);

}


Is that the correct way to apply it? I does kind of work - although the spinning gets a bit crazy when the mouse approaches the top left corner.
Re: Mouse Rotation
Reply #5 - Jun 29th, 2006, 6:23pm
 
Managed to crack it finally. Will upload my experiments when they're done. Thanks again.

Matt
Page Index Toggle Pages: 1