Difference between revisions of "Parabolic Shot"

From Synfig Studio :: Documentation
Jump to: navigation, search
m
m (Mathematical function to follow)
Line 6: Line 6:
 
== Mathematical function to follow ==
 
== Mathematical function to follow ==
  
We're going to simulate a cannon shot. A cannon bullet describes a parabolic trajectory after it is shot by the cannon.
+
We're going to simulate a cannon shot. A cannonball describes a parabolic trajectory after it is fired from the cannon.
  
 
[[Image:Parabolic-shot.gif]]
 
[[Image:Parabolic-shot.gif]]

Revision as of 21:23, 17 December 2007

Languages Language: 

English • español



This is a small tutorial to show how to make an object follow a path given by a mathematical function, as requested here.

Mathematical function to follow

We're going to simulate a cannon shot. A cannonball describes a parabolic trajectory after it is fired from the cannon.

Parabolic-shot.gif

The mathematical equations that govern the trajectory are:

X=V0x*t+X0
Y=-0.5*G*t²+V0y*t+Y0

where:

t = the current time for the mathematical equation
X = the 'x' coordinate of the bullet at a time 't'
Y = the 'y' coordinate of the bullet at a time 't'
X0 = the 'x' position of the bullet at time t = 0
Y0 = the 'y' position of the bullet at time t = 0
V0x = the 'x' component of the velocity when shot (t = 0)
V0y = the 'y' component of the velocity when shot (t = 0)
G = the gravity's acceleration.

You usually have the angle and the velocity modulus instead of its components. The decomposition is quite easy:

V0x = V0*cos(phi)
V0y = V0*sin(phi)

where:

V0 = the velocity when shot (t = 0)
phi = the cannon shooting angle with the horizontal.

See this Wikipedia article if you need more information regarding the maths equations of a parabolic shot.

Splitting the functions

So, how can we introduce that functions in Synfig?. To do that you should split the equations into its components. Once decomposed then you realize how many independent variables you need for the simulation. Also you will discover what are the 'engines' for the animation and what are parameters.

Lets start with the X coordinate:

X=V0x*t+X0

Applying basic maths rules you can see that the function is composed by:

  1. The addition of two terms: (V0x*t) + (X0)
  2. The first term is a multiplication of two single terms: (V0x) * (t)
  3. And the third term is a single term (X0)

So to convert those formula into a Synfig understandable way you should create the following conversion types (between parenthesis the conversion type and between brackets the exported value or the current value if not exported:

[X] (Add)
LHS [X0]
RHS [X0]
Link [V0x]
Scalar [t]
Scalar [1.0]
(Notice that the Scalar parameter from the Add convert type is not used so its value is 1.0)

So with this conversion type X becomes V0x*t+X0.

Lets continue with the Y coordinate:

Y=-0.5*G*t²+V0y*t+Y0

This one is a little more complicated because there are more terms. Anyway you should always try to group the terms in pairs and see what's the operator used. Take care on the arithmetic operators properties (commutative, associative, distributive and identity) to respect the final formulae value. We will search for the operators with the lower order of precedence (multiplication, division and modulus precede to addition and subtraction).

So there are two '+' operators what gives three terms: (-0.5*G*t²) + (V0y*t) + (Y0). Then as well as Synfig has only a two terms convert type for addition operation, we need to make the Add conversion two times.

Continuing with the analysis the individual terms are composed by multiplied terms: (-0.5) * (G) * (t²) and (V0y) * (t).

Again raises the same problem. Synfig have a conversion type called 'Scale' that gives the multiplication of a Link by an Scalar. So we need to make the Scale conversion several times for the first term (in fact three times due that t²=t*t), and one time for the second term.

So the final result is this:

Y (Add)
LHS (Scale)
Link (Scale)
Link (Scale)
Link [t]
Scalar [t]
Scalar [G]
Scalar [-0.5]
RHS (Add)
LHS (Scale)
Link [V0y]
Scalar [t]
RHS [Y0]
Scalar [1.0]
Scalar [1.0]

Now lets study the velocity composition with the velocity modulus and its shooting angle.

V0x = V0*cos(phi)
V0y = V0*sin(phi)

You can see that split this formulas is very simple once you have understood what Synfig needs:

V0x (Cos)
Angle [phi]
Amplitude [V0]

and

V0y (Sin)
Angle [phi]
Amplitude [V0]

OK I've analysed the maths functions but how do I put them into Synfig?

Well, In the example of the cannon and the bullet, I've assigned the X and Y values to the X and Y values of the centre of the circle that defines the bullet. You can assign the mathematical equations to any of the convertible parameter that exists in any layer.

So you have to go to the circle Center parameter and make right click and select 'Convert -> Composite' from the context menu. Once done you'll obtain two parameters: 'X-axis' and 'Y-axis'.

All that you need to do is make right click to the corresponding parameter that you obtain after each convert type and select the correct conversion type from the analysis you have done before. Then you will convert sequentially each sub parameter to the proper one.

Once done you should export the most simple terms to allow easy modification by the user or to allow multiple link in the same formulae. For example the 't' variable (the time for the mathematical equation) is used several times. You should export it only once. The rest of times that 't' appears you should selec the exported 't' Valuenode form the child dialog and make right click 'Connect' to connect with it.

Once done you should obtain somethign like this:

Final-Convert-Result.png

The 't' parameter

In this case the 'engine' of the equations is the variable 't' (the physic time). To make the engine parameter to 'move' in the animation we need to link it to the animation current time. To do that we are going to use the 'Linear' convert type. In fact this one is the only convert type that allow a change of the parameter with the animation time. It is really important to understand how it works because it will allow to insert 'motion' to our mathematical equations.

The 'Linear' convert type gives two parameters:

Rate: is the amount of change of the converted parameter in one second of animation time.

Offset: is an offset value to add to the result of the multiplication of the animation time by the Rate value.

So if the converted type is 't' we will obtain:

t = Rate*T+Offset

where T is the current animation time in seconds.

You should have notice that in the image of the conversion types result, the 't' parameter is also converted. It was not expanded to no complicate the sample. Here is the result:

Time-convert.png

I've exported the Offset parameter to 't1' to allow easy manipulation during the animation.

Setting up the parameters

Once done all the conversions and all the exportations you can go to the Child ValueNode list and insert the proper values for the parameters of the animation: V0, phi, G, X0, Y0, t1

The X-axis and Y-axis parameters are calculated based on the variation of the 't' parameter that is linear with the animation time T. You can also export the parameter 'Rate' form the converted type 't' if you want extra external variation of the equation motion. Test the values of the mentioned animation parameters to the desired ones. Specially some combinations of G and V0 gives good or bad values. I've set G=2.0 so my example is not in the earth :)

Producing more than one shot

You have notice that the shot animation is a little boring due to it starts at time T=0 and there is only one shot. How can you do the same than the sample? How can I use the same bullet several times making different shots with different angles, positions and initial velocities? Quite easy. Use the Offset parameter form the Linear conversion of the 't' variable.

Initially the Offset parameter is set to 0.0. That means that t=T or in other hands, the animation time is the same than the equation time. But what happen if you modify the Offset parameter during the animation?. You can 'rewind' the equation motion to the place you want just inserting the proper values of the Offset parameter at the proper animation time.

T (sec) Offset (t units) Rate (t units/sec) t = Rate*T+Offset Offset waypoint (in/out)
0.0 0.0 2.0 0.0 linear/linear
0.5 -1.0 2.0 0.0 linear/linear
4.0 -8.0 2.0 0.0 constant/linear
5.5 -11.0 2.0 0.0 linear/linear

Those are the graphical representation for the above values:

Offset curve:

Offset-curve.png

Rate curve:

Rate-curve.png

't' curve:

T-Curve.png


Notice that between T=0 and T=0.5 the Offset value has a linear variation that is inverse to the current Rate (2.0). It produces a no variation on the 't' parameter.

Also when Offset is set to constant = -1.0 (between T=0.5 and T=4.0 seconds) the 't' curve becomes linear (according to the value of Rate). During that period of time the equation is in 'action' and the parabolic shot is done with the current values of X0, Y0, V0, phi and G.

When Offset is suddenly set to -8.0 at T=4.0 it produces a 'rewind' of the equation animation to its 'zero' (t=Rate*T+Offset=2.0*4.0-8.0=0.0) so the bullet comes again to its origin.

Notice again that between T=4 and T=5.5 the Offset parameter has a linear variation between -8.0 and -11.0 (what comes from the current T value, divided by the Rate and changed sign), what gives a 't' result constant and equal to 0.0.

Again, from T=5.5 and beyond, the Offset is set to constant (there is no waypoint later so the value is kept) and the 't' value becomes linear (again there is a second shot). But in this case at T=5.5 the other parameters have been set to other different values (angle is higher and initial velocity is lower) using a constant waypoint. It allows to simulate a new shot with different parameters.

The file link and other comments

The above animation can be found also at youtube. The result sifz file for that animation is here.

I've added a fancy shooting cannon (it has a rotation angle linked to the 'phi' parameter) a motion blur on the bullet and some background.

I hope you enjoyed the tutorial. If need any help please ask it in the discuss are, at the forum or at the IRC.


Languages Language: 

English • español