2.4: Friction Force – The Nature of Code

[ad_1]
Chapter: 2

Official book website:

Twitter:

In this video, we cover how to simulate friction in code.

Read along: natureofcode.com/book/chapter-2-forces/#chapter02_section7

Example: github.com/shiffman/The-Nature-of-Code-Examples/tree/master/Processing/chp2_forces/NOC_2_4_forces_friction

Help us caption & translate this video!


Posted

in

by

Tags:

Comments

13 responses to “2.4: Friction Force – The Nature of Code”

  1. Stephen Vinuya Avatar

    Thank you dan, you made physics so interesting and kinda easy 🙂

  2. zoilo garingan Avatar

    Any alternatives or remedy on the method get() of the PVector?? it is already deprecated 🙁

  3. Numinous Cranium Avatar

    I love you, I will bake you a cake of unicorns and love.

    My objects were trying to fall through the ground – oddly weird, but I looked at your code and you are setting the location back to the width or height when it goes beyond, I love you, thank you so much. I am now going to code all night and study hard because of you.

    Live long good feller.

  4. SilenceOfThought Avatar

    here is my solution of homework 🙂

    box b;
    boolean on=false;
    void setup(){
    size(600,300);
    b = new box();
    }
    void mousePressed(){
    on=true;
    }

    void draw(){

    background(255);

    //apply forces

    PVector gravity = new PVector(0,0.3);
    b.addforce(gravity);
    PVector loc = b.loc.get();
    if (on){
    float dist = dist((loc.x)+20,height/2-20,mouseX,height/2-20);
    PVector mouse = new PVector(mouseX,height/2-20);
    PVector move = mouse.sub(loc);
    move.setMag(dist/1000);
    b.addforce(move);}
    float c;
    if ((loc.x>width/2-120) && (loc.x<width/2+80)) c=-0.3;
    else c=-0.05;
    PVector friction = b.vel.get();
    friction.normalize();
    friction.mult(c);
    b.addforce(friction);

    b.update();
    b.edge();
    b.display();
    fill(100);
    rect(0,height/2,width,height/2);
    fill(0);
    rect(width/2-100,height/2,200,height/2);
    }

    class box {
    PVector loc,vel,acc;
    box(){
    loc = new PVector(0,0);
    vel = new PVector(0,0);
    acc = new PVector(0,0);
    }

    void addforce(PVector force){
    PVector f = force;
    acc.add(force);
    }

    void update(){
    loc.add(vel);
    vel.add(acc);
    acc.mult(0);
    vel.limit(10);
    }

    void edge(){
    if (loc.x>width-40){loc.x=width-40; vel.x*=-0.8;}
    if (loc.x<0){loc.x=0; vel.x*=-0.8;}
    if (loc.y>height/2-20){loc.y=height/2-20; vel.y*=-0.5;}
    }

    void display(){
    stroke(0);
    strokeWeight(2);
    fill(255,0,0);
    rect(loc.x,loc.y,40,20);
    }

    }

    EDIT: Fixed.

  5. Qwerty Avatar

    I kinda wonder. Wouldn't it make sense to scale the friction/air resistance according to size/mass as well ? Bigger objects – bigger resistance. If we don't then multiple objects will again fall at different rates. I'm not sure which is more correct.

  6. Loïs Avatar

    Hi Daniel and thank you so much for all these awesome tutorials. Definitely the best teaching on coding out there! Just one question, related to what is shown 15:37 i.e m.velocity.get(). How would I perform the same object property cloning in P5.js? And why don't we need to do the same with m.mass here?

  7. corey333p Avatar

    "Thus The Moonwalk"… sounds like a good band name

  8. Sreekanth Narayanaswamy Avatar

    Isnt the normal force proportional to the mass ? in that case wouldnt friction be proportional to it ?

  9. Mhmd Sadeea Avatar

    I really need to thank you … I'm impressed you are AWESOM !!

  10. galalon2 Avatar

    Just a little fix: the friction force is not always constant, if the velocity is slow enough the force of friction should not be stronger tham her, because then the ball would change direction, so you suold have used limit(c); instead of mult(c);

  11. Gil Maria Nimer Avatar

    Greetings,
    I want to thank you in advance, because these videos are perfect for building your own physics engine and in fact, it is what motivated me to program an actual Javascript physics engine so I can include it into my 2D game.
    So thank you 😀

    However, I had a question about Normal Force: If it doesn't take much from your time, could you explain what is the Normal Force please and how could I include it into my code? If it is too much to ask, then could you give me resources that could explain it to me?

    Reason being, I want to be as loyal as possible to the newton laws. Therefore, I would be very greatful if you could help me in this matter.

    Thanks

  12. Vibhav Bobde Avatar

    Can you explain all the procedural elements in the alto's adventure 🙂 Pliss. Thank you

  13. Jonathan Talisman Avatar

    Hi Dan , thank you very much for this series of videos , I am now considering sketching the moonwalk.

Leave a Reply

Your email address will not be published. Required fields are marked *