Implement friction and gravity in a simple Spring

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:

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.

Hello,

This is a good resource:
The Nature of Code

``:)~`

1 Like