-
Notifications
You must be signed in to change notification settings - Fork 2
/
tutorial_08_physics.ml
87 lines (69 loc) · 2.43 KB
/
tutorial_08_physics.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
(* Adaptation of the physics tutorial from Orx *)
(* This example is a direct adaptation of the 08_Physics.c tutorial from Orx *)
module State = struct
type t = Orx.Camera.t
let state : t option ref = ref None
let get () = Option.get !state
end
let event_handler
(event : Orx.Event.t)
(physics : Orx.Physics_event.t)
(_payload : Orx.Physics_event.payload) =
( match physics with
| Contact_remove -> ()
| Contact_add ->
let sender = Orx.Event.get_sender_object event |> Option.get in
let recipient = Orx.Event.get_recipient_object event |> Option.get in
Orx.Object.add_fx sender "Bump" |> ignore;
Orx.Object.add_fx recipient "Bump" |> ignore
);
Ok ()
let update (clock_info : Orx.Clock.Info.t) =
let camera = State.get () in
let delta_rotation =
if Orx.Input.is_active "RotateLeft" then
Some (4.0 *. Orx.Clock.Info.get_dt clock_info)
else if Orx.Input.is_active "RotateRight" then
Some (-4.0 *. Orx.Clock.Info.get_dt clock_info)
else
None
in
match delta_rotation with
| None -> ()
| Some delta ->
(* Rotate the camera *)
let current_rotation = Orx.Camera.get_rotation camera in
Orx.Camera.set_rotation camera (current_rotation +. delta);
(* Rotate gravity *)
let gravity = Orx.Vector.rotate_2d (Orx.Physics.get_gravity ()) delta in
Orx.Physics.set_gravity gravity
let init () =
let (type_, id, mode) =
Orx.Input.get_binding "RotateLeft" 0 |> Result.get_ok
in
let input_rotate_left = Orx.Input.get_binding_name type_ id mode in
let (type_, id, mode) =
Orx.Input.get_binding "RotateRight" 0 |> Result.get_ok
in
let input_rotate_right = Orx.Input.get_binding_name type_ id mode in
Orx.Log.log
("@.- '%s' & '%s' will rotate the camera@."
^^ "* Gravity will follow the camera@."
^^ "* a bump visual FX is played on objects that collide"
)
input_rotate_left input_rotate_right;
let viewport = Orx.Viewport.create_from_config_exn "Viewport" in
let camera = Orx.Viewport.get_camera viewport |> Option.get in
let clock = Orx.Clock.get_core () in
Orx.Clock.register clock update;
State.state := Some camera;
Orx.Event.add_handler Physics event_handler;
let (_scene : Orx.Object.t) = Orx.Object.create_from_config_exn "Scene" in
Ok ()
let run () =
if Orx.Input.is_active "Quit" then
Orx.Status.error
else
Orx.Status.ok
let () =
Orx.Main.start ~config_dir:"examples/tutorial/data" ~init ~run "08_Physics"