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 › How to Get an Object to Slowly Decrease Speed
Page Index Toggle Pages: 1
How to Get an Object to Slowly Decrease Speed (Read 845 times)
How to Get an Object to Slowly Decrease Speed
May 18th, 2007, 3:53pm
 
Hi everyone,
Lets say I want a rectangle to move 5 pixels, then 3, then 2, then 1 so it appears the object is slowing down and isn't just making hard/sudden moves.

How can I do this?

I tried to create this command (where xpos is a variable to control the rectangle's x position):
xpos=xpos+5;
 xpos=xpos+3;
 xpos=xpos+2;
 xpos=xpos+1;

but the object appears to only move 11 pixels at once and not gradually.

Any help would be great.

Thanks,
Will
Re: How to Get an Object to Slowly Decrease Speed
Reply #1 - May 18th, 2007, 4:20pm
 
You need to have an xspeed variable, and move it once per frame, and then decrease the value of xspeed.

e.g.
Code:

//in draw()
xpos=xpos+xspeed;
xspeed=xspeed*0.75;
Re: How to Get an Object to Slowly Decrease Speed
Reply #2 - May 21st, 2007, 7:59pm
 
Hmm.  I'm a bit confused how this works.

I have void key pressed commands set up for left, right, up, and down so pressing the key just once will move the object 5 pixels, then 4, then 3, etc.

I tried to put in a loop that would move it the appropriate number of times, but that didn't seem to work.

FYI, I'm trying to emulate the graphics of the main fish in the game "Fishy".
Re: How to Get an Object to Slowly Decrease Speed
Reply #3 - May 21st, 2007, 8:51pm
 
Unfortunately you're misunderstanding a major thing about processing.

Nothing gets drawn to the screen until the draw function finishes

So doing xpos+=5;xpos+=4;xpos+=2;xpos+=1 is exactly the same as xpos+=12; since there's no magic that says "oh the position has changed, I'd better update the screen", it just wats until draw has finished, and then things are drawn.

So you need to make it move 5, wait fo rthe next time around draw, move it 4, wait for the next time round, move it 2, wait for the next time round. etc etc.

So you need to do something like:

Code:
int xspeed;
int xpos;

void setup()
{
//normal setup stuff...
xpos=width/2;
xspeed=0;
}

void draw()
{
//other draw stuff..
xpos+=xspeed;
if(xspeed>0)
xspeed=xspeed-1;
if(xspeed<0)
xspeed=xspeed+1;
//more drawing stuff maybe?
}

void keyPressed()
{
if(key==LEFT)
xspeed=5;
if(key==RIGHT)
xspeed=-5;
}
Re: How to Get an Object to Slowly Decrease Speed
Reply #4 - May 27th, 2007, 11:33pm
 
You're misunderstanding my question.

On a single key press, I want the object to move 5 pixels, then 4, then 3, then 2, then 1 and stop.  That's not happening on 5 key presses, but rather 1 single key press--which is why I tried to put in a loop command, as I explained earlier.
Re: How to Get an Object to Slowly Decrease Speed
Reply #5 - May 28th, 2007, 11:59am
 
No, I'm not, that's what that code does. You press a key once, and then one frame it moves 5, then the next frame it moves 4, the next it moves 3 etc.

Here, this is a cimplete example showing it working, just cut and paste this as a new sketch and you'll see:
Code:
int xspeed;
int xpos;

void setup()
{
size(300,300);
xpos=width/2;
xspeed=0;
frameRate(3);
}

void draw()
{
background(0);
xpos+=xspeed;
ellipse(xpos,height/2,5,5);
if(xspeed>0)
xspeed=xspeed-1;
if(xspeed<0)
xspeed=xspeed+1;
}

void keyPressed()
{
if(keyCode==LEFT)
xspeed=-5;
if(keyCode==RIGHT)
xspeed=5;
}
Re: How to Get an Object to Slowly Decrease Speed
Reply #6 - May 28th, 2007, 6:21pm
 
Ah, I get it now.

Thanks!
Page Index Toggle Pages: 1