Ocaml code for 2d vectors
December 11th, 2008
Image via WikipediaFlying Frog consultancy has some great Ocaml tutorials. They also sell a book and access to Ocaml journal. When I made games in c++ I praised the fact that I used 2d vectors for all 2d stuff instead of separate x, y floats. This made code much shorter and I could do tons of simulation with basic vector operations (like acceleration’s, forces, collisions, rotations..).
One of the examples on FF Consultancy includes a very compact set of 2d vector functions and operators also. A great start to begin a dive at Ocaml game-dev experiment.
(* This is a comment (* mine *). Multiline comments can be nested. *) (* We define a new type vec2. Vec2 is a record, one of OCaml’s data structures *) type vec2 = { x: float; y: float } (* We define a constructor function for this type. { } is there because we are creating a record, they don’t wrap a function like in c like languages *) let vec2 x y = {x=x; y=y} (* This is a function that creates the zero vector, it uses previously defined function vec2. *) let zero = vec2 0. 0. (* Surprisingly to some, OCaml doesn’t have operator overloading These are operators over vec2, again { } are here because of record *) let ( +| ) a b = {x = a.x +. b.x; y = a.y +. b.y} let ( -| ) a b = {x = a.x -. b.x; y = a.y -. b.y} let ( ~| ) a = {x = -. a.x; y = -. a.y} let ( *| ) s r = {x = s *. r.x; y = s *. r.y} (* And some basic functions for vectors. *) let normal a = { x = a.y; y = -. a.x } let dot a b = a.x *. b.x +. a.y *. b.y let length2 r = dot r r let length r = sqrt(length2 r) let unitise r = 1. /. length r *| r (* example: the distance between two points *) let distance r t = length r -| t
You can see that OCaml code is very compact. OCaml has a strict static type system, but if you look at the code I declared just the 2 floats. This is because OCaml compiler figures out of what type something is!
Ocaml is known for it’s speed that comes close to c++, but it provides a much safer and higher level programming environment. OCaml is an unpure functional language which can also do imperative and object oriented programming.
It’s very interesting language, different in a lot of ways to your papa’s cup of java/python/perl/c++ . Mainly, because it’s a functional language !
—
I wrote more about OCaml in my previous posts. This post was basically one of the first ones I tried to write but didn’t finish it back then, so I am posting it now.
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=726e2efa-57cc-43e3-8f18-a7c31753a55b)
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=0ebd3411-7161-4dce-b29e-27f3f7dc7ccd)

![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=416d300b-fa77-4a15-94ba-ae86ea240eed)
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=ec552030-ea58-4328-8408-1cb5be7e13c6)
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=f233d2a2-58b1-4b1d-ac99-e4b197a7b68b)

![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=6a38954a-1798-4ab7-a667-4e3d08308921)
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=f305cef5-6e9b-470b-a1e3-3c08b8164419)
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=6bfdeaa7-d530-42ac-a3e9-c6cc204fdc8c)
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=db31df37-e100-4aba-9498-8c03ac2f95c6)
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=5495029e-2ffa-418a-a5ed-f68b851db9c5)

![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=c1171a55-89ab-414e-8c64-324161c9149b)

