# How to make object "vibrate" before moving upwards?

I want to make this square move back and forth (so that it looks like it’s vibrating or shaking) for several seconds before making it go upwards. How would I go about doing that? Thanks!

<
int y;
int yPos=350; // controls where it starts

void setup()
{
size (400,400);
smooth();
background(0);
noStroke();
fill(0,255,0);

}

void draw()
{
background(0);
ellipse(200, yPos, 40, 40);

yPos=yPos-3; // controls the speed
if (yPos>height-20)
{
yPos=20;
}
}

Hi and welcome

Here is idea to vibrating rect

``````float vibrate = 0.0;
float noise = 8;
void setup(){
size(400,400);

}

void draw(){
background(0,0,200);
vibrate +=0.4;
float x = noise(vibrate)* noise ;
rect(100+x, 100, 200,200);

}

``````
1 Like

Im terms of an animation you need to have a
timer to tell you when the jiggle stops and it starts to
Move

The timer would change a variable phase or so from 0
To 1

Then evaluate phase with if and show either jiggle or move

Thanks! Can you please explain me to though how I would add that to the ellipses I already have and then make it move upwards? I tried implementing some of the code but it just goes upwards. I must be adding certain things in the incorrect order.

hi
this how to stop vibrate while moving if you want vibrate before moving there is other methods to do that as @Chrisir said you can use timer and you can use random instead of noise

``````float y;
int yPos=400; // controls where it starts
float vibrate = 0.0;
float noise = 8;
void setup(){
size(400,400);

}

void draw(){
background(0,0,200);
vibrate +=0.9;
float x = noise(vibrate)* noise ;
ellipse(100+x, yPos,40, 40);
yPos=yPos-1; // controls the speed
if (yPos>=  height||yPos <= 350 ) {
vibrate =0;
}
{
//yPos=20;
}
}
``````

Ok thank you! I am trying to import an image of an asteroid and have it move on the x axis, but am struggling. Here is my code.

PImage ship;
PImage bPlanet;
PImage asteroid;

float y;
float x;
int yPos = 400; // controls where it starts
int xPos = 400;
float vibrate = 0.0;
float noise = 8;

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

}

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

fill(0, 10); // the 10 is supposed to control the speed of the blinks but doesn’t work
rect(0, 0, 0, width, height);
fill(255);
noStroke();
ellipse(random(width), random(height), 3, 3);

//rocket movement and vibration
vibrate +=10.0;
float x = noise(vibrate)* noise;
image(ship, 100+x, yPos, 100, 200);
yPos=yPos-2; // controls the speed
if (yPos>= height||yPos <= 350 ) {
vibrate =0;
}
{

//asteroid
image(asteroid, xPos, 100+y, 100, 200);
xPos = xPos-1;
if (xPos>width-20){
xPos = 20;
}

• Where are you struggling?

• What happens now and what do you want to happen instead?

My version

I noticed that the stars in the background flicker. I took care of that.

Also asteroid goes left / right repeatedly now.

``````

PImage ship;
PImage bPlanet;
PImage asteroid;

PGraphics  bckg ;

float y;
float x;
int yPos = 400; // controls where it starts
int xPos = 400;
float vibrate = 0.0;
float noise = 8;

void setup() {
size(400, 400);
background(0);

// resize
ship.resize(66, 0);
bPlanet.resize(66, 0);
asteroid.resize(66, 0);

// make background
bckg = createGraphics(400, 400);
bckg.beginDraw();
bckg.background(0);
bckg.stroke(255);
//  bckg.line(20, 20, mouseX, mouseY);
for (int i=0; i<100; i++) {
bckg.ellipse(random(width), random(height), 3, 3);
}
bckg.endDraw();
}

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

//fill(0, 10); // the 10 is supposed to control the speed of the blinks but doesn’t work
//rect(0, 0, 0, width, height);
fill(255);
noStroke();
//  ellipse(random(width), random(height), 3, 3);

//rocket movement and vibration
vibrate +=10.0;
float x = noise(vibrate)* noise;
image(ship, 100+x, yPos, 100, 200);
yPos=yPos-2; // controls the speed
if (yPos>= height||yPos <= 350 ) {
vibrate =0;
}
{

//asteroid
image(asteroid, xPos, 100+y, 100, 200);
if (xPos>width-20) {
}
if (xPos<20) {
}
}
}
//

``````

New version

renamed some variables, made two functions etc.

• decreasing of vibration
``````
PGraphics bckg;

PImage asteroid;
float xAsteroid = 400;
float yAsteroid;

PImage ship;
int yShip = 430; // controls where ship starts
int phaseShip=0;
float vibrateShip = 0.0;
float noiseShip = 18.0;

PImage bPlanet;

// ----------------------------------------------------------------------

void setup() {
size(400, 400);
background(0);

// resize
ship.resize( 100, 200);
bPlanet.resize(66, 0);
asteroid.resize( 100, 200);

// make background
bckg = createGraphics(400, 400);
bckg.beginDraw();
bckg.background(0);
bckg.stroke(255);
//  bckg.line(20, 20, mouseX, mouseY);
for (int i=0; i<100; i++) {
bckg.ellipse(random(width), random(height), 3, 3);
}
bckg.endDraw();
}

void draw() {
background(bckg);

//ship/rocket movement and vibration
showShip();

//asteroid
showAsteroid();
}

//-------------------------------------------------------------------------------------

void showShip() {
//ship/rocket movement and vibration

float xOffsetShip=0;
if (phaseShip==0) {
vibrateShip +=10.0;
xOffsetShip = (noise(vibrateShip)-0.5) * noiseShip;
}

//display
image(ship, 100+xOffsetShip, yShip);
text("ship", 100+xOffsetShip, yShip);

yShip=yShip-2; // controls the speed

if (phaseShip==0 && yShip <= 300) {
// decrease vibration
noiseShip-=0.3;
if (noiseShip<=0.4) {
// end vibration
vibrateShip = 0;
noiseShip=0;
phaseShip=1;
}
}//if
}//func

void showAsteroid() {
//asteroid

//display
image(asteroid, xAsteroid, 100+yAsteroid);
// move