Day 9 ~ Force

in #learning7 years ago

Day 9 ~ Force

<p dir="auto">I swear I haven't been slacking on my 100 days of learning, ok, I have. I'm actually in the middle of finals and a rather large project at work so I do have some legitimate excuse's. <p dir="auto">Although I would like to get back on track and there's no better way to avoid what you are supposed to be doing that writing a blog post about force. <p dir="auto">So what is a force? <p dir="auto">Simply: <p dir="auto"><code>A vector that causes an object with mass to accelerate. <p dir="auto">All objects with mass experience force. Some common forces: gravity, tension, spring, buoyancy, air resistance. <p dir="auto">These are things we want to model within our code to create a dynamic simulation of the real world. <p dir="auto">Now that we know what a vector is ( an entity with magnitude and acceleration ). <p dir="auto">And what acceleration is ( the change in velocity over time ) we can apply these concepts to our object with mass, or at least simulate it. <p dir="auto">A quick hark back to high school for a refresher on Newtons law's of motion, a great foundation for our simulation and a ready made framework to simulate motion. <p dir="auto">So what happens in the real world according to Newtons law's? <p dir="auto">Newtons first law: <p dir="auto"><code>An object at rest stays at rest and an object in motion stays in motion <p dir="auto">The point: An objects velocity never changes unless something acts upon it. In other words a force. i.e friction, air resistance. However in a vacuum or if there is a net force of zero the object will stay in it's current state. <p dir="auto">Newton's second law: <p dir="auto"><code>Force = Mass * Acceleration <p dir="auto">This is powerful clue as to what we about to do in code. In a previous example we looked at a basic motion algorithm in our update function ( the function that updates the location of the ball in the draw loop ). <p dir="auto"><img src="https://images.hive.blog/0x0/https://steemitimages.com/DQmb66ZbsKVuobn5aQ5KzUEz5RZzy99MmSfRYS6L8GmSCmJ/acc_vel.gif" alt="acc_vel.gif" /> <p dir="auto">The <a href="https://codepen.io/harps116/pen/jZYgZw" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">code for the image. <p dir="auto">Here's the basic idea of the function: <pre><code>update() { acceleration = n; velocity.add(acceleration); location.add(velocity) } <p dir="auto">Let's take a step back for a second, We can now look at this simple function from a different perspective. <pre><code>Newtons second law: F = M * A Solving for A A = F / M Let's say M is one ( programmers can just make up world where the mass of everything is one ) A = F <p dir="auto">So we could say: <pre><code>update() { acceleration = force; <-- our cornerstone! velocity.add(acceleration); location.add(velocity) } <p dir="auto">So now we have a way to get creative, we have to calculate force. This is our key to applying different forces to our objects to create different types of behavior. <p dir="auto">Newton's third law: <p dir="auto"><code>For every action there is an equal and opposite reaction <p dir="auto">The point: forces always occur in pairs. The two forces are of equal strength but in opposite directions. <p dir="auto">This is something we don't really have to worry about this in our simulations as we are currently only working with relatively simple examples. <p dir="auto">I'm planning on doing a follow up where I actually write some code to incorporate our new perspective on motion! <p dir="auto">Happy coding! <p dir="auto">Check out Daniel Shiffman's great series on the <a href="http://natureofcode.com/book/introduction/" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Nature of Code, this 100 days of learning is a great excuse for me to finally work through the lessons and take some notes as I go.