# GMLA free simple quadratic bezier curve script in GML

#### andev

##### Member
Put in 4 x/y coordinates and a lerp value to find the point. I couldn't find a script that did only this out the box in GML. Here's an example of implementation:  GM Version: Any
Target Platform: ALL

The GML you're here for:
Code:
``````///Bezier_Point_Find(lerp, p0x, p0y, p1x, p1y, p2x, p2y, p3x, p3y)
/*
Lerp is a value from 0-1 to find on the line between p0 and p3
p1 and p2 are the control points

returns array (x,y)
*/
var t   = argument0;
var p0x = argument1;
var p0y = argument2;
var p1x = argument3;
var p1y = argument4;
var p2x = argument5;
var p2y = argument6;
var p3x = argument7;
var p3y = argument8;

//Precalculated power math
var tt  = t  * t;
var ttt = tt * t;
var u   = 1  - t; //Inverted
var uu  = u  * u;
var uuu = uu * u;

//Calculate the point
var px =     uuu * p0x; //first term
var py =     uuu * p0y;
px += 3 * uu * t * p1x; //second term
py += 3 * uu * t * p1y;
px += 3 * u * tt * p2x; //third term
py += 3 * u * tt * p2y;
px +=        ttt * p3x; //fourth term
py +=        ttt * p3y;

//Pack into an array
var PA; PA = px; PA = py;
return PA;``````

The original C#:
It comes from a well written tutorial, which you can find here.
GML doesn't define variable types, and also doesn't support vectors.
Code:
``````Vector3 CalculateBezierPoint(float t,
Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
{
float u = 1 – t;
float tt = t*t;
float uu = u*u;
float uuu = uu * u;
float ttt = tt * t;

Vector3 p = uuu * p0; //first term
p += 3 * uu * t * p1; //second term
p += 3 * u * tt * p2; //third term
p += ttt * p3; //fourth term

return p;
}``````

• • Solitary Confinement, hippyman, rIKmAN and 16 others

#### NeZvers

##### Member
Up you go. More need to know this awesomeness!

• andev and Lonewolff
L

#### Lonewolff

##### Guest
Up you go. More need to know this awesomeness!
Agreed! I hadn't even seen this post before. Very nice!

• NeZvers

#### SaltySamurai

##### Member
I may need an example of it in use I can tinker with, how would I use this just to draw a line to the screen?

#### SaltySamurai

##### Member
I just figured it out, ok, now I know what it's doing at least! XD

#### D'Andrëw Box

##### Member
• • #### andev

##### Member
Thank you for this! I don't really understand very well the math behind it, but it worked perfectly for what i needed to!
(I wanted to do this comment before, but i haven't an account on the forums)

View attachment 38421
That's looking very nice! What do the box colors mean?

#### D'Andrëw Box

##### Member
That's looking very nice! What do the box colors mean?
Nodes! It's a node-based gameflow designer for visual novels and text adventures!

##### Member
Thank you for this! I don't really understand very well the math behind it, but it worked perfectly for what i needed to!
(I wanted to do this comment before, but i haven't an account on the forums)

View attachment 38421
nice!!!  can you send me that script bezier line,please...

Last edited:

#### D'Andrëw Box

##### Member
nice!!!  can you send me that script bezier line,please...
i modified a lot the script since that, but here it's if you need it!

GML:
``````/// @function    draw_bezier(x1, y1, x2, y2, x3, y3)
/// @param x1 {Real} The X coordinate of the first control point.
/// @param y1 {Real} The Y coordinate of the first control point.
/// @param x2 {Real} The X coordinate of the second control point.
/// @param y2 {Real} The Y coordinate of the second control point.
/// @param x3 {Real} The X coordinate of the third control point.
/// @param y3 {Real} The Y coordinate of the third control point.
/// @param x4 {Real} The X coordinate of the third control point.
/// @param y4 {Real} The Y coordinate of the third control point.
function draw_bezier(x1, y1, x2, y2, x3, y3, x4, y4) {
var _step = 0.05;
draw_primitive_begin(pr_linestrip);
draw_vertex(x1, y1);
for (var i = 0; i <= 1; i += _step) {
// get intermediate coordinates
var ix = lerp(x1, x2, i);
var iy = lerp(y1, y2, i);
var jx = lerp(x2, x3, i);
var jy = lerp(y2, y3, i);
var kx = lerp(x3, x4, i);
var ky = lerp(y3, y4, i);

// get further intermediate coordinates
var iix = lerp(ix, jx, i);
var iiy = lerp(iy, jy, i);
var jjx = lerp(jx, kx, i);
var jjy = lerp(jy, ky, i);

// get final curve point
var bx = lerp(iix, jjx, i);
var by = lerp(iiy, jjy, i);
draw_vertex(bx, by);
}
draw_vertex(x4, y4);
draw_primitive_end();
}``````