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 › making line bounce back
Page Index Toggle Pages: 1
making line bounce back (Read 662 times)
making line bounce back
Sep 29th, 2009, 12:27pm
 
hey there, im kinda new to processing, so i was hoping i could get some help with this. i have my code here:

int x;
int y;


void setup() {
size (400, 400);
x = 100;
background(0);



}


void draw() {
// background (0);



stroke (mouseY, mouseX, random(0, 255), 50);
strokeWeight(1);
line (x, x, mouseX, pmouseY);
x = x - 1;

}

my wish to have the line bouce off the walls so it is not continuously moving off stage. any ideas?

thanks so much for you help, chris
Re: making line bounce back
Reply #1 - Sep 29th, 2009, 1:25pm
 
its just moving off the stage because you substract 1 from x with every frame. So one point of your line is moving further away in the upper left corner. the other point is mapped to your mouseX and previous mouseY coordinate. doesnt make much sense with the pmouse but ok, still works though.

So what do you mean by bounce off the wall? could you explain a bit better how your line should behave?
Re: making line bounce back
Reply #2 - Sep 29th, 2009, 2:10pm
 
Thanks for having deleted the post from the wrong place (Exhibition). Removed my own answer as well...

I wrote: "Check the reference of if and width/height."
The traditional idea is to have a 'dir' variable with value 1 or -1. Instead of doing x = x - 1 (which can be written x -= 1; or even x--;, you do x += dir;. When x reaches 0 or width (tested with 'if'), you change the sign of dir (dir = -dir;).
Page Index Toggle Pages: 1