2.2: Applying a Force - The Nature of Code

Поділитися
Вставка
  • Опубліковано 2 лют 2025

КОМЕНТАРІ • 125

  • @robertsolomon3328
    @robertsolomon3328 8 років тому +97

    I swear to god you have the bestest explanations on programming in the observable universe. btw your enthusiasm and passion for programming (which is obvious) is contagious!! i'm 100% sure that's why you explain everything so good. thanks so much for everything!!!

    • @TheCodingTrain
      @TheCodingTrain  8 років тому +16

      I'm so glad to hear, thank you!

    • @ramseshendriks2445
      @ramseshendriks2445 6 років тому

      I definitely agree with you

    • @maniac50ae14
      @maniac50ae14 6 років тому +1

      NO! Thank you Danielson, thank you!

    • @magnusm4
      @magnusm4 4 роки тому

      Maybe also cause teaching helps you learn it even more. Hence when he teach you he teaches himself so that can get you even more excited.

  • @SWABsie
    @SWABsie 8 років тому +110

    Wow! 15:50 "i am such a loser..." you are not! You are just you and we love it XD I wish you could be my professor at every class! Keep up :)

    • @TheCodingTrain
      @TheCodingTrain  8 років тому +16

      Thank you!

    • @magnusm4
      @magnusm4 4 роки тому +2

      "I am such a loser"¨
      My self esteem: Allow me to introduce myself.

    • @junweili3527
      @junweili3527 2 роки тому

      I wish too❤ lol

  • @Rhua_Raj
    @Rhua_Raj 7 місяців тому

    Wow, even though this video is 8 years old, it's still incredibly helpful! I've been diving into Unity for about a year and a half now, struggling to grasp game physics, and stumbling upon this channel has been a game-changer. Thank you for creating such valuable content, and making it so visually engaging and easy to follow!

  • @RedEyedJedi
    @RedEyedJedi 7 років тому +25

    "All the forces since the dawn of time are now affecting me" = best comment ever!

  • @sebastiansoto4168
    @sebastiansoto4168 6 років тому +2

    What you were doing then and now is just getting better. I watch most of your content and I do not even have a course related to this, but I love, WE love the way you teach. You are NOT a loser and please, never regret what you do. You are truly helping way to many people and you have no idea how much we appreciate what you do. Thanks, from Costa Rica!

    • @TheCodingTrain
      @TheCodingTrain  6 років тому +1

      Thank you for the nice feedback! Costa Rica is on my dream list of places to visit for a vacation sometime soon!

  • @lima_yankee709
    @lima_yankee709 6 років тому +1

    This is better than Netflix!!! Seriously I've watched your videos for the last 3 days and my brain goes BUM because I start imagining what sort of things I could do with the stuff you've taught me so far...and there are still so many videos left haha :D

  • @eyalmeltser4296
    @eyalmeltser4296 8 років тому +22

    You make me smile and learn! keep up the good work

  • @Jdozjsnwj783
    @Jdozjsnwj783 5 років тому +1

    Thank God I found your videos. Best teacher I've ever had. Blows my college out of the water

  • @pycoton
    @pycoton 8 років тому +4

    Sir, you are a master and a poet ! Thank you so much for making the world a better place !

  • @kyungcheolkoh4517
    @kyungcheolkoh4517 6 років тому +6

    at first i was not sure if you are a funny guy. Now i love your humor!!!

  • @surajjha5542
    @surajjha5542 7 років тому +1

    I really love the way you teach sir , your lectures remind me that learning is supposed to be fun .

  • @RocketFriday
    @RocketFriday 3 роки тому +1

    Randomly found this video and wow, I love this! The subject matter (coding with physics) is awesome, your humour is 10/10 xD and your level of excitement and passion makes me feel excited about learning it! Well done!

  • @joaobrazf
    @joaobrazf 5 років тому +1

    You are not a loser, you are awesome. Realy wish I had you as a friend. Seems like u need a hud, can't do it personaly so here is a virtual hug *hugs in virtual*.

  • @Biyon.venuja
    @Biyon.venuja 2 роки тому

    woahh I've been watching your videos without attending my CS classes....you are such a great teacher Dan!! Thank you..these videos are very friendly even though you feel like your talking to a camera..sometimes I really tend to ask questions from the "video" XD..keep up the good work..I'v been watching every live stream too..I like the way you crack all those jokes and then get full serious about lesson after about a millisecond wow..

  • @muhmho1587
    @muhmho1587 8 років тому +9

    I give the thumb up
    even before watching
    u r a great man Daniel :)

  • @milestincknell8889
    @milestincknell8889 8 років тому +5

    Still continuing to be amazing.

  • @robinkohrs8097
    @robinkohrs8097 2 роки тому

    i think this is the best video i have ever seen!!💙 It's incredible how good you are in teaching...!! Thank you so much!

  • @rukna3775
    @rukna3775 6 років тому +1

    I cannot continue the tutorial cuz I have not got the base code, can u post a link where I can download the base code of the first video of this series? That would be a great help. Cheers!! :)

  • @simeon2396
    @simeon2396 8 років тому +37

    Hahaah you got me with that blowingthing you did at the end :p

  • @jeffreycordova9082
    @jeffreycordova9082 9 років тому +2

    Thanks for the great enthusiasm, I'll be checking out your other lessons pretty soon.

  • @javulicraft2228
    @javulicraft2228 6 років тому +4

    I need help!! I implemented the same exact code as you did here in the video. The only thing i changed was syntax fir it to work with p5. Im using only gravity for this example and im having a problem. As time goes by, the "moving ball" keeps going a little bit higher, to the point that it goes out of the screen. I believe the problem is frame rate. Since the program only loops x times per second, the frame in which the ball has surpassed height, the ball is lets say at height + 3. Then the checkEdges function is executing changing the velocity sign. The problem is that since the ball is at height + 3, the velocity is higher than what it would be if the ball was at height. So when that sign is changed the ball actually goes with more velicity than it should. And i believe that is whats causing the ball to bounce higher and higher. Please can anyone help me?

    • @zeldajerk
      @zeldajerk 6 років тому +6

      Make sure this is in your mover.edges function:
      if (location.x > width) {
      velocity.x = velocity.x * -1;
      location.x = width;
      } else if (location.x < 0) {
      velocity.x = velocity.x * -1;
      location.x = 0;
      }
      if (location.y > height) {
      velocity.y = velocity.y * -1;
      location.y = height;
      } else if (location.y < 0) {
      velocity.y = velocity.y * -1;
      location.y = 0;
      }

  • @AndreBlackScribe
    @AndreBlackScribe 9 років тому

    Good tutorial for newcomers. Your books are equally good. Nice Work.

  • @wael3449
    @wael3449 6 років тому

    would it not work to simply create a new "physics" vector, and then add to that all the other "force" vectors we have?
    like;
    PVector physics = new PVector(0, 0);
    PVector gravity = new PVector(0, 0.2);
    PVector wind = new PVector(0.1, 0);
    physics.add(gravity);
    physics.add(wind);
    b1.applyForce(physics);
    and why would this be bad in that case?

  • @yannini11
    @yannini11 8 років тому +8

    *Blows* 'I'm such a loser' ^^ that just made me laugh so hard:p thanks for making these amzing tutorials so entertaining!

  • @sterv4976
    @sterv4976 8 років тому +8

    What do you have for your edges method

    • @ilustrado7291
      @ilustrado7291 8 років тому +16

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

    • @nubeslocas
      @nubeslocas 7 років тому

      Thanks!

    • @varunpatkar501
      @varunpatkar501 5 років тому

      @@ilustrado7291 it should be width-r/2 where r is radius of ellipse.
      as then it won't go halfway into the wall before bouncing off

    • @JustinStazza
      @JustinStazza 4 роки тому

      @@ilustrado7291 You my friend have solved 2 days of headaches. I didn't realise that the position was reset each time and my mover would loose steam and eventually become one with the floor. You have my thanks.

  • @matthieu-g.9976
    @matthieu-g.9976 6 років тому +1

    thanks u , best teacher ever

  • @98009200
    @98009200 8 років тому

    Hi Daniel,
    Great video's! Your jibber-jabber makes it easy to follow your train of thoughts and make's it possible for me to check if i get it.
    Unfortunately i have a problem with my bouncing ball i can not solve! With every bounce it bounces less high. but the system only has 'gravity' as a constant force, there is no friction. I think its because of the 'bounce' function but i'm not sure
    (How) can i solve this problem?

  • @ericsgotlegs
    @ericsgotlegs 3 роки тому

    If the mover has a constant force pushing down on it, why is it not losing any y height on each bounce in your example? When I first wrote my code, it was losing y height. I went though it line by line and the line that made it switch to not losing any y height was in the Mover class: changing "position = new PVector(width/2,height/2); " to "position = new PVector(30,30);" I don't understand why that's the case... anyone have a clue?

  • @iobotic
    @iobotic 7 років тому

    I'm really loving that, great book and videos

  • @rahulmathew8713
    @rahulmathew8713 5 років тому +1

    Now I understand what my school Physics teacher was trying to teach me after 18 years since I left school lol

  • @sweethomes674
    @sweethomes674 Рік тому

    Fantastic and fun😊Thanks 🕊

  • @SymphonyKol_androidLover
    @SymphonyKol_androidLover 3 роки тому

    awesome awesome tutorial sir hat's off.

  • @artemlyubchenko3022
    @artemlyubchenko3022 4 роки тому +1

    May the acceleration be with you.

  • @justsomedude5108
    @justsomedude5108 5 років тому

    I've got the forces working, however the forces seem to be overriding my bounds and eventually falls through the floor. What am I missing? When my object hits the bounds i am just reversing velocity. ...bounces around a few times then falls through the floor.

    • @TheCodingTrain
      @TheCodingTrain  5 років тому

      Ah, you probably need to set the position back to the boundary so that it doesn't get stuff offscreen by accident!

    • @justsomedude5108
      @justsomedude5108 5 років тому

      @@TheCodingTrain Thanks for taking the time to respond to me. That was it! Of course right after I posted the comment I realized what I was doing wrong, then got to feel like a total goof for having to ask :-) Thank you for all the information you share.

  • @justsomedude5108
    @justsomedude5108 7 років тому

    Hi thank you for the videos. I'm having a bit of trouble wrapping my head around this and implementing it in javascript.. How is velocity not spiriling out of control? And also I understand you can change the direction when hitting the bottom or walls by multiplying by 1 or -1 but how is the circle being pulled back down after it bounces (changing direction)? And is the pVector.add() function the same as accelleration.x += force.x; accelleration.y += force.y ? Thanks again.

    • @gghelis
      @gghelis 7 років тому

      The circle is being pulled back, because the direction of the acceleration vector remains the same, but the direction of the velocity vector changes to the opposite. So when the ball hits the floor, it changes its direction and starts moving up, but the acceleration is still directed downwards, so the ball slows down until its speed reaches zero, then it falls down again and so on. That's also the reason why the velocity doesn't grow infinitely, it just goes in cycles.
      Hope that helps.

    • @justsomedude5108
      @justsomedude5108 7 років тому

      gghelis thanks buddy. Honestly It didn't make sense to me yet but later on when I get home I'll read through your comment a few more times and try to figure it out

  •  7 років тому

    I love you shiffman! hahah just you speak very quickly and I speak in spanish! hahah, thanks

  • @Lukets125
    @Lukets125 7 років тому +1

    I REALLY LOVE YOU DIANIEL! YOU ARE AWESOME! BIG EU TE AMO FROM BRAZIL

  • @santiagojaramillorodriguez8272
    @santiagojaramillorodriguez8272 3 роки тому

    Is it the same to write directly location.add(force) ?

  • @bradleystone9099
    @bradleystone9099 7 років тому

    What if you wanted to apply force to the object when it's neutral, using your mouse or a key press, and display the object's acceleration and speed?

  • @magnusm4
    @magnusm4 4 роки тому

    I've tried to write a math formula to make applied acceleration decrease the higher the velocity is.
    To make it harder to slow down once you've started going fast.
    But I can't find a good way to write this, plus having either friction (which is drag that increases with velocity basically by your videos) or drag (which is slowing down NOT based on velocity).

  • @CosteaAndrei
    @CosteaAndrei 7 років тому

    Dude, you are awesome! Much love!

  • @artemlyubchenko3022
    @artemlyubchenko3022 4 роки тому

    You could apply forces in the setup and not clear the acceleration in update.

  • @amandaniess7028
    @amandaniess7028 5 років тому

    That was very helpful. Thanks! :D

  • @shimakeemakenza4469
    @shimakeemakenza4469 6 років тому

    I'm confused, you zero out acceleration but not velocity... but wouldn't velocity also just accumulate all the acceleration added to it every update?... Also, why add acceleration to velocity then velocity to location?.. why not directly add acceleration to location then zero out acceleration?... I'm trying to follow allong but am confused to some of the things your applying...

    • @wothin
      @wothin 6 років тому +1

      Because these are just the basics. This will get more important down the road, as the types of forces and behaviors get more complex.
      EDIT: For example, if something is falling, it will accelerate the whole time it is falling until it reaches a terminal velocity, which depends on the mass of the object. Plus it important to note that at that stage he simplifies the real physics for understanding

    • @shimakeemakenza4469
      @shimakeemakenza4469 6 років тому

      I see... thank you for that... i'll continue to keep watching to see connections on topics further down the line...

  • @godofnoobs1597
    @godofnoobs1597 5 років тому

    Where do I get code of this video

  • @aZnLy96
    @aZnLy96 6 років тому

    can someone explain to me why its bouncing back down after it reverses the velocity at (location.y > height)? i'm sure its very simple but i dont get it right now.

    • @aZnLy96
      @aZnLy96 6 років тому

      i just got to the blowing, im such a loser part now. i love you lol thank you so, so much for the video series, greatly appreciated!

  • @MisterDerban
    @MisterDerban 6 років тому

    great explanation

  • @albertomedinarobredo
    @albertomedinarobredo 7 років тому

    Love this guy!

  • @VideoProfess0r
    @VideoProfess0r 7 років тому

    Every time the ball bounces against the wall, I lose 0.3 velocity...I can't figure out why....gravity force = 0.3...each time it hits the wall, i print out the velocity...15, 14.7, 14.4, 14.1...etc. until it sticks against the wall. What am I doing wrong?

    • @VideoProfess0r
      @VideoProfess0r 7 років тому

      i found your edge code in the comment (where you set the loc = width if it passes the wall)...that "almost fixed the lost velocity"...Each time the ball hits the wall, i print the velocity, this is what I get....13.5, 13.5, 13.2, 13.2, 12.9, 12.9, 12.6, 12.6, 12.3, 12.3, 12, 12, 11.7....then forever it stays at 11.7....please explain (btw, gravity is the only force being applied and it's set to 0.3...and I zero out the force just like u do...I'm wondering maybe u have the same issue, but we don't see it in the video.....?

    • @VideoProfess0r
      @VideoProfess0r 7 років тому

      ahhh, we have to add or subtract the radius from width or height when it bounces...once I did that, the velocity is 13.5 forever :)

  • @daveo1081
    @daveo1081 4 роки тому

    I’m so confused , what kind of syntax is that? “Void” ?? Where did he get that from

  • @kinotarantino1
    @kinotarantino1 8 років тому

    Hi guys, there is a moment in which the ball stops bouncing and goes under the height or width and we loose the ball - do you know any best method to avoid this? Many thanks for any reply.

    • @ShazzaBobb
      @ShazzaBobb 8 років тому +1

      set the ball equal to the height minus the radius of the ball.
      *after all the algorithm for ball, update and bounce* (preferably placed inside bounce function in constructor)
      if (y > height - radius){
      y = height - radius;
      }
      do the same for if x > width or < 0 (if applicable)

    • @kinotarantino1
      @kinotarantino1 8 років тому

      works perfectly - thank you:)

  • @thomasrinderer590
    @thomasrinderer590 3 роки тому

    The acceleration is like: "All the forces that ever happened since the dawn of time are now affecting me" which is not what we want.

  • @gghelis
    @gghelis 7 років тому +1

    Your github links don't work :(

  • @MrJloa
    @MrJloa 7 років тому

    Why u need acceleration? Remove it, change applyforce to add to velocity, in update use just velocity

  • @TriGuyBrendan
    @TriGuyBrendan 7 років тому

    So great, Thank you :D

  • @nicolasgaritagoitia5536
    @nicolasgaritagoitia5536 6 років тому

    my bouncing ball falls below the window after four or five bounces, any idea why?

    • @ArimB-cu2hp
      @ArimB-cu2hp 6 років тому

      make the x and y = the edges. example:
      this.edges = function () {
      if (this.pos.x + this.r >= width) { // right
      // make x equal the width
      this.pos.x = width;
      // change vel.x
      this.vel.x *= -1;
      } else if (this.pos.x - this.r = height) { // bottom
      // make y equal height
      this.pos.y = height;
      // change vel.y
      this.vel.y *= -1;
      } else if (this.pos.y - this.r

    • @ArimB-cu2hp
      @ArimB-cu2hp 6 років тому +2

      when the ball is close to the edge and a force applied is bigger than the distance of the ball to the edge, then the ball goes over the edge, and no code makes the ball come back. thats why you want the position of the ball to be equal to the edge if the position is over the edge.

    • @nicolasgaritagoitia5536
      @nicolasgaritagoitia5536 6 років тому

      @@ArimB-cu2hp thank you

  • @ZombieHappyBear
    @ZombieHappyBear 5 років тому

    Why is is javascript it void and not functions , is diferent from my p5.js.

    • @TheCodingTrain
      @TheCodingTrain  5 років тому

      This video uses Processing (which is built on top of the Java programming language). For more info, visit processing.org and also this video might help ua-cam.com/video/AmlAiKsiy0o/v-deo.html.

  • @ryanharris4066
    @ryanharris4066 5 років тому +1

    You are not a loser!

  • @ameychavekar5728
    @ameychavekar5728 4 роки тому

    sir am from india plz take tutorian on harwere interface with processing

  • @dimondimon535
    @dimondimon535 4 роки тому

    if someone has the problem that the ball bounces higher everytime it touches the ground so u must change the edge method like he did, and forgot maybe to say. (or its obvious and im dumb)^^

  • @robertidonotsharemyfullnam496
    @robertidonotsharemyfullnam496 4 роки тому

    15:50 nah ur not a loser, just a creative person (like me). also u'd be a great dad, cause THAT is one heck of a dad joke XD

  • @Mr.Whiskers
    @Mr.Whiskers 5 років тому

    🤯! Physics!

  • @benamende5897
    @benamende5897 6 років тому +1

    can you fix the GitHub links please :)

    • @paladin1147
      @paladin1147 6 років тому

      github.com/shiffman/The-Nature-of-Code-Examples

  • @kyoheo5326
    @kyoheo5326 8 років тому +2

    if(loc.y > height)
    {
    vel.y*=-1;
    }
    why you didnt mention this on video.. :(

    • @ilustrado7291
      @ilustrado7291 8 років тому +1

      It was already coded from the previous videos bro.

  • @ethanhermsey
    @ethanhermsey 4 роки тому +1

    actually laughed hard out loud :p 'i'm such a loser' hahahahahahaha

  • @justingolden21
    @justingolden21 5 років тому

    Not gonna lie when you blew air out of your mouth and the ball moved it was kinda cool

  • @cnuch645
    @cnuch645 6 років тому

    Sir please provide pdf notes

  • @Vinni-2K
    @Vinni-2K 5 років тому +2

    *chuckles* i'm such a loser
    hahahaha
    don't worry you're cool :D

  • @CamiloGomezDev
    @CamiloGomezDev 6 років тому

    15:50 was a bit too hilarious

  • @guirdo
    @guirdo 5 років тому

    15:47 Wooooww! Hahaha!

  • @DaniloVesovic
    @DaniloVesovic 7 років тому

    I want to like u 1.000.000 times per video

  • @santiagojaramillorodriguez8272
    @santiagojaramillorodriguez8272 3 роки тому

    The only thing I can think about with all this is to make videogames!!!

  • @greatgamegal8193
    @greatgamegal8193 6 років тому +1

    "I'm such a loser" Me too, me too

  • @HakanGuler02
    @HakanGuler02 5 років тому

    lol 'im such a loser' 15:50

  • @AlanAC22
    @AlanAC22 8 років тому

  • @techbytefrontier
    @techbytefrontier 7 років тому

    im such a loser hahahuaha

  • @OliverLonghi
    @OliverLonghi 6 років тому

    "i´m such a loser" 15:50
    No you are not, Daniel.

  • @mauriciomedina6747
    @mauriciomedina6747 8 років тому

    do you often forget to start recording? haha

    • @TheCodingTrain
      @TheCodingTrain  8 років тому +2

      +Mauricio Medina you would be surprised how often I used to do that (am better now in my more recent ones.)

    • @mauriciomedina6747
      @mauriciomedina6747 8 років тому

      +Daniel Shiffman thanks for great tutorials and book!

    • @brokenrobotgames2577
      @brokenrobotgames2577 7 років тому

      LOL, can only imagine how annoying it was to have explained things to yourself whilst thinking you were explaining it to me.

    • @blasttrash
      @blasttrash 6 років тому

      But explaining to yourself can improve your skills as well. :)

  • @tagaiismailov4235
    @tagaiismailov4235 4 роки тому

    Oh my god, you can speak Japanese. さようなら👋。

  • @satvikavyas7122
    @satvikavyas7122 3 роки тому

    how can we apply a force to the object such that as it gets closer to the edge the force applied gets stronger too?