# Implement friction and gravity in a simple Spring

**URL:** <https://discourse.processing.org/t/implement-friction-and-gravity-in-a-simple-spring/30070>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [May 12, 2021, 8:49pm UTC](https://discourse.processing.org/t/implement-friction-and-gravity-in-a-simple-spring/30070 "2021-05-12T20:49:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ikermorales](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@ikermorales](https://discourse.processing.org/u/ikermorales)\
**Post date:** [May 12, 2021, 8:49pm UTC](https://discourse.processing.org/t/implement-friction-and-gravity-in-a-simple-spring/30070/1 "2021-05-12T20:49:52Z")

</div>

Hello, Im trying to make a Spring in Proccesing, I need to add “friction” and “gravity” to my Spring to finish the project but I dont figure it out how.

I have managed to get here, I have created a basic Spring, but I don’t know how to implement those 2 variables:

```auto
float y = 250;
float velocity = 0;
float restLength = 200;
float k = 0.01;

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

void draw() {
  background(112, 50, 126);
  noStroke();
  fill(45, 197, 244);
  circle(300, y, 64);

  float x = y - restLength;
  float force = -k * x;

  // F = A
  velocity += force;
  y += velocity;

  velocity *= 0.99;
}

```

Would appreciate some help, thanks.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 12, 2021, 9:00pm UTC](https://discourse.processing.org/t/implement-friction-and-gravity-in-a-simple-spring/30070/2 "2021-05-12T21:00:45Z")

</div>

Hello,

This is a good resource:  
_[The Nature of Code](https://natureofcode.com/book/)_

``:)~`
