A point is not a number, it is a pair of numbers. Which means how you wrote
a,
b,
c, and
d is wrong.
Points also have slightly different multiplication and addition rules than numbers... given two points
P1 = (X1, Y1) and
P2 = (X2, Y2):
P1 + P2 = (X1, Y1) + (X2, Y2) = (X1 + X2, Y1 + Y2)
Given a number
C and a point
P = (X,Y)
C*P = C*(X,Y) = (C*X,C*Y)
In GS, you can represent a point as an array with two numbers in it. Like
{1,2} or
{3.5,10}.
So, let's investigate the problem at hand:
This is how I describe the function Beizer in terms of what it wants and what it gives back: Given 3 points and 1 number, I'll give you back a point.
So, translated into GS2...
PHP Code:
function beizer(temp.a, temp.b, temp.c, temp.d, temp.t) {
return somePointHere;
}
Unfortunately, GS isn't expressive enough to let us show that
a,
b,
c, and
d are points and
t is a number, so we'll just have to remember that they are instead.
The formula for a cubic beizer curve is this:
B(t) = (1 - t)^3 * a + (1 - t)^2 * 3 * t * b + (1 - t) * 3 * t^2 * c + t^3 * d
This almost translates directly into GS, but we need to take into account the fact that we are working with points (
a,
b,
c, and
d), and numbers (
t), instead of only numbers.
To get the coordinates of the points out of the array, for example with
a, do this:
a[0] would give you the first coordinate, and
a[1] would give you the second.
So
(1 - t)^3 * a in the original formula is actually
{(1 -t)^3 * a[0], (1 - t)^3 * a[1]} in GS2.
Thankfully, GS already has functions which do these operations for us:
vectorscale(point, number) and
vectoradd(point, point).
So
(1 - t)^3 * a in the original formula can be written as
vectorscale(a, (1-t)^3).
Now it is just a matter of putting this all together...
PHP Code:
function beizer(temp.a, temp.b, temp.c, temp.d, temp.t) {
temp.part1 = vectorscale(temp.a, (1 - temp.t)^3);
temp.part2 = vectorscale(temp.b, (1 - temp.t)^2 * 3 * temp.t);
temp.part3 = vectorscale(temp.c, (1 - temp.t) * 3 * temp.t^2);
temp.part4 = vectorscale(temp.d, temp.t^3);
return vectoradd(temp.part1, vectoradd(temp.part2, vectoradd(temp.part3, temp.part4)));
}
You call it like this:
PHP Code:
function onWeaponFired() {
player.chat = this.beizer({1,2,0}, {2,3,0}, {4,5,0}, {5,6,0}, 0.5);
}
Note: the vector functions require 3D points, but it doesn't matter since you just make the third coordinate 0. However, this has the nice benefit of working in three dimensions if you need it.
The alternative to the vector functions is to write out the formula twice for each coordinate of the point, but that means you'll have duplicate code which gets annoying to maintain.
Edit: Okay this post took me forever to write, fowlplay beat me to it . Hopefully this is still useful since it shows another way of writing the code.