Now that we have implemented motion with velocity and acceleration, let’s try something a bit more complex and a great deal more useful. We’ll dynamically calculate a ball’s acceleration according to the following condition:[br][br][b]Acceleration towards a given point.[/b][br][br]Anytime we want to calculate a vector based on a rule or a formula, we need to compute two things: [b][i]magnitude[/i][/b] and [b][i]direction[/i][/b]. Let’s start with direction. We know the acceleration vector should point from the ball’s location towards the point's location. Let’s say the ball is located at the point (x,y) and the point at (x', y').[br][br]In the following Figure we see that we can get a vector (dx,dy) by subtracting the ball’s location from the point’s location.
If the ball were to actually accelerate using that vector, it would appear instantaneously at the point's location. This does not make for good animation, of course, and what we want to do now is decide how quickly that ball should accelerate toward that given point.[br][br]In order to set the magnitude (whatever it may be) of our acceleration, we must first normalize that direction vector. That’s right, [i]normalize[/i]. If we can shrink the vector down to its unit vector (of length one) then we have a vector that tells us the direction and can easily be scaled to any value.[br][br]To summarize, we take the following steps:[list=1][*]Calculate a vector that points from the object to the target location (the given point)[/*][*]Normalize that vector (reducing its length to 1)[/*][*]Scale that vector to an appropriate value (by multiplying it by some value)[/*][*]Assign that vector to acceleration[/*][/list]
Thus the script in the Setup button is:[br][br][code]##My circle or ball[br]xloc=5[br]yloc=5[br]SetValue(xloc, 5)[br]SetValue(yloc, 5)[br][br]O=(xloc, yloc)[br][br]c=Circle(O, 1)[br][br]##Velocity[br]xvel=Slider(-0.3, 0.3, 0.001, 1, 140, false, true, false, false)[br]yvel=Slider(-0.3, 0.3, 0.001, 1, 140, false, true, false, false)[br]SetValue(xvel, RandomUniform(-0.3, 0.3))[br]SetValue(yvel, RandomUniform(-0.3, 0.3))[br][br]##Attractor[br]Ax=Slider(-15,15, 0.001, 1, 140, false, true, false, false)[br]Ay=Slider(-10,10, 0.001, 1, 140, false, true, false, false)[br][br]A=(Ax, Ay)[br][br]#direction[br]dx=Ax-xloc[br]dy=Ay-yloc[br]norm=sqrt(dx^2+dy^2)[br]scale=0.06[br][br]#Normalize and scale[br]dxn=(Ax-xloc)/norm*scale[br]dyn=(Ay-yloc)/norm*scale[br][br]##Acceleration[br]xacc=dxn[br]yacc=dyn[br][br]Run=Slider(-5, 5, 0.01, 1, 140, false, true, false, false)[br][br]Box=Polygon((-15, -10), (15,-10), (15, 10), (-15, 10))[/code][br][br]Finally, the slider Run should have the following script:[br][br]#Update velocity[br][code][/code]SetValue(xvel, xvel + xacc)[br]SetValue(yvel, yvel + yacc)[br][br]#Update location[br]SetValue(xloc, xloc+xvel)[br]SetValue(yloc, yloc+yvel)[br][br]The following applet shows this implementation.
You may be wondering why the circle doesn’t stop when it reaches the target. It’s important to note that the object moving has no knowledge about trying to stop at a destination; it only knows where the destination is and tries to go there as quickly as possible. Going as quickly as possible means it will inevitably overshoot the location and have to turn around, again going as quickly as possible towards the destination, overshooting it again, and so on and so forth.[br][br]This example is remarkably close to the concept of gravitational attraction (in which the object is attracted to the given point). However, one thing missing here is that the strength of gravity (magnitude of acceleration) is inversely proportional to distance. This means that the closer the object is to the attractor, the faster it accelerates.