View Full Version : Mathematics of Movement
C-Dawg
10-28-2006, 02:20 PM
It's been ages since I've been in high school, and so my trigonometry and newtonian motion mathematics are pretty rusty. I've seen some coders (Saffith, I'm looking at you here) do some pretty fancy things using trig to create smooth movement in ZScript. I was hoping anyone who does this could post here and explain the fundementals of how they use the mathematical functions of ZScript to produce the elegent results they do. A primer for the old folks like me, and the young ones who won't get to this in school for awhile.
Any takers?
Saffith
10-28-2006, 11:19 PM
Well, I'll see what I can do when I have a bit more time, but did you have anything specific in mind?
Movement at an angle, circular movement, movement along an arc... Anything else?
C-Dawg
10-29-2006, 11:35 AM
I was hoping you could explain the process you go through in determining what algorithm will best create the movement you want, and explain the mathematics behind why the algorithm you're using is a good idea.
For instance, you could start by explaining things you've already done, like circular orbits, and then maybe more complicated behavior, like movement towards Link, movement in a relative direction, figure-8s, psuedo random movement, that sort of thing.
Just a few key examples of how to mathematically construct simple movement patterns would allow the rest of us to combine them into new forms to get new movement we're looking for.
And its not like this is too helpful today, right now - the multiple FFC bug makes alot of interesting ideas unworkable (or at least untestable) for the moment.
DarkDragon
11-28-2006, 12:33 PM
This is a little late, I know, but I'll do what I can.
Let's start with very simple movement, an FFC that travels in a straight line across the screen. You probably remember straight lines being described in Algebra class as equations such as
y = x
which is the so-called implicit equation of a diagonal line of slope 1 passing through the origin. This form is implicit because it contains both the variables x and y, and whereas it is a useful description of the entire set of points lying on the line, it is not so useful for modelling FFC movement because there is no explicit dependence on time. Therefore we'll instead want to focus on parametric equations, which are the key to understanding movement along a path. Master them and pretty much all static motion of FFCs becomes easy and straightforward.
A parametric equation is defined as a pair of functions
( x(t), y(t) )
for instance, these are all parametric equations:
( t, 1 ) ( t, t^2 ) ( Sqrt(t^5+4t^2-9), cos(8t) )
The equation describes a single point (or FFC, if you will) moving through time t. For instance, if an FFC were to follow the path of the parametric equation ( t, t^2 ), at time t=0 the FFC would be at (0,0), at t=1, (1,1), at t=5, (5,25), etc.
How do you convert from an implicit equation to a parametric one? The easiest way is usually to set one of x or y to t, then solve for the other variable. For instance, for the simple example y=x, if we set x=t, we have y=t, and so the parametric equation is (t, t).
For another example, take y=x^2. If we set x=t, we have y=t^2, and the parametric equation is ( t, t^2 ). If we set y=t, we get the equivalent parametric equation ( sqrt(t), t ).
Going back to the simple diagonal line example ( t, t ), we can see how it's straightforward to convert the parametric equation into ZScript:
ffc script foo {
void main() {
for(int t=0; t<100; t++) {
this->X = t;
this->Y = t;
WaitFrame();
}
}
}
Now let's consider an important kind of path, the circle. The unit circle is described implicitly by x^2 + y^2 = 1. This gives the parametric equation
( t, sqrt(1-t^2) )
but whereas this equation will work, it has several problems, which I might go into in a future post. A better, if somewhat nonintuitive, description of the unit circle is
( sin(t), cos(t) )
which is what I'll use for the following discussion.
Now we can look at some transformations of the parametric equations.
1. To scale an axis by a constant, multiply that axis's function by that constant. For instance,
( 2sin(t), cos(t) )
describes an oval which is fat along the x axis.
( 2sin(t), 2cos(t) )
dilates both axes, yielding a bigger circle of radius 2.
2. To translate a path, just add a contant to the equations, as appropriate. For instance, to center the circle at (10,20) instead of (0,0), we just modify the equation to
( sin(t)+10, cos(t)+20 ).
EXERCISE: Write a ZScript snippet that makes an FFC move around a circle of radius 10 centered in the middle of the screen.
3. Reparametrization. The most important transformation. Whereas the others altered the LOCATIONS of the paths, reparametrization alters the TIME of the paths. To reparametrize a parametric equation by a function f(t), you replace all instances of t in the old equation by f(t); that is, you compose by f(t):
( x(f(t)), y(f(t)) ).
For instance, reparametrizing ( t, t^2 ) by f(t)=t+2 yields
( t+2, t^2+4t+4 ).
Why would you want to reparametrize?
1. To translate the path through time, by adding a constant. For instance, in our first example of the diagonal line, ( t, t ), the FFC started at the origin at t=0, and the FFC hit (4, 4) at t=4. What if we wanted the FFC to start at (4, 4) instead? We just fast-forward time and reparametrize by f(t) = t+4: ( t+4, t+4 ).
2. To make the FFC move faster or slower, mutliply by a constant. To make the FFC move twice as fast, reparametrize by f(t) = 2t: ( 2t, 2t ). To reverse the FFC's direction, multiply by -1.
3. Fancier effects. For instance, you can make an FFC accelerate of decelerate by reparametrizing nonlinearly. If f(t) is a function that increases faster and faster, the reparametrized parametric equation will accelerate through time. For instance, composing the diagonal line with f(t)=t^2 yields ( t^2, t^2 ), which will zoom off the screen. Make f(t) a function that increases slower and slower for deceleration. If f(t) oscillates, such as f(t)=sin(t), the FFC will move back and forth through time.
EXERCISE: Earlier I gave two different parametric equations for the parabola y=x^2. How are they different? Why did I say they were equivalent (hint: reparametrization)?
EXERCISE: Write a parametric equation for an FFC that spirals out from the origin.
EXERCISE: (trickier) Write a parametric equation for an FFC that oscillates smoothly between (0,0) and (50,50) along a straight line.
C-Dawg
01-05-2007, 07:09 PM
Let's say I want an FFC to move in a circle, but I want it to move more quickly. I'm not entirely sure how to do this.
sin(x), cos(x); x++;
will move very slowly.
2sin(x), 2cos(x); x++;
will just increase the radius.
sin(x) + 2, cos(x) + 2; x++;
will just move the center.
So the answer appears to be
sin(x), cos(x); x = x + y;
But this has it's own set of problems. Sin and Cos are periodic with a period of 2*pi, correct? So is there a built in "speed limit" to making some thing orbit around? That is, the following code:
sin(x), cos(x); x = x + 2*pi;
wouldn't ever move the FFC at all, would it?
So if I want the player to specify the speed of the FFC, do I need to scale it between 1 and 2*pi, perhaps?
Dark Nation
01-05-2007, 08:30 PM
Let's say I want an FFC to move in a circle, but I want it to move more quickly. I'm not entirely sure how to do this.
sin(x), cos(x); x++;
will move very slowly.
X+=2 will make it move twice as fast. Though it may be less confusing to call it angle instead of x.
C-Dawg
01-05-2007, 08:45 PM
Right, Dark Nation, but let's say I want the player to be able to define the speed using the D0-7 variables. It looks like they'll have to select a speed between 0 and about 6.28, because the circle should be periodic around 2pi.
And, come to think of it, high speed values will make movement look really wierd.
Higher movement speed around a circle, in Zquest terms, just means that the FFC gets further around the circle in a single tic. So if you have the speed set to 6.20, really close to a complete revolution around the circle, the FFC will appear to move BACKWARDS. Because each time, it will zip all the way around the circle, just barely not getting back to where it started. It's just going the "long way around."
So the actual speed limit for movement along the circle is pi, it looks like. Pi would make it move exactly halfway. Any more than that, and it's movement is indistinguishable from movement backwards.
jman2050
01-05-2007, 08:55 PM
um, the angles aren't measured in radians. They're measured in degrees. Just thought I'd let you know :P
C-Dawg
01-05-2007, 09:14 PM
So Sin(x) has a period of 360? That sounds wrong.
jman2050
01-05-2007, 09:36 PM
Yes, the scripting engine automatically converts the value passed into sin and cos from degrees to radians. So yes, the period is 360.
Saffith
01-05-2007, 10:52 PM
There are also radian versions available - RadianSin(), etc.
DarkDragon
01-08-2007, 09:54 AM
Note that the substitution x -> 2x, yielding Sin(2x), Cos(2x) also moves the FFC twice as fast.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.