An analysis of the "elasticity of Physics" tutorial on the Blue Ideal Classic forum is more clear ...
First of all, to understand the elastic effect, we see it here, this force to drag objects to the destination, and when the object and destination farther, the greater the force, like a spring hanging an object!
Now, start writing code step by step.
First of all, casually create a MC, such as drawing a small ball, put it on the left side of the stage, the MC write the following code:
Onclipevent (load) { vx=1} onclipevent (enterframe) { _x+=vx;}
VX represents the velocity along the x-axis, _x+=vx equals _X=_X+VX. We add the X coordinates to the speed of each frame in the MC. To test the animation, we can see the ball moving uniformly. What if it's made to be variable speed? By the way, we can change VX, let VX shift by frame, giving it an acceleration:
Onclipevent (load) { vx=1; ax=.1;} Onclipevent (enterframe) { vx+=ax; _X+=VX;}
To test the animation, we see the ball gradually accelerating. If you turn VX into 5,ax-0.1, the ball slows down first and then accelerates backwards.
Our aim should be to accelerate the ball to the right, then decelerate and then reverse, then cycle, as if the center of a fixed-point vibration.
Next, we set up a center, using TX to represent the x-coordinate of the center, then the distance between TX and _x, and the acceleration ax is proportional to the distance, i.e. the nearer the distance, the smaller the acceleration.
Onclipevent (load) { tx=250} Onclipevent (enterframe) { //ax is a fraction of the distance from TX to _x ax= (tx-_x) *.2; Vx+=ax; _X+=VX;}
Now, we see the ball bouncing back and forth, the actual situation should be like a loss of energy, the amplitude of the ball should be gradually reduced. Now using K instead of 0.2 in the upper, it is like the stiffness coefficient of a spring; the amplitude of each amplitude decrease is expressed by damp=0.9:
Onclipevent (load) { tx=250; k=.2; damp=.9;} Onclipevent (enterframe) { //ax is a fraction of the distance from TX to _x ax= (tx-_x) *k; Vx+=ax; Vx*=damp; _X+=VX;}
Now you can see the real elastic effect, you can change the parameters damp and k, and in addition, do the same for y coordinates. The code is as follows:
Onclipevent (load) { tx=250; ty=250; k=.2; damp=.9;} Onclipevent (enterframe) { ax= (tx-_x) *k; Ay= (ty-_y) *k; Vx+=ax; Vy+=ay; Vx*=damp; Vy*=damp; _X+=VX; _y+=vy;}
We also allow the ball to be dragged as follows:
1. Double-click the ball into the editing state;
2. Select the whole ball and turn it into a button;
3. Write code on the button:
On (press) { StartDrag (""); Drag=true;} On (release, releaseoutside) { stopdrag (); Drag=false;}
4. Back to the MC code, the change is as follows:
Onclipevent (enterframe) { if (!drag) { ax= (tx-_x) *k; Ay= (ty-_y) *k; Vx+=ax; Vy+=ay; Vx*=damp; Vy*=damp; _X+=VX; _y+=vy }}
Up to now, we use a fixed-point (250,250), we can also change him to the coordinates of the mouse, with _root._xmouse, etc. to express:
Onclipevent (load) { k=.2; damp=.9;} Onclipevent (enterframe) { ax= (_root._xmouse-_x) *k; ay= (_root._ymouse-_y) *k; Vx+=ax; Vy+=ay; Vx*=damp; Vy*=damp; _X+=VX; _y+=vy; }
This concludes the tutorial.
From classic: Aero-lk
The purpose of this tutorial is to produce a number of conditions under which the action script provides a practical knowledge of the effect of the motion scripts. That is, the knowledge of elasticity mathematics calculation. These mathematical knowledge is based on Hooke's law, from the rapid movement and swing to the actual elastic movement, and it is found in almost every book on physics.
Overview:
Elastic animation is very attractive, it can give the object response, action on the spiritual. The crux of the problem is to have a practical understanding of the mathematical principles. You must be familiar with the following three basic terms:
Position (P): Position (P)
Velocity (v): Speed (v)
Acceleration (a): Acceleration (a)
The above three concepts are applied in elastic animation. You may be familiar with the fact that a spring reacts to an external force (such as stretching), and the larger the force on it, the greater the reaction of the spring. This fact is the basis of elastic animation, which is called Hooke's law.
The spring reaction is determined by the constant called the "elasticity coefficient (k)". Further, in a frictionless environment, the spring is forever stretching and swinging, as long as it is pulled by external forces. So we're going to introduce a "block constant (c)" To limit the movement to a slow stop. So here's the math formula we're going to use:
-k* (P-PO) =a //An element whose acceleration is proportional to the displacement of the spring c*v=a //An element whose acceleration and velocity have been limited by the action
So
a=-k* (P-PO) +c*v
In the formula above:
A PO is an initial condition, i.e. the initial position, initial ratio, or initial size of the object on the screen.
K is the elasticity coefficient
c is the blocking constant
In this equation, you will notice that both will produce an acceleration, which will change the speed of the object, just as the velocity can change the object's displacement. So the principle analysis is as follows: When we change an object from its initial state to another State, an acceleration is produced, which in turn produces a velocity that changes the object's displacement so that it can return to its original state. However, when it returns to its original position, there is no force to stop it, so it will continue to move (too far). Once again, the spring produces an acceleration that allows the object to return to its original position, so that if there is no obstruction, the object will oscillate or move around in its initial position. The initial state can be its position on the screen, size, scale, transparency, and so on.
The formulas for displacement state, velocity and acceleration are:
a=-k* (p.current-p.initial)//That is a=-k* (current state-initial state) v.current=c*v.last+a//that is, current speed =c* previous speed +p.current=p.last+v.current// Current state = Previous State + current speed
The method of applying this action to MC is:
Onclipevent (enterframe) { dx = This._xscale-xo;//Initialize elastic deformation or oscillation in x-axis (n = this._yscale-yo;//Initialize elastic deformation or oscillation in y-coordinate) ax =-_ROOT.SPRING*DX//generate the acceleration ay =-_root.spring*dy in x-axis coordinates;// generate acceleration under y-coordinate VX = Vx*_root.damp+ax; Generate x-axis velocity vy = vy*_root.damp+ay;//generate velocity in y-coordinate this._xscale + = VX;//Move the object to its initialized x-axis coordinates this._yscale = VY; Moves the y-coordinate of an object to its initialization
An elastic value can be set to its initial state in the following ways:
Onclipevent (load) { XO = 100;