fied flippers breakdancing sometimes + added ball collision (v1)

This commit is contained in:
Alexandre 2024-12-22 11:40:05 +01:00
parent 3c9f645959
commit 2a316b4d28
5 changed files with 86 additions and 35 deletions

BIN
main.cmi

Binary file not shown.

BIN
main.cmx

Binary file not shown.

121
main.ml
View File

@ -19,6 +19,9 @@ let __istr__ = " 1200x800" ;;
let univ_dt = 0.003 ;;
let _camx_ = ref 0
and _camy_ = ref 0 ;;
type pt_2d = {
mutable x : float ;
mutable y : float ;
@ -421,6 +424,9 @@ let is_collision_s (b : ball) (s : sphere) (dt : float) =
else
vect_dist_2D (step_one_ball b dt) (s.center) <= (s.radius +. b.radius) ;;
let is_collision_bl (b : ball) (ob : ball) (dt : float) =
vect_dist_2D (step_one_ball b dt) (ob.xy) <= (ob.radius +. b.radius) ;;
let remove_dead_destr (destr : polygon dynamic) (cand : int list) =
let rec aux = function
| [] -> ()
@ -429,7 +435,7 @@ let remove_dead_destr (destr : polygon dynamic) (cand : int list) =
aux t
in aux cand ;;
let update_ball_data (b : ball) (polys : polygon dynamic) (destr : polygon dynamic) (spheres : sphere dynamic) (flips : flipper dynamic) (dt : float) =
let update_ball_data (b : ball) (id : int) (balls : ball array) (polys : polygon dynamic) (destr : polygon dynamic) (spheres : sphere dynamic) (flips : flipper dynamic) (dt : float) =
b.fres.x <- 0. ;
b.fres.y <- 0. ;
@ -558,6 +564,28 @@ let update_ball_data (b : ball) (polys : polygon dynamic) (destr : polygon dynam
end
done ;
for s = 0 to Array.length balls -1 do
if s <> id && is_collision_bl b balls.(s) dt then begin
(* apply normal reaction force *)
let proj_n = vect_normalize_2D (vect_diff_2D b.xy balls.(s).xy) in
let scal = (vect_dot_product_2D (vect_normalize_2D gforce) proj_n) in
if scal > 0. then begin
let reaction_force_2 = vect_mult_2D proj_n (univ_g *. b.mass *. scal) in
b.fres.x <- b.fres.x +. reaction_force_2.x *. 1.1 ;
b.fres.y <- b.fres.y +. reaction_force_2.y *. 1.1 ;
end;
(* change velocity according to angle *)
let theta = b.radius /. (vect_norm_2D (vect_diff_2D b.xy balls.(s).xy)) in
let intersection = (vect_convexf b.xy balls.(s).xy theta) in
let director = vect_normal_2D intersection (vect_sum_2D intersection proj_n) in
let symmetric = vect_symmetry b.v {x = 0. ; y = 0.} director in
b.v.x <- symmetric.x ;
b.v.y <- symmetric.y ;
end
done ;
for f = 0 to flips.len -1 do
let (hitarr, hitlen) = (is_collision_p b flips.tab.(f).vtxs dt) in
if hitlen > 0 then begin
@ -619,7 +647,7 @@ let update_ball_data (b : ball) (polys : polygon dynamic) (destr : polygon dynam
let update_balls (bl : ball array) (polys : polygon dynamic) (destr : polygon dynamic) (spheres : sphere dynamic) (flips : flipper dynamic) (dt : float) =
for b = 0 to Array.length bl -1 do
if bl.(b).active then begin
update_ball_data bl.(b) polys destr spheres flips dt ;
update_ball_data bl.(b) b bl polys destr spheres flips dt ;
end
done ;;
@ -633,14 +661,18 @@ let update_flippers (flips : flipper dynamic) (dt : float) =
and theta0 = flips.tab.(fl).theta in
match flips.tab.(fl).side with
| Left ->
let theta_dt = flips.tab.(fl).theta +. flips.tab.(fl).dtheta *. dt in
if theta_dt > flips.tab.(fl).agmax then
let theta_dt = ref (flips.tab.(fl).theta +. flips.tab.(fl).dtheta *. dt) in
if !theta_dt > flips.tab.(fl).agmax then begin
flips.tab.(fl).dtheta <- -.(flips.tab.(fl).dtheta) ;
theta_dt := flips.tab.(fl).theta +. flips.tab.(fl).dtheta *. dt
end ;
if theta_dt < flips.tab.(fl).agmin then
if !theta_dt < flips.tab.(fl).agmin then begin
flips.tab.(fl).dtheta <- 0. ;
theta_dt := flips.tab.(fl).theta +. flips.tab.(fl).dtheta *. dt
end ;
flips.tab.(fl).theta <- theta_dt ;
flips.tab.(fl).theta <- !theta_dt ;
flips.tab.(fl).vtxs.vertexes.(0) <- {
x = x0 +. len *. (cos (theta0 *. 3.14159 /. 180.));
@ -656,14 +688,18 @@ let update_flippers (flips : flipper dynamic) (dt : float) =
};
| Right ->
let theta_dt = flips.tab.(fl).theta +. flips.tab.(fl).dtheta *. dt in
if theta_dt > flips.tab.(fl).agmax then
let theta_dt = ref (flips.tab.(fl).theta +. flips.tab.(fl).dtheta *. dt) in
if !theta_dt > flips.tab.(fl).agmax then begin
flips.tab.(fl).dtheta <- 0. ;
theta_dt := flips.tab.(fl).theta +. flips.tab.(fl).dtheta *. dt
end ;
if theta_dt < flips.tab.(fl).agmin then
if !theta_dt < flips.tab.(fl).agmin then begin
flips.tab.(fl).dtheta <- -.(flips.tab.(fl).dtheta) ;
theta_dt := flips.tab.(fl).theta +. flips.tab.(fl).dtheta *. dt
end ;
flips.tab.(fl).theta <- theta_dt ;
flips.tab.(fl).theta <- !theta_dt ;
flips.tab.(fl).vtxs.vertexes.(0) <- {
x = x0 +. len *. (cos (theta0 *. 3.14159 /. 180.));
@ -693,25 +729,25 @@ let draw_integer x0 y n0 r =
for i = 0 to size do
let x = x0 + offset - i*(len*11/7) in
if Array.mem (!n mod 10) [|0; 4; 5; 6; 7; 8; 9|] then
draw_poly_line [|(x-len/2, y+len); (x-len/2, y)|];
draw_poly_line [|(x-len/2 + !_camx_, y+len + !_camy_); (x-len/2 + !_camx_, y + !_camy_)|];
if Array.mem (!n mod 10) [|0; 2; 3; 5; 6; 7; 8; 9|] then
draw_poly_line [|(x-len/2, y+len); (x+len/2, y+len)|];
draw_poly_line [|(x-len/2 + !_camx_, y+len + !_camy_); (x+len/2 + !_camx_, y+len + !_camy_)|];
if Array.mem (!n mod 10) [|0; 1; 2; 3; 4; 7; 8; 9|] then
draw_poly_line [|(x+len/2, y+len); (x+len/2, y)|];
draw_poly_line [|(x+len/2 + !_camx_, y+len + !_camy_); (x+len/2 + !_camx_, y + !_camy_)|];
if Array.mem (!n mod 10) [|2; 3; 4; 5; 6; 8; 9|] then
draw_poly_line [|(x-len/2, y); (x+len/2, y)|];
draw_poly_line [|(x-len/2 + !_camx_, y + !_camy_); (x+len/2 + !_camx_, y + !_camy_)|];
if Array.mem (!n mod 10) [|0; 1; 3; 4; 5; 6; 7; 8; 9|] then
draw_poly_line [|(x+len/2, y-len); (x+len/2, y)|];
draw_poly_line [|(x+len/2 + !_camx_, y-len + !_camy_); (x+len/2 + !_camx_, y + !_camy_)|];
if Array.mem (!n mod 10) [|0; 2; 3; 5; 6; 8; 9|] then
draw_poly_line [|(x-len/2, y-len); (x+len/2, y-len)|];
draw_poly_line [|(x-len/2 + !_camx_, y-len + !_camy_); (x+len/2 + !_camx_, y-len + !_camy_)|];
if Array.mem (!n mod 10) [|0; 2; 6; 8|] then
draw_poly_line [|(x-len/2, y-len); (x-len/2, y)|];
draw_poly_line [|(x-len/2 + !_camx_, y-len + !_camy_); (x-len/2 + !_camx_, y + !_camy_)|];
n := !n/10;
done ;;
@ -733,25 +769,25 @@ let draw_integer_alignedleft x0 y n0 len =
for i = 0 to size do
let x = !cur_x in
if Array.mem (!n mod 10) [|0; 4; 5; 6; 7; 8; 9|] then
draw_poly_line [|(x, y+len); (x, y)|];
draw_poly_line [|(x + !_camx_, y+len + !_camy_); (x + !_camx_, y + !_camy_)|];
if Array.mem (!n mod 10) [|0; 2; 3; 5; 6; 7; 8; 9|] then
draw_poly_line [|(x, y+len); (x+len, y+len)|];
draw_poly_line [|(x + !_camx_, y+len + !_camy_); (x+len + !_camx_, y+len + !_camy_)|];
if Array.mem (!n mod 10) [|0; 1; 2; 3; 4; 7; 8; 9|] then
draw_poly_line [|(x+len, y+len); (x+len, y)|];
draw_poly_line [|(x+len + !_camx_, y+len + !_camy_); (x+len + !_camx_, y + !_camy_)|];
if Array.mem (!n mod 10) [|2; 3; 4; 5; 6; 8; 9|] then
draw_poly_line [|(x, y); (x+len, y)|];
draw_poly_line [|(x + !_camx_, y + !_camy_); (x+len + !_camx_, y + !_camy_)|];
if Array.mem (!n mod 10) [|0; 1; 3; 4; 5; 6; 7; 8; 9|] then
draw_poly_line [|(x+len, y-len); (x+len, y)|];
draw_poly_line [|(x+len + !_camx_, y-len + !_camy_); (x+len + !_camx_, y + !_camy_)|];
if Array.mem (!n mod 10) [|0; 2; 3; 5; 6; 8; 9|] then
draw_poly_line [|(x, y-len); (x+len, y-len)|];
draw_poly_line [|(x + !_camx_, y-len + !_camy_); (x+len + !_camx_, y-len + !_camy_)|];
if Array.mem (!n mod 10) [|0; 2; 6; 8|] then
draw_poly_line [|(x, y-len); (x, y)|];
draw_poly_line [|(x + !_camx_, y-len + !_camy_); (x + !_camx_, y + !_camy_)|];
n := !n/10;
cur_x := !cur_x - (len*11/7);
@ -762,7 +798,7 @@ let draw_float x y n0 r =
let ent = int_of_float n in
let frac = expand_fl (n -. float_of_int ent) in
draw_integer_alignedleft x y ent r ;
fill_circle (x + (ln10 ent) * r * 11/7 + 3*r/2) (y - r) 3 ;
fill_circle (x + (ln10 ent) * r * 11/7 + 3*r/2 + !_camx_) (y - r + !_camy_) 3 ;
draw_integer_alignedleft (x + 3*r/5 + (ln10 ent + 1)*r*11/7) y ((100 * frac) / (pw 10 (1+ ln10 frac))) r ;;
let average (arr : pt_2d array) =
@ -772,7 +808,7 @@ let average (arr : pt_2d array) =
let draw_polygon (poly : polygon) =
set_color (rgb (poly.rgb mod 256) ((poly.rgb / 256) mod 256) ((poly.rgb / (256*256)) mod 256)) ;
fill_poly (Array.init (Array.length poly.vertexes) (fun i -> (int_of_float poly.vertexes.(i).x, int_of_float poly.vertexes.(i).y))) ;
fill_poly (Array.init (Array.length poly.vertexes) (fun i -> ((int_of_float poly.vertexes.(i).x) + !_camx_, (int_of_float poly.vertexes.(i).y) + !_camy_))) ;
if poly.score <> 0 && poly.shScore then begin
set_line_width 4 ;
let (cx, cy) = average poly.vertexes in
@ -782,7 +818,7 @@ let draw_polygon (poly : polygon) =
let draw_sphere (s : sphere) =
set_color (rgb (s.rgb mod 256) ((s.rgb / 256) mod 256) ((s.rgb / (256*256)) mod 256)) ;
fill_circle (int_of_float s.center.x) (int_of_float s.center.y) (int_of_float s.radius) ;
fill_circle ((int_of_float s.center.x) + !_camx_) ((int_of_float s.center.y) + !_camy_) (int_of_float s.radius) ;
if s.score <> 0 && s.shScore then begin
set_line_width 4 ;
set_color (rgb (255 - (s.rgb mod 256)) (255 - ((s.rgb / 256) mod 256)) (255 - ((s.rgb / (256*256)) mod 256))) ;
@ -791,14 +827,14 @@ let draw_sphere (s : sphere) =
let draw_flipper (f : flipper) =
set_color (rgb 64 64 64) ;
fill_circle (int_of_float f.xy.x) (int_of_float f.xy.y) (int_of_float f.radius) ;
fill_circle ((int_of_float f.xy.x) + !_camx_) ((int_of_float f.xy.y) + !_camy_) (int_of_float f.radius) ;
draw_polygon f.vtxs ;;
let draw_ball (b : ball) =
set_color (rgb (b.rgb mod 256) ((b.rgb / 256) mod 256) ((b.rgb / (256*256)) mod 256)) ;
fill_circle (int_of_float b.xy.x) (int_of_float b.xy.y) (int_of_float b.radius) ;
fill_circle ((int_of_float b.xy.x) + !_camx_) ((int_of_float b.xy.y) + !_camy_) (int_of_float b.radius) ;
set_line_width 4 ;
draw_circle (int_of_float b.xy.x) (int_of_float b.xy.y) (int_of_float b.radius) ;;
draw_circle ((int_of_float b.xy.x) + !_camx_) ((int_of_float b.xy.y) + !_camy_) (int_of_float b.radius) ;;
let draw_all_balls (bs : ball array) =
for k = 0 to Array.length bs -1 do
@ -816,19 +852,28 @@ let get1char_plus () =
else
'@' ;;
let control_camera = function
| 'z' -> _camy_ := !_camy_ - 55
| 'q' -> _camx_ := !_camx_ + 55
| 's' -> _camy_ := !_camy_ + 55
| 'd' -> _camx_ := !_camx_ - 55
| 'n' -> failwith "Aborted.\n"
| _ -> () ;;
let control_flippers (flips : flipper dynamic) =
match get1char_plus () with
| 'q' ->
| 'w' ->
for fl = 0 to flips.len -1 do
if flips.tab.(fl).side = Left && flips.tab.(fl).dtheta = 0. then
flips.tab.(fl).dtheta <- 300. ;
done
| 'd' ->
| 'c' ->
for fl = 0 to flips.len -1 do
if flips.tab.(fl).side = Right && flips.tab.(fl).dtheta = 0. then
flips.tab.(fl).dtheta <- -. 300. ;
done
| _ -> () ;;
| ch -> control_camera ch ;;
let create_ball (r : float) (x0 : int) (y0 : int) (m : float) (red : int) (green : int) (blue : int) =
{
@ -1034,9 +1079,15 @@ let flippers = dyn_create default_flipper ;;
(* kill platform *)
dyn_add polygons (create_polygon [|(700, -20); (500, -20); (500, 1); (700, 1)|] 0. 0 255 32 32) ;;
(* upper part *)
dyn_add polygons (create_polygon [|(500, -200); (700, -200); (700, -250); (500, -250)|] 0. 0 32 32 32) ;;
dyn_add polygons (create_polygon [|(500, 800); (500, 1100); (450, 1100); (450, 800)|] 0. 0 32 32 32) ;;
dyn_add polygons (create_polygon [|(700, 800); (700, 1100); (750, 1100); (750, 800)|] 0. 0 32 32 32) ;;
dyn_add polygons (create_polygon [|(450, 1100); (750, 1100); (750, 1150); (450, 1150)|] 0. 0 32 32 32) ;;
(* outer walls *)
dyn_add polygons (create_polygon [|(0, 0); (500, 0); (500, 20); (0, 20)|] 1. 0 32 32 32) ;;
dyn_add polygons (create_polygon [|(700, 0); (1200, 0); (1200, 20); (700, 20)|] 1. 0 32 32 32) ;;
dyn_add polygons (create_polygon [|(0, -250); (500, -250); (500, 20); (0, 20)|] 1. 0 32 32 32) ;;
dyn_add polygons (create_polygon [|(700, -250); (1200, -250); (1200, 20); (700, 20)|] 1. 0 32 32 32) ;;
dyn_add polygons (create_polygon [|(0, 800); (500, 800); (500, 780); (0, 780)|] 1. 0 32 32 32) ;;
dyn_add polygons (create_polygon [|(700, 800); (1200, 800); (1200, 780); (700, 780)|] 1. 0 32 32 32) ;;
dyn_add polygons (create_polygon [|(1180, 0); (1200, 0); (1200, 800); (1180, 800)|] 1. 0 32 32 32) ;;

BIN
main.o

Binary file not shown.

BIN
pinball

Binary file not shown.