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

**URL:** https://discourse.processing.org/t/how-to-make-object-vibrate-before-moving-upwards/35157
**Category:** Coding Questions
**Created:** [February 14, 2022, 4:37am UTC](https://discourse.processing.org/t/how-to-make-object-vibrate-before-moving-upwards/35157 "2022-02-14T04:37:55Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![AndrewTheGoat22](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@AndrewTheGoat22](https://discourse.processing.org/u/AndrewTheGoat22)
#### Post date: [February 14, 2022, 4:37am UTC](https://discourse.processing.org/t/how-to-make-object-vibrate-before-moving-upwards/35157/1 "2022-02-14T04:37:56Z")

</div>

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;  
}  
}

>

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [February 14, 2022, 6:42am UTC](https://discourse.processing.org/t/how-to-make-object-vibrate-before-moving-upwards/35157/2 "2022-02-14T06:42:57Z")

</div>

Hi and welcome

Here is idea to vibrating rect

```auto
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);

}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 14, 2022, 11:09am UTC](https://discourse.processing.org/t/how-to-make-object-vibrate-before-moving-upwards/35157/3 "2022-02-14T11:09:55Z")

</div>

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

---

<div class="post-metadata">

### Author: ![AndrewTheGoat22](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@AndrewTheGoat22](https://discourse.processing.org/u/AndrewTheGoat22)
#### Post date: [February 14, 2022, 12:46pm UTC](https://discourse.processing.org/t/how-to-make-object-vibrate-before-moving-upwards/35157/4 "2022-02-14T12:46:06Z")

</div>

> [@jafal](#):
>
> ```auto
> 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);
> 
> }
> 
> ```

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.

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [February 14, 2022, 4:39pm UTC](https://discourse.processing.org/t/how-to-make-object-vibrate-before-moving-upwards/35157/5 "2022-02-14T16:39:57Z")

</div>

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

```auto
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;
}
}

```

---

<div class="post-metadata">

### Author: ![AndrewTheGoat22](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@AndrewTheGoat22](https://discourse.processing.org/u/AndrewTheGoat22)
#### Post date: [February 14, 2022, 7:17pm UTC](https://discourse.processing.org/t/how-to-make-object-vibrate-before-moving-upwards/35157/6 "2022-02-14T19:17:28Z")

</div>

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);  
ship = loadImage(“Rocket1.png”);  
bPlanet = loadImage(“bPlanet.png”);  
asteroid = loadImage(“asteroid.png”);

}

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;  
}

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 16, 2022, 1:03pm UTC](https://discourse.processing.org/t/how-to-make-object-vibrate-before-moving-upwards/35157/7 "2022-02-16T13:03:47Z")

</div>

> [@AndrewTheGoat22](#):
>
> but am struggling.

- 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.

```auto

PImage ship;
PImage bPlanet;
PImage asteroid;

PGraphics bckg ; 

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

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

  //load
  ship = loadImage("Rocket1.png");
  bPlanet = loadImage("bPlanet.png");
  asteroid = loadImage("asteroid.png");

  // 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);
    xPos = xPos+xPosAdd;
    if (xPos>width-20) {
      xPosAdd = -1;
    }
    if (xPos<20) {
      xPosAdd = 1;
    }
  }
}
//

```

* * *

**New version**

renamed some variables, made two functions etc.

- decreasing of vibration

```auto

PGraphics bckg; 

PImage asteroid;
float xAsteroid = 400;
float yAsteroid;
int xAsteroidPosAdd=1;

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);

  //load
  ship = loadImage("Rocket1.png");
  bPlanet = loadImage("bPlanet.png");
  asteroid = loadImage("asteroid.png");

  // 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
  xAsteroid = xAsteroid+xAsteroidPosAdd;

  // turn 
  if (xAsteroid>width-20) {
    xAsteroidPosAdd = -1;
  }
  if (xAsteroid<20) {
    xAsteroidPosAdd = 1;
  }
  //
}//func 
//

```
