# Playing with Phaser Tweens and Bezier curves

Are you tired to create straight paths with your Phaser Tweens? Do you want the enemies in your game to follow a more unpredictable path? Have a try with Bezier curves. A Bezier curve is a mathematically defined curve used in two-dimensional graphic applications. The curve is defined by four points: the initial position and the terminating position (which are called “anchors”) and two separate middle points (which are called “handles”). The shape of a Bezier curve can be altered by moving the handles. The mathematical method for drawing curves was created by Pierre Bézier in the late 1960’s for the manufacturing of automobiles at Renault. End of the boring theory, play with this prototype:
Drag any of the green or red points to change the yellow path and see the white circle following it. Phaser natively supports Bezier interpolation in tweens, although the ball seems to “cut” some curves. Have a look at the source code:
```var game;
var pointsArray = [];
var pointColors = ["0x00ff00", "0x008800", "0x880000", "0xff0000"];
var bezierGraphics;
var movingSprite;

game = new Phaser.Game(800, 500);
game.state.start("PlayGame");
}

var playGame = function(game){}
playGame.prototype = {
},
create: function(){
for(var i = 0; i < 4; i++){
var draggablePoint = game.add.sprite(game.rnd.between(100, game.width - 100), game.rnd.between(100, game.height - 100), "point");
draggablePoint.inputEnabled = true;
draggablePoint.tint = pointColors[i];
draggablePoint.input.enableDrag();
draggablePoint.anchor.set(0.5);
pointsArray[i] = draggablePoint;
}
updateDrag();
stopDrag();
}
}

function startDrag(){
movingSprite.destroy();
}

function stopDrag(){
movingSprite.scale.set(0.5);
movingSprite.anchor.set(0.5);
x: [pointsArray.x, pointsArray.x, pointsArray.x, pointsArray.x],
y: [pointsArray.y, pointsArray.y, pointsArray.y, pointsArray.y],
}, 5000,Phaser.Easing.Quadratic.InOut, true, 0, -1).interpolation(function(v, k){
return Phaser.Math.bezierInterpolation(v, k);
});
}

function updateDrag(){
bezierGraphics.clear();
bezierGraphics.lineStyle(2, 0x008800, 1);
bezierGraphics.moveTo(pointsArray.x, pointsArray.y);
bezierGraphics.lineTo(pointsArray.x, pointsArray.y);
bezierGraphics.lineStyle(2, 0x880000, 1)
bezierGraphics.moveTo(pointsArray.x, pointsArray.y);
bezierGraphics.lineTo(pointsArray.x, pointsArray.y);
bezierGraphics.lineStyle(4, 0xffff00, 1);
bezierGraphics.moveTo(pointsArray.x, pointsArray.y);
for (var i=0; i<1; i+=0.01){
var p = bezierPoint(pointsArray, pointsArray, pointsArray, pointsArray, i);
bezierGraphics.lineTo(p.x, p.y);
}
}

function bezierPoint(p0, p1, p2, p3, t){
var cX = 3 * (p1.x - p0.x);
var bX = 3 * (p2.x - p1.x) - cX;
var aX = p3.x - p0.x - cX - bX;
var cY = 3 * (p1.y - p0.y);
var bY = 3 * (p2.y - p1.y) - cY;
var aY = p3.y - p0.y - cY - bY;
var x = (aX * Math.pow(t, 3)) + (bX * Math.pow(t, 2)) + (cX * t) + p0.x;
var y = (aY * Math.pow(t, 3)) + (bY * Math.pow(t, 2)) + (cY * t) + p0.y;
return {x: x, y: y};
}
```
Next time I am showing you a real world example of a game based on Bezier interpolation, meanwhile you can download the source code.
214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 10000000
// 2 Cars
// 2048
// Avoider
// Ballz
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Breakout
// Bricks
// Columns
// CubesOut
// Dots
// DROP'd
// Dudeski
// Eskiv
// Filler
// Fling
// Globe
// HookPod
// Hundreds
// InkTd
// Iromeku
// Lumines
// Magick
// MagOrMin
// Maze
// Memdot
// Nano War
// Nodes
// o:anquan
// Ononmin
// Pacco
// Phyballs
// Platform
// Poker
// Pool
// Poux
// Pudi
// qomp
// Racing
// Renju
// SameGame
// Security
// Sling
// Slingy
// Sokoban
// Splitter
// Sproing
// Stack
// Stringy
// Sudoku
// Tetris
// Threes
// Toony
// Turn
// TwinSpin
// vvvvvv
// Wordle
// Worms
// Yanga
// Zhed
// zNumbers