Programming simulation Nature (vii): Mechanics Vector and Newton's Law

Source: Internet
Author: User

Order

Old books have a cloud: the ancients 10th and out, vegetation Gio, a day Hou Yi shooting days, the Sun nine birds are dead, save people in scourged.

It is rumored that Hou Yi used the arrows to hold the mechanical simulation system, which can realize the deep space precise guidance to shoot down the sun.

...

"The stars are hanging in the sky, just like a dream is far away ..." the Yuan whined.

"Uncle Ape, what are you humming?" ”

“... The stars disappear in the sky, just as the promise is too late to realize ... "the yuan to the nameless son to cover the ears of choice ignore.

"Cough, I am reciting the poet Xiao Gang's poetry!" ”

...

0th Chapter

"The stars hang in the sky ... Did your dad ever shoot the sun? "Yuan just want to continue affectionate recitation, seem to think of what."

"Yes, listen to the mother said Daddy is the most powerful." ”

"I don't believe it, how did you say he did it?" The yuan could not help looking up at the sky that Blue planet of water.

"With a mechanical system ..."

"Uncle Ape, the particle system in your hands is embedded!" The nameless son continues to answer

First section Random Walk class

Let's say you're standing on a single-plank bridge, tossing a coin every 10 seconds: if the coin is facing up, you step forward and back up, then step back. This is the random walk-a motion trajectory consisting of a series of random steps. Then, moving from the bridge to the ground, you can do a two-dimensional random walk, but each step needs to toss two coins, and need to follow the following rules

First Toss Second Toss Results
Positive Positive Take a step forward
Opposite Positive One step to the right.
Positive Opposite One step to the left.
Opposite Opposite Take one step backwards.

This is a simple rule, but a random walk can model a variety of phenomena in the real world: from the movement of gas molecules to the play of children throughout the day.

Now we build Walker objects, which represent an object that can walk randomly, with the following characteristics: maintaining its own data (position) and being able to perform certain actions (such as drawing itself or moving one step)

First, we define a template for the Walker class--walker object

" " <summary> " " represents a random walk object " " </summary>  Public Class Walker      Public  as Integer     Public  as Integer End Class

Constructor is responsible for initialization of the object

    " " <summary>    " "initializes the position of the current object" " </summary>    " " <param name= "INITX" >the specified object position x component</param>    " " <param name= "inity" >the specified object position y component</param>     Public Sub New(INITX as  Single, inity as  Single)        Me. X =INITXMe. Y =inityEnd Sub

Create a new Move () method to implement control object movement

    " " <summary>    " "The current object moves randomly one frame" " </summary>     Public SubRndmove ()DimChioce as Integer=Rnd. Next (4)'random values 0, 1, 2, or 3        IfChioce =0  ThenX= X +1        ElseIfChioce =1  ThenX= X-1        ElseIfChioce =2  ThenY= Y +1        ElseY= Y-1        End If    End Sub

Create a new Walker instance and loop through its rndmove () method in a process, and output to the screen after each call, and we can see its random trajectory.

Second section vector

This refers to Euclidean vectors, or geometric vectors. It is defined as: A geometric object of both size and direction. In programming, introducing vectors does not add new work to us, it simplifies your code, and the vectors provide a lot of out-of-the-box functions for the mathematical operations that often occur in motion simulations.

Now, replace the variables in the Walker class with a vector equivalent.

ImportsSystem.numerics" " <summary>" "represents a random walk object" " </summary> Public ClassWalker PublicLocation asVector2 Public Shared Rnd  as NewRandom" " <summary>    " "initializes the position of the current object" " </summary>    " " <param name= "INITX" >the specified object position x component</param>    " " <param name= "inity" >the specified object position y component</param>     Public Sub New(INITX as  Single, inity as  Single) Location=NewVector2 (INITX, inity)End Sub    " " <summary>    " "The current object moves randomly one frame" " </summary>     Public SubRndmove ()DimChioce as Integer=Rnd. Next (4)'random values 0, 1, 2, or 3        IfChioce =0  Then Location= Location +NewVector2 (1,0)        ElseIfChioce =1  Then Location= Location +NewVector2 (-1,0)        ElseIfChioce =2  Then Location= Location +NewVector2 (0,1)        Else Location= Location +NewVector2 (0, -1)        End If    End SubEnd Class

Added acceleration and speed attributes, and added Method Normalmove (),

     PublicVelocity asVector2 PublicAcceleration asVector2" " <summary>    " "Current object Physics simulation move one frame" " </summary>     Public Subnormalmove () Velocity= Velocity + acceleration'Update SpeedLocation = location + Velocity'Update Location    End Sub

It is easy to know that the relationship between the three vectors is a "trickle down" effect, and the acceleration affects the velocity, which in turn affects the position.

Create a new Walker instance, loop through its normalmove () method in a process, and record the trajectory.

The third section Force and Newton's Law

We must first understand the concept of force in the real world, Force can refer to the strength of strength, such as "she pushed the big stone vigorously", or "he forcefully say that sentence." But here is a more written concept, derived from Newton's Law of motion:

Force is a vector that causes mass objects to accelerate.

When you see the first part of the definition-the force is a vector, you should be heartily pleased with the same position or acceleration, which can be easily described in programming.

Combining the concept of force, we need to look at Newton's three Laws of motion.

Newton's first motion law is usually briefly described as:

Objects have a tendency to remain stationary or to move.

This statement omits the effect of external forces and can be extended to:

The object remains stationary or uniform motion unless there is an unbalanced force acting on it.

In programming, we can express Newton's first law:

In equilibrium, the velocity vector (vector type) of an object is always constant.

Then Skip Newton's second law, direct Newton's third law, which is usually expressed as:

Each force has an equal and opposite reaction.

This expression is misleading and can be better expressed as:

Forces are always paired, and the two forces are equal in size and opposite in direction.

Similarly, this expression is misleading because it seems to say that the forces of the pair appear to be offset against each other, and that the force of the pair appears not on the same object.

Describe Newton's third law from a programmatic perspective:

To calculate a force F(vector type) applied by a on b , an additional reaction- F acting on a by B must be applied.

Sometimes it is not necessary to follow the above, for example, when simulating wind effects, there is no need to calculate the reaction force on the air because there is no air to simulate.

Here is Newton's second law, which is expressed as:

Resultant force equals mass multiplied by acceleration:F=m*A

Now we use programming to simulate this law, combining the sum of forces and Newton's second law, adding the Applyforce () method

     Public  PropertyMass as  Single=10.0 'Mass size    " " <summary>    " "The specified force is used for the current object" " </summary>    " " <param name= "Fvec" >the specified force</param>     Public SubApplyforce (Fvec asVector2) Acceleration= acceleration + Fvec/MassEnd Sub

Update the Normalmove () method, you need to clear the acceleration

  <SUMMARY>   "  physical simulation of the current object moves a frame   </summary>  public  sub  Span style= "color: #000000;" > Normalmove () velocity  = velocity + acceleration  "  Update speed  location = position + velocity  "  update bit  acceleration = new  Vector2 (0 , 0 )   acceleration clear 0  end Sub  

Now you can create arbitrary or use a force that conforms to the physical laws of Reality and act on Walker objects.

PostScript

"Uncle Ape, what are you doing?" ”

"Drafting an instrument ..."

"I asked my mother, she did not want to say anything about Daddy." "There was some disappointment when the nameless spoke.

"Haha, say you make up, shoot day thing I will not believe!" ”

"You're the best, all right." ”

"That is of course, I am the creator god, who can be worse than me." "Yuan a pair of mighty look."

"Yo, also create God, why don't you heaven?" ”

“....”

"How do you know I'm going to heaven?" The yuan shook the hand of the manuscript.

Chinese name: Hou Yi Project

English name: Hou Yi Project

Star manned rocket Dundee project

Chief Engineer: Yuan

Engineering Stage: the demonstration

Project Objective: To realize manned Dundee

"Alas, the children of many remote places on the moon still cannot afford to learn," The nameless son tried to frown.

“...”

"Wu Gangxhu cut the tree every day, but no one paid him," he sighed.

“...”

"What's the point of Dundee such a thing?" ”

So "Yuan look at the eyes of the nameless child's face."

"You don't have to go, otherwise no one to play with me." ”

Appendix

1. Predict previously, see the last decomposition. You can also browse directory navigation .

2. Resource recommendation: Https://github.com/shiffman/The-Nature-of-Code-Examples

3. Reference book: The Nature of code [US] Daniel Shiffman

Programming simulation Nature (vii): Mechanics Vector and Newton's Law

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.