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 & HelpSyntax Questions › rotate image in relation to mouse movement
Page Index Toggle Pages: 1
rotate image in relation to mouse movement (Read 2463 times)
rotate image in relation to mouse movement
Apr 14th, 2010, 12:21am
 
Hi there,

I am new to Processing and am finding it difficult to find an answer to my problem.  There have been answers to how to rotate an image but not to the extent I want (or then I have not found).

My aim is for an image to be centered where the mouse is, and have the image rotate along with the mouse movement.  So for example, if the mouse is at the bottom left hand corner of the screen and i move it towards the top right, the image will tilt in the direction of movement of the mouse and then returns to its original state when the mouse movement has stopped.  I was thinking the tan rule and rise over run, but in my code it did not go accordingly.  Also i was thinking how would this rule work if the x position did not change ( y / 0 is impossible after all).

Basically, the image will actually be a large set of images being played that simulate something similar to brushstrokes, but other graphics will also be incorporated whilst time changes, so those brushstrokes may turn out to become something different.

If anyone knew how to solve my dilemna, I would be most glad  Cheesy
Re: rotate image in relation to mouse movement
Reply #1 - Apr 14th, 2010, 1:43am
 
the4got10one wrote on Apr 14th, 2010, 12:21am:
in my code it did not go accordingly.

Show it!
Well, not a full paint program, but a small sketch illustrating what you tried.
I suppose you already guessed you have to use the vector formed by pmouseX/Y and mouseX/Y.
Re: rotate image in relation to mouse movement
Reply #2 - Apr 14th, 2010, 1:54am
 
I mock this up quickly, it is difficult without seen what you have so far, hope it helps,

Code:
PVector location;
PVector velocity;

float boxSize = 40.0;


void setup() {
 size(640, 480);
 smooth();
 
 location = new PVector(mouseX, mouseY);
}


void draw() {
 background(255);
 
 // get velocity vector relative to mouse position
 velocity = new PVector(mouseX - pmouseX, mouseY - pmouseY);
 location.add(velocity);
 
 pushMatrix();
 // move box to location
 translate(location.x, location.y);
 
 // check if mouse is moving
 if (mouseX == pmouseX && mouseY == pmouseY) {
   rotate(0.0);
 }
 else {
   rotate(velocity.heading2D());
 }
 
 // draw black box
 noStroke(); fill(0);
 rect(-boxSize * 0.5, -boxSize * 0.5, boxSize, boxSize);
 
 // draw red line
 stroke(255, 0, 0);
 line(0.0, 0.0, boxSize * 0.5, 0.0);
 
 popMatrix();
}



Cheers
rS
Re: rotate image in relation to mouse movement
Reply #3 - Apr 14th, 2010, 3:07am
 
Code:

// Example 15-4: Image sequence

int maxImages = 10; // Total # of images
int imageIndex = 0; // Initial image to be displayed is the first

// Declaring an array of images.
PImage[] images = new PImage[maxImages];

//variables for color changing
float r = random (255);
float g = random (255);
float b = random (255);

float deg = 5;
float rad = radians(deg);

void setup() {
 size(600,400);
 
 // Loading the images into the array
 // Don't forget to put the JPG files in the data folder!
 for (int i = 0; i < images.length; i ++ ) {
   images[i] = loadImage( "animal" + i + ".jpg" );
 }
 frameRate(15);
}

void draw() {
 
 background(0);
 imageMode (CENTER);
 image(images[imageIndex],mouseX,mouseY);
 tint (r,g,b);
 // increment image index by one each cycle
 // use modulo " % "to return to 0 once the end of the array is reached
 imageIndex = (imageIndex + 1) % images.length;
 
 deg = atan(pmouseY/pmouseX);
 
 rotate (rad);
 
 //color to change consistently
 r = r + 5;
 g = g + 5;
 b = b + 5;
 
 if (r > 255) {
   r = random (255);
 }
 if (g > 255) {
   g = random (255);
 }
 if (b > 255) {
   b = random (255);
 }

}




its at a beginners coding level ... Embarrassed

basis is from the Learning Processing book

http://rapidshare.com/files/375721240/mock_movement.jpg

above is a link to an image of what i sorta mean, quick sketch. sorry, couldnt figure how to insert images.
Re: rotate image in relation to mouse movement
Reply #4 - Apr 14th, 2010, 3:13am
 
@ nardove

Thank you for your fast responce with the mock.  That's basically how i want it to work, just with a .jpg file instead.  How would that be done?  Sorry if the answer to that does turn out to be very basic.  Guess I have to read up on the matrix thing.
Re: rotate image in relation to mouse movement
Reply #5 - Apr 14th, 2010, 3:42am
 
Glad it helps.

All you need to do is draw your image inside the pushMatrix and popMatrix methods like this:
Code:
pushMatrix();
translate(location.x, location.y);
// if statement goes here
image(images[imageIndex], 0.0, 0.0);
popMatrix();


Check out this post regarding the push and pop matrix methods
http://processing.org/discourse/yabb2/num_1236633057.html

Let us know it that work out

Cheers
rS
Re: rotate image in relation to mouse movement
Reply #6 - Apr 14th, 2010, 3:54am
 
Okay I will let you know how it goes asap.  Thank you for your help  Grin
Re: rotate image in relation to mouse movement
Reply #7 - Apr 14th, 2010, 11:29pm
 
okay so i put it into my code and played around abit.  And it seems to work just as I wanted. THANK YOU VERY VERY VERY MUCH NARDOVE!!!!!!!  Cheesy  Cheesy  Cheesy  Cheesy  Cheesy

I'm kind of understanding the code as well, but there is one section i dont understand.  

Code:
  // check if mouse is moving
else {
rotate(velocity.heading2D());
}


I'm not too sure what it means ( by the .heading2D() part ), if you could help me clarify this, it would be most helpful and greatly appreciated
Re: rotate image in relation to mouse movement
Reply #8 - Apr 15th, 2010, 1:01am
 
Sure!

heaving2D() Calculate the angle of rotation for this vector (only 2D vectors)

More here http://dev.processing.org/reference/core/javadoc/processing/core/PVector.html#he...

If you need to understand how to work with vectors, the best place is
http://www.shiffman.net/teaching/nature/

I will also recommend to get this PDF http://stores.lulu.com/dshiffman from the same author

Cheers
rS
Re: rotate image in relation to mouse movement
Reply #9 - Apr 15th, 2010, 1:03am
 
the4got10one wrote on Apr 14th, 2010, 11:29pm:
I'm not too sure what it means ( by the .heading2D() part ), if you could help me clarify this, it would be most helpful and greatly appreciated


When in doubt search the Reference Wink

Since 'velocity' is a PVector that's the first place to look since heading2D() is presumably a method of the PVector class.  And sure enough from the refs you'll find a link to some extended PVector documentation.
Re: rotate image in relation to mouse movement
Reply #10 - Apr 15th, 2010, 5:51am
 
Cool cool. Thank you again for your help nardove! and also thank you blindfish! Greatly appreciated  Cheesy
Page Index Toggle Pages: 1