fixed ball collision

This commit is contained in:
Alexandre 2024-12-23 11:52:23 +01:00
parent 2a316b4d28
commit 082144319c
5 changed files with 27 additions and 8 deletions

BIN
main.cmi

Binary file not shown.

BIN
main.cmx

Binary file not shown.

35
main.ml
View File

@ -17,7 +17,7 @@ and __height__ = 800 ;;
let __istr__ = " 1200x800" ;;
let univ_dt = 0.003 ;;
let univ_dt = 0.01 ;;
let _camx_ = ref 0
and _camy_ = ref 0 ;;
@ -76,6 +76,7 @@ type ball = {
v : pt_2d ;
a : pt_2d ;
fres : pt_2d ;
fres_0 : pt_2d ;
} ;;
(* --- *)
@ -436,8 +437,11 @@ let remove_dead_destr (destr : polygon dynamic) (cand : int list) =
in aux cand ;;
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. ;
b.fres.x <- b.fres_0.x ;
b.fres.y <- b.fres_0.y ;
b.fres_0.x <- 0. ;
b.fres_0.y <- 0. ;
let destr_remove = ref [] in
@ -573,6 +577,9 @@ let update_ball_data (b : ball) (id : int) (balls : ball array) (polys : polygon
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 ;
(* apply forces to collisionned marble *)
balls.(s).fres.x <- balls.(s).fres.x -. reaction_force_2.x *. 1.1 ;
balls.(s).fres.y <- balls.(s).fres.y -. reaction_force_2.y *. 1.1 ;
end;
(* change velocity according to angle *)
@ -583,6 +590,14 @@ let update_ball_data (b : ball) (id : int) (balls : ball array) (polys : polygon
b.v.x <- symmetric.x ;
b.v.y <- symmetric.y ;
(* apply forces to collisionned marble *)
let rc_theta = balls.(s).radius /. (vect_norm_2D (vect_diff_2D balls.(s).xy b.xy)) in
let rc_intersection = (vect_convexf balls.(s).xy b.xy rc_theta) in
let rc_director = vect_normal_2D rc_intersection (vect_sum_2D rc_intersection proj_n) in
let rc_symmetric = vect_symmetry balls.(s).v {x = 0. ; y = 0.} rc_director in
balls.(s).v.x <- rc_symmetric.x ;
balls.(s).v.y <- rc_symmetric.y ;
end
done ;
@ -885,6 +900,7 @@ let create_ball (r : float) (x0 : int) (y0 : int) (m : float) (red : int) (green
v = {x = 0. ; y = 0.} ;
a = {x = 0. ; y = 0.} ;
fres = {x = 0. ; y = 0.} ;
fres_0 = {x = 0. ; y = 0.} ;
} ;;
let create_polygon ?showScore:(shsc=true) (arr : (int * int) array) (rest : float) (pts : int) (red : int) (green : int) (blue : int) =
@ -1022,7 +1038,7 @@ let simulate (data : polygon dynamic) (destructible : polygon dynamic) (dats : s
open_graph __istr__ ;
set_window_title "WAH" ;
let pinballs = generate_pinballs 8 10.0 600 800 0.15 255 255 0 in
let pinballs = generate_pinballs 8 10.0 600 1000 0.15 255 255 0 in
let stime = Unix.gettimeofday () in
let ctime = ref (Unix.gettimeofday ()) in
@ -1080,10 +1096,13 @@ let flippers = dyn_create default_flipper ;;
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) ;;
dyn_add polygons (create_polygon [|(500, -200); (700, -200); (700, -250); (500, -250)|] 1. 0 32 32 32) ;;
dyn_add polygons (create_polygon [|(500, 800); (500, 1100); (450, 1100); (450, 800)|] 1. 0 32 32 32) ;;
dyn_add polygons (create_polygon [|(700, 800); (700, 1100); (750, 1100); (750, 800)|] 1. 0 32 32 32) ;;
dyn_add polygons (create_polygon [|(450, 1100); (750, 1100); (750, 1150); (450, 1150)|] 1. 0 32 32 32) ;;
(* TEST *)
(* dyn_add polygons (create_polygon [|(500, 800); (700, 800); (700, 850); (500, 850)|] 1. 0 32 32 32) ;; *)
(* outer walls *)
dyn_add polygons (create_polygon [|(0, -250); (500, -250); (500, 20); (0, 20)|] 1. 0 32 32 32) ;;

BIN
main.o

Binary file not shown.

BIN
pinball

Binary file not shown.