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 › Trouble with rotate
Page Index Toggle Pages: 1
Trouble with rotate (Read 313 times)
Trouble with rotate
Feb 20th, 2009, 11:11pm
 
I am trying to recreate a screensaver where a small object, such as a ball, travels around the screen and bounces of the edges.

The ball speeds up and slows down.
I also want the ball to stretch when it is going faster, but since the ball isn't travelling at a right angle, I figured I couldn't do it.

Then I found rotate, and I can't get it to work right. Can someone show me how to properly use rotate without any other kind of translation and without matrices?
Re: Trouble with rotate
Reply #1 - Feb 20th, 2009, 11:58pm
 
helps?

void setup(){  
 size(300,300);
}
void draw(){
 background(0);
 float siz = sqrt(sq(mouseX-pmouseX)+sq(mouseY-pmouseY)) + 10;
 float ang = atan2(mouseY-pmouseY,mouseX-pmouseX);
 float x = cos (ang) * 20;
 float y = sin (ang) * 20;
 pushMatrix();
 translate(mouseX,mouseY);
 rotate(ang);
 beginShape();
 vertex(-siz-x ,-siz-y);
 vertex(-siz-x, siz+y);
 vertex( siz+x, siz+y);
 vertex( siz+x,-siz-y);
 endShape(CLOSE);
 popMatrix();
}


Re: Trouble with rotate
Reply #2 - Feb 21st, 2009, 12:07am
 
Sort of. I can get the rest of my program to work fine, but I can't get it to work when rotate is part of the program. I'm trying to rotate the ball 45 degrees, so that when it is moving quickly (up-left, down-right, etc.), it will stretch in that direction. I want to rotate my ellipse so that it stretches in the direction it is actually travelling, instead of on the x or y axis.
Re: Trouble with rotate
Reply #3 - Feb 21st, 2009, 1:11pm
 
I adapted an old bouncing balls sketch to illustrate your requirement:
Code:
Ball ball1, ball2;

void setup()
{
 size(600, 400);
 smooth();

 ball1 = new Ball(20, 50, -5, 8, 23, #002277);
 ball2 = new Ball(120, 150, 7, -3, 31, #007722);
}

void draw()
{
 background(#AAFFEE);
 ball1.Move();
 ball2.Move();
 ball1.Display();
 ball2.Display();
}


class Ball
{
 float posX, posY; // Position
 float speedX, speedY; // Movement (linear)
 float radius;
 color ballColor;
 float angle;
 float squashFactor;

 Ball(float px, float py, float sX, float sY, float r, color c)
 {
   posX = px; posY = py;
   speedX = sX; speedY = sY;
   radius = r; ballColor = c;
 }

 void Move()
 {
   squashFactor = 1.2;
   posX += speedX;
   if (posX < radius || posX > width - radius)
   {
     speedX = -speedX;
     posX += speedX;
     squashFactor = 1.0;
   }
   posY += speedY;
   if (posY < radius || posY > height - radius)
   {
     speedY = -speedY;
     posY += speedY;
     squashFactor = 1.0;
   }
   angle = atan2(speedY, speedX);
 }

 void Display()
 {
   noStroke();
   fill(ballColor);
// Old drawing method, easy and boring...
//    ellipse(posX, posY, radius * 2, radius * 2);
   // New method: I draw at 0, 0 and translate and rotate at the right place
   pushMatrix();
   translate(posX, posY);
   rotate(angle);
   ellipse(0, 0, radius * 2 * squashFactor, radius * 2 / squashFactor);
   stroke(255);
   noFill();
   ellipse(2 * radius / 3, 0, radius / 3, radius / 3);
   popMatrix();
 }
}

It is still rough, but it should give you some ideas.
Re: Trouble with rotate
Reply #4 - Feb 21st, 2009, 7:43pm
 
Thankyou!Thankyou!Thankyou!Thankyou!Thankyou!Thankyou!Thankyou!Thankyou!Thankyou
!Thankyou!Thankyou!Thankyou!Thankyou!Thankyou!Thankyou!Thankyou!

I have no idea what pushMatrix and popMatrix are actually doing (never got into matrices in precalc), but all the examples of translations use them, so I added them, and I tried your idea for drawing at (0,0) and then translating the objects to the desired position, and IT WORKS! YAY!

So far, it will only stretch in one direction, but I just need to adjust the code that determines my rotation angle variable. I'll post the actual code so you can see how I was doing everything once that's working.

EDIT: I guess my original request (no matrices) doesn't work if you want translations, huh?
Re: Trouble with rotate
Reply #5 - Feb 21st, 2009, 8:25pm
 
Actually, translation, rotation and such transformations use matrices behind the scene, but you don't have to worry about them.
Somehow, you can see pushMatrix as saveTransformationContext and popMatrix as restoreTransformationContext, that's all.
Transformations cumulate (although are reset after draw() call) so to make each drawing (here, each ball) independent of the others, you have to save and restore context.
Re: Trouble with rotate
Reply #6 - Feb 23rd, 2009, 4:34am
 
Okay, here's my code. I would've posted it earlier, and I probably would've gotten some better help from you guys, but this is for school homework, and I doubt my instructor would like seeing my project here before it's done and turned in.


//Bouncy Ball by compsciguy

//Initializing variables
int x;
int y;
int speedX;
int speedY;
int speed;
int speedCounter = 0;
int speedDirection = 1; //For speedDirection, 1=Up and 0=Down
float circleWidth = 70.0;
float circleHeight = 70.0;
boolean stretched = false;
float rotation;
int Rbg;
int Gbg;
int Bbg;
int Rball;
int Gball;
int Bball;
int colorChanger = 0;

//Setting up
void setup() {
 size(768,512);
 frameRate(30);
 x = (width/2);
 y = (height/2);
 //Randomly choose colors for background and ball
 Rbg = int(random(0,255));
 Gbg = int(random(0,255));
 Bbg = int(random(0,255));
 if(Rbg > 127) {
   Rball = Rbg - 128;
 } else {
   Rball = Rbg + 128;
 }
 if(Gbg > 127) {
   Gball = Gbg - 128;
 } else {
   Gball = Gbg + 128;
 }
 if(Bbg > 127) {
   Bball = Bbg - 128;
 } else {
   Bball = Bbg + 128;
 }
 smooth();//Apparently this enables anti-aliasing (avoids pixelized curves)
}

void draw() {
 //These lines randomly choose a starting direction for the ball; BETWEEN -2 and 2 because random does not include
 while(speedX == 0) {
 speedX = int(random(-2,2));
 speedX *= 5;
 println(abs(speedX));
 }
 while(speedY == 0) {
 speedY = int(random(-2,2));
 speedY *= 5;
 }
 background(Rbg,Gbg,Bbg);
 //Makes the ball move across the screen
 x = x + speedX;
 y = y + speedY;
   
 //This will make the ball speed up or slow down after it has traveled 30 increments of its speed
 //speedCounter increments by 1 at the end of the draw function
 if(speedCounter == 30) {
   if(speedDirection == 1) {
     if(speedX > 0) {
       speedX++;//Travelling right, speeding up
     } else {
       speedX--;//Travelling left, speeding up
     }
     if(speedY > 0) {
       speedY++;//Travelling down, speeding up
     } else {
       speedY--;//Travelling up, speeding up
     }
   }
   if(speedDirection == 0) {
     if(speedX > 0) {
       speedX--;//Travelling right, slowing down
     } else {
       speedX++;//Travelling left, slowing down
     }
     if(speedY > 0) {
       speedY--;//Travelling down, slowing down
     } else {
       speedY++;//Travelling up, slowing down
     }
   }
   speedCounter = 0;
   println(abs(speedX));
 }
 
 //Make the ball slow down if it is speeding up and the speed reaches 30
 if(abs(speedX) == 30) {
   if(speedDirection == 1) {
     speedDirection = 0;
   }
 }

 //Make the ball speed up if it is slowing down and the speed reaches 1
 if(abs(speedX) == 5) {
   if(speedDirection == 0) {
     speedDirection = 1;
   }
 }

 //When the ball's edge reaches a wall, it will change direction
 //I added some code here so that it would change direction when the EDGE of the
 //ball touched the side, instead of the middle of the ball
 //UPDATE: When I rotate the ball, it bounces when the edge is just past the wall;
 //This creates an effect like the ball is invisibly squishing into the wall before bouncing off
 if ((x > width - (circleHeight*(sqrt(2)/4))) || (x < 0 + (circleHeight*(sqrt(2)/4)))) {
   speedX = speedX * -1;
   colorChanger++;
 }
 if ((y > height - (circleHeight*(sqrt(2)/4))) || (y < 0 + (circleHeight*(sqrt(2)/4)))) {
   speedY = speedY * -1;
   colorChanger++;
 }
Re: Trouble with rotate
Reply #7 - Feb 23rd, 2009, 4:37am
 
Stupid message limits. >_>
Here's the rest:



 //Changes the colors of the background and the ball when the ball has "bounced" 3 times
 //The greater than is in there because sometime it will hit two walls in one run of the draw function
 //and send it past 3 before it gets back to the colorChanger function
 //Also, the colors are set to be high-contrast (R, G, and B are each 128 color units apart from
 //the ball to the background
 if(colorChanger >= 3) {
       Rbg = int(random(0,255));
   Gbg = int(random(0,255));
   Bbg = int(random(0,255));
   if(Rbg > 127) {
     Rball = Rbg - 128;
   } else {
     Rball = Rbg + 128;
   }
   if(Gbg > 127) {
     Gball = Gbg - 128;
   } else {
     Gball = Gbg + 128;
   }
   if(Bbg > 127) {
     Bball = Bbg - 128;
   } else {
     Bball = Bbg + 128;
   }
   colorChanger = 0;
 }
 
 //Stretch the ball in the correct direction if the ball's speed is 14 or greater
 if(((abs(speedX)) >= 14) && (!stretched)) {
   stretched = true;
 }
 
 //Unstretch the ball if the ball's speed is less than 14
 if(((abs(speedX)) < 14) && (stretched)) {
   stretched = false;
 }
 
 //STRETCH IT!
 if(stretched) {
   circleHeight = 85.0;
   circleWidth = 55.0;
 } else {
   circleHeight = 70.0;
   circleWidth = 70.0;
 }
 
 //Rotate the ball when it is stretched, so that it will be stretched in the correct direction
 if(stretched) {
   if((speedX/speedY) > 0) {//This means positive; if both are + or -, then this will come out +, and it is moving NW or SE
     rotation = (PI/4)*-1;
   } else {//This means negative (speedX and speedY cannot be 0); if one is + and the other is -, then this will be -, and it's moving NE or SW
     rotation = PI/4;
   }
 }
 
 //Draw the ball at the correct location; I am drawing it at 0,0 and translating the location because it works better with the rotate command
 pushMatrix();
 stroke(Rball,Gball,Bball);
 fill(Rball,Gball,Bball);
 translate(x,y);
 rotate(rotation);
 ellipse(0,0,circleWidth,circleHeight);
 popMatrix();
 speedCounter++;
}
Page Index Toggle Pages: 1