package ease-caml
ease-caml Documentation
ease-caml is a game framework agnostic library for easing and tweening. A tween takes a floating point value (or collection thereof) from one value continuously over time to another value via an easing function. Tweens are a common method for animating entities in games. ease-caml provides mechanisms for easily creating and managing the tweens in your favorite OCaml game framework.
A Simple Example
We will use the raylib-ocaml game framework to demonstrate how to use ease-caml. However, any game framework with a sufficiently decent game loop should suffice.
Consider the example presented in the raylib-ocaml README:
let setup () =
Raylib.init_window 800 450 "raylib [core] example - basic window";
Raylib.set_target_fps 60
let rec loop () =
if Raylib.window_should_close () then Raylib.close_window ()
else (
let open Raylib in
begin_drawing ();
clear_background Color.raywhite;
end_drawing ();
loop ()
)
let () = setup () |> loopCurrently, a white screen is displayed. We will adapt this example to have a ball bounce on the screen.
First we create a ball just outside the screen (add this to the top of the example):
module FT = Tween.FloatTween (* To modify a single floating point value *)
type circle =
{
r: float;
x: float;
y: float ref;
}
let ball : circle = { r = 40.0; x = 400.0; y = ref ~-.40.0 }This will create a ball just outside the viewport. Note that y is a float ref since we will be making the ball fall in the y direction (and thus y must also be mutable!).
Now we will create a tween to move the ball, right below let ball : circle = ... add
let ty = FT.make_tween ball.y 225.0 ~ef:Easers.bounce 1.0ball.y is the value we will be tweening. 225.0 is the value that ball.y will ultimately become. And we ease (~ef is short for ease_function) via the provided Easers.bounce function for a duration of 1.0.
Also, we will need a tween_manager that updates all of our tweens for us. Right below add:
let tm = FT.new_manager ()and in setup (well, anywhere really!) we need to add ty to the tween manager tm with:
FT.add ty tmtm will update ty which will update y. It might seem a little silly to have tm, but you will likely have more than 1 tween in a game and will want a manager to schedule each one of them instead of manually updating each tween individually. Lastly, we need tm to update everything. Right below let open Raylib in add:
FT.update tm (get_frame_time ());Finally, we need to actually draw the ball. Right after clear_background Color.raywhite; add:
draw_circle (Int.of_float ball.x) (Int.of_float !(ball.y)) ball.r Color.maroon;All in all you should have:
module FT = Tween.FloatTween
type circle =
{
r: float;
x: float;
y: float ref;
}
let ball : circle = { r = 40.0; x = 400.0; y = ref ~-.40.0 }
let ty = FT.make_tween ball.y 225.0 ~ef:Easers.bounce 1.0
let tm = FT.new_manager ()
let setup () =
Raylib.init_window 800 450 "simple_tween";
Raylib.set_target_fps 60;
FT.add ty tm
let rec loop () =
if Raylib.window_should_close () then Raylib.close_window ()
else (
let open Raylib in
FT.update tm (get_frame_time ());
begin_drawing ();
clear_background Color.raywhite;
draw_circle (Int.of_float ball.x) (Int.of_float !(ball.y)) ball.r Color.maroon;
end_drawing ();
loop ()
)
let () = setup () |> loopRunning this code should result in a ball bouncing on the screen.
Recap
To make the ball bounce, we
- Create a
circletype. - Specify a tween to operate on the
circletype. - Add a tween manager and add the tween to the tween manager.
- Update the tween manager.
You will likely need far fewer managers than tweens so most of the time will be spent on the first two steps.
When You're Done With A Tween
Your ball has bounced, now you want to move on and resume executing something else for your game. This can be done with a callback. A callback : unit -> unit is a field for your tween that executes once the tween has finished. For instance, after the ball has bounced, we may want to finish the game. In the setup function add
FT.set_callback ty (fun () -> print_endline "Game over!")The ball will bounce, then the console will print Game over!. Alternatively, instead of FT.set_callback, you could use the binary operator $+. That is
ty $+ (fun () -> print_endline "Game over!")Sophisticated Tweens
Chaining Tweens
Suppose we want a ball that moves back and forth. Referring to the previous example, instead of
module FT = Tween.FloatTween
type circle =
{
r: float;
x: float;
y: float ref;
}
let ball : circle = { r = 40.0; x = 400.0; y = ref ~-.40.0 }
let ty = FT.make_tween ball.y 225.0 ~ef:Easers.bounce 1.0We change it to
type circle =
{
r: float;
x: float ref;
y: float;
}
let ball : circle = { r = 40.0; x = ref 200.0; y = 225.0 }
let left = FT.make_tween ball.x 600.0 ~ef:Easers.quad 1.0
let right = FT.make_tween ball.x ~sv:600.0 200.0 ~ef:Easers.quad 1.0Let's analyze whats been changed:
- We now are interested in changing the
xfield so we makex: float ref. - The start coordinates of the ball are different: The ball is now centered vertically and slightly to the left of center.
leftshould make the ball move from left to right.rightshould make the ball move from right to left.
A Shortcut: reverse
Dealing with coordinates can be tedious and sometimes we can avoid it. Changing
let right = FT.make_tween ball.x ~sv:600.0 200.0 ~ef:Easers.quad 1.0to:
let right = !!leftnearly gives us what we want. (!!) is short hand for creating a new tween that is the reverse of the original. However, this does not quite work. Reversing also reverses the ease function by default which does not happen above. Fortunately, you can just do:
let right = FT.reverse ~preserve_ease:true leftto not reverse the ease method and get what we want!
Adding the Tweens
The Wrong Way
It might be tempting to put
FT.add left tm;
FT.add right tmBut this will not work because it will run both tweens left and right at the same time for each iteration of the game loop. We need to perform left then right.
The Right Way
We now address this issue. To do so we make a new tween:
let left_right = FT.extends left rightThe FT.extends a b makes a tween that first performs a and then follows it with b. Thus, running this will cause the ball to move from left to right then right to left. (Alternatively, extends is equivalent to the binary operator $>, so you could do let left_right = left $> right.)
Rinse and Repeat
You might want to repeat this left-to-right-right-to-left animation over and over again. You could make a bunch of tweens to do this, but ease-caml provides a repeat a n function to do this automatically for you. To do so, we make yet another tween
let repeat = FT.repeat left_right 2This will repeat the left-to-right-right-to-left animation twice. By changing 2 you can change the number of times the tween repeats. Furthermore, if you want to repeat this indefinitely, you may supply a value of -1. That is,
let repeat = FT.repeat left_right ~-1Tweening a Vector
You may want to operate on a collection of floats such as a vector of floats. Technically, all you need is FloatTween and you can create a tween for each component of the vector. It would be nice to just have one tween for the entire vector. Fortunately, you can do this in ease-caml! Since there is a lot of different vector libraries, ease-caml uses functors to easily adapt to whichever vector library you want to use. We provide an example using Raylib vectors.
First, we need to make an instance of the Tween module that is specialized to Vector2.t. Whenever making an instance of the Tween module it is important to make sure you can do the addition and (left) scalar multiplication of two vectors. Thus, we write:
module Double = struct
open Raylib
type v = Raylib.Vector2.t
let ( *. ) = Fun.flip Vector2.scale (* Raylib uses right scalar multiplication.
Fun.flip converst to left scalar multiplication. *)
let ( +. ) = Vector2.add
end
module V2Tween = Tween.Make(Double)Now, to move diagonally like in the video above, we can use just one tween:
let move = V2Tween.make_tween ball.pos (Raylib.Vector2.create 600.0 300.0) ~ef:Easers.quad 1.0For reference, the entire code is:
type circle =
{
pos: Raylib.Vector2.t ref;
r: float;
}
module Double = struct
open Raylib
type v = Raylib.Vector2.t
let ( *. ) = Fun.flip Vector2.scale
let ( +. ) = Vector2.add
end
module V2Tween = Tween.Make(Double)
let ball : circle = { r = 40.0; pos = ref @@ Raylib.Vector2.create 200.0 150.0 }
let move = V2Tween.make_tween ball.pos (Raylib.Vector2.create 600.0 300.0) ~ef:Easers.quad 1.0
let tm = V2Tween.new_manager ()
let setup () =
Raylib.init_window 800 450 "simple_tween";
Raylib.set_target_fps 60;
V2Tween.add move tm
let rec loop () =
if Raylib.window_should_close () then Raylib.close_window ()
else
let open Raylib in
V2Tween.update tm (Raylib.get_frame_time ());
begin_drawing ();
clear_background Color.raywhite;
draw_circle_v !(ball.pos) ball.r Color.maroon;
end_drawing ();
loop ()
let () = setup () |> loopAPI
TweenDatatypes and functions used to make tweens. Tweens are used to continuously change one float value to another float value over time. Seeexamples/simple_tween.mlfor a simple example of how this is done in conjunction with the Raylib game library. For common continuous functions used to change the values over time, see theEasersmodule.EasersA collection of common easing functions for tweens adapted to OCaml from easings.net. A tween is a (likely continuous) function from the unit interval to itself.