# This is the third chapter of the book "The Nature of Code" about friction and resistance, I don't know what the problem is, as a beginner this is my first problem I can't solve, so I ask you for help.THANKS！

**URL:** <https://discourse.processing.org/t/this-is-the-third-chapter-of-the-book-the-nature-of-code-about-friction-and-resistance-i-dont-know-what-the-problem-is-as-a-beginner-this-is-my-first-problem-i-cant-solve-so-i-ask-you-for-help-thanks/36331>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [April 14, 2022, 9:00am UTC](https://discourse.processing.org/t/this-is-the-third-chapter-of-the-book-the-nature-of-code-about-friction-and-resistance-i-dont-know-what-the-problem-is-as-a-beginner-this-is-my-first-problem-i-cant-solve-so-i-ask-you-for-help-thanks/36331 "2022-04-14T09:00:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiajia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jiajia/32/16099_2.png) [@jiajia](https://discourse.processing.org/u/jiajia)\
**Post date:** [April 14, 2022, 9:00am UTC](https://discourse.processing.org/t/this-is-the-third-chapter-of-the-book-the-nature-of-code-about-friction-and-resistance-i-dont-know-what-the-problem-is-as-a-beginner-this-is-my-first-problem-i-cant-solve-so-i-ask-you-for-help-thanks/36331/1 "2022-04-14T09:00:39Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

 ![屏幕截图 2022-04-14 165906](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/f/0/f0ca7ccacbeb6d3fa000b91552de9a6eb6af42b0.jpeg)

这是《The Nature of Code》书中第三章关于摩擦力与阻力的作业，我不知道哪里有问题，作为初学者这是我的第一个问题我没有办法去解决所以在这里求助大家。谢谢大家！

Mover movers = new Mover[100];  
Liquid liquid;  
void setup(){  
size(640,320);  
smooth();  
for(int i =0;i\<movers.length;i++){  
movers[i] = new Mover(random(0.1,5),0,0);  
}  
liquid = new Liquid(0,height,2,height/2,0.1);  
}

void draw(){  
background(255);  
liquid.display();  
PVector wind=new PVector(0.01,0);  
// PVector gravity=new PVector(0,0.1);  
for(int i =0;i\<movers.length;i++){  
if(movers[i].isInside(liquid)){  
movers[i].drag(liquid);  
}  
float m =0.1\*movers[i].mass;  
PVector gravity = new PVector(0,m);  
float c =0.1;  
PVector friction = movers[i].velocity.get();  
friction.mult(-1);  
friction.normalize();  
friction.mult(c);

movers[i].applyForce(friction);  
movers[i].applyForce(wind);  
movers[i].applyForce(gravity);  
movers[i].update();  
movers[i].display();  
movers[i].checkEdges();  
}  
}

class Mover{  
PVector location;  
PVector velocity;  
PVector acceleration;  
float mass;  
Mover(float m,float x,float y){  
mass= m;  
location = new PVector(x,y);  
velocity = new PVector(0,0);  
acceleration = new PVector(0,0);  
}  
void applyForce(PVector force){  
PVector f = PVector.div(force,mass);  
acceleration.add(f);  
}  
void update(){  
velocity.add(acceleration);  
location.add(velocity);  
acceleration.mult(0);  
}  
void display(){  
stroke(0);  
fill(174);  
ellipse(location.x,location.y,mass_16,mass_16);  
}  
void checkEdges(){  
if(location.x\>width){  
location.x = width;  
velocity.x\*=-1;  
}else if(location.x \< 0){  
velocity.x\*=-1;  
location.x = 0;  
}  
if(location.y\>height){  
velocity.y\*=-1;  
location.y = height;  
}  
}  
}

class Liquid{  
float x,y,w,h;  
float c;  
PVector acceleration;  
float mass;  
PVector location;  
PVector velocity;  
Liquid(float x\_,float y\_,float w\_,float h\_,float c\_){  
x=x\_;  
y=y\_;  
w=w\_;  
h=h\_;  
c=c\_;  
}  
void display(){  
noStroke();  
fill(174);  
rect(x,y,w,h);  
}  
void applyForce(PVector force){  
PVector f = PVector.div(force,mass);  
acceleration.add(f);  
}

boolean isInside(Liquid l){  
if(location.x\>l.x && location.x\<l.x+l.w && location.y\>l.y && location.y\<l.y+l.h){  
return true;  
}else{  
return false;  
}  
}

void drag(Liquid l){  
float speed = velocity.mag();  
float dragMagnitude = l.c_speed_speed;  
PVector drag = velocity.get();  
drag.mult(-1);  
drag.normalize();  
drag.mult(dragMagnitude);  
applyForce(drag);  
}  
}

---

<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:** [April 14, 2022, 10:10am UTC](https://discourse.processing.org/t/this-is-the-third-chapter-of-the-book-the-nature-of-code-about-friction-and-resistance-i-dont-know-what-the-problem-is-as-a-beginner-this-is-my-first-problem-i-cant-solve-so-i-ask-you-for-help-thanks/36331/2 "2022-04-14T10:10:05Z")

</div>

Hello,

The answer is here:  
[https://natureofcode.com/book/chapter-2-forces/](https://natureofcode.com/book/chapter-2-forces/)

The chapter tells you which class to put the functions in.

`:)`
