1use bevy::prelude::*;
2use std::f32::consts::PI;
3
4pub fn rotate_point(point: Vec2, angle_rad: f32) -> Vec2 {
5 let cos = angle_rad.cos();
6 let sin = angle_rad.sin();
7 Vec2::new(point.x * cos - point.y * sin, point.x * sin + point.y * cos)
8}
9
10pub fn rotate_vector(vector: Vec2, angle_rad: f32) -> Vec2 {
11 rotate_point(vector, angle_rad)
12}
13
14pub fn w_poly6(r: f32, h: f32) -> f32 {
15 if r >= h {
16 return 0.0;
17 }
18 let coef = 315.0 / (64.0 * PI * h.powi(9));
19 coef * (h * h - r * r).powi(3)
20}
21
22pub fn w_spiky_gradient(r: f32, h: f32) -> f32 {
23 if r >= h {
24 return 0.0;
25 }
26 let coef = -45.0 / (PI * h.powi(6));
27 coef * (h - r).powi(2)
28}
29
30pub fn w_viscosity_laplacian(r: f32, h: f32) -> f32 {
31 if r >= h {
32 return 0.0;
33 }
34 let coef = 45.0 / (PI * h.powi(6));
35 coef * (h - r)
36}
37
38pub fn get_grid_cell(position: Vec2, grid_cell_size: f32) -> (i32, i32) {
39 (
40 (position.x / grid_cell_size).floor() as i32,
41 (position.y / grid_cell_size).floor() as i32,
42 )
43}
44
45pub fn get_neighboring_cells(cell: (i32, i32)) -> [(i32, i32); 9] {
46 [
47 (cell.0 - 1, cell.1 - 1),
48 (cell.0, cell.1 - 1),
49 (cell.0 + 1, cell.1 - 1),
50 (cell.0 - 1, cell.1),
51 (cell.0, cell.1),
52 (cell.0 + 1, cell.1),
53 (cell.0 - 1, cell.1 + 1),
54 (cell.0, cell.1 + 1),
55 (cell.0 + 1, cell.1 + 1),
56 ]
57}
58
59pub fn wind_tunnel_velocity(y: f32, freestream_velocity: Vec2, box_size: Vec2) -> Vec2 {
60 let max_speed = freestream_velocity.x;
61 let normalized_y = (y + box_size.y / 2.0) / box_size.y;
62 let velocity_x = max_speed * (1.0 - (2.0 * normalized_y - 1.0).powi(2));
63 Vec2::new(velocity_x, 0.0)
64}