Skip to content

Commit 118429e

Browse files
committed
Add spawn/destroy controls
1 parent 32f6d40 commit 118429e

10 files changed

Lines changed: 320 additions & 28 deletions

File tree

.github/workflows/ci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ jobs:
1515
runs-on: ubuntu-latest
1616

1717
steps:
18-
- uses: actions/checkout@v4
19-
- name: Build
20-
run: cargo build --verbose
21-
- name: Run tests
22-
run: cargo test --verbose
18+
- uses: actions/checkout@v4
19+
- name: Build
20+
run: cargo build --verbose
21+
- name: Run tests
22+
run: cargo test --verbose

Cargo.lock

Lines changed: 105 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7-
macroquad = "0.4.13"
7+
macroquad = "0.4.13"
8+
rand = "0.9.0"

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
# Gravity Simulator
2+
23
A gravity simulator tech demo, made with Macroquad.
4+
35
## Building from Source
6+
47
- [Install Rust](https://www.rust-lang.org/learn/get-started)
58
- Run the following commands:
9+
610
```sh
711
git clone https://github.com/Pixels67/gravity-sim.git
812
cd gravity-sim
913
cargo build --release
1014
```
15+
1116
***Note***: To get debug lines build with `--dev`
17+
1218
## Controls
19+
1320
**[A]** / **[D]**: Left / Right \
1421
**[W]** / **[S]**: Forward / Back \
15-
**[LShift]** / **[LCtrl]**: Up / Down
22+
**[LShift]** / **[LCtrl]**: Up / Down
23+
24+
**[LMB]** To enter object creation mode or if in object creation mode go into drag mode \
25+
**[RMB]** To return to idle/default mode

src/lib.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
pub mod object;
2-
pub mod world;
32
pub mod physics;
43
pub mod screen;
4+
pub mod world;
55

66
#[cfg(test)]
77
mod tests {
8-
use macroquad::prelude::*;
98
use crate::physics::*;
9+
use macroquad::prelude::*;
1010

1111
#[test]
1212
fn physics_get_grav_force() {
13-
assert_eq!(get_grav_force(1., 1., 1., vec3(1., 0. ,0.)), vec3(1., 0., 0.));
14-
assert_eq!(get_grav_force(1., 2., 3., vec3(2., 0. ,0.)), vec3(1.5, 0., 0.));
13+
assert_eq!(
14+
get_grav_force(1., 1., 1., vec3(1., 0., 0.)),
15+
vec3(1., 0., 0.)
16+
);
17+
assert_eq!(
18+
get_grav_force(1., 2., 3., vec3(2., 0., 0.)),
19+
vec3(1.5, 0., 0.)
20+
);
1521
assert_eq!(get_grav_force(1., 1., 1., Vec3::ZERO), Vec3::ZERO);
1622
}
1723

@@ -126,4 +132,4 @@ mod tests {
126132

127133
assert!(!ray.raycast(pos, 1.));
128134
}
129-
}
135+
}

src/main.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
use macroquad::miniquad::window::*;
2-
use macroquad::prelude::*;
31
use gravity_sim::object::*;
42
use gravity_sim::world::*;
3+
use macroquad::miniquad::window::*;
4+
use macroquad::prelude::*;
55

6-
const BG_COLOR: Color = Color{r: 0.05, g: 0.05, b: 0.05, a: 1.};
6+
const BG_COLOR: Color = Color {
7+
r: 0.05,
8+
g: 0.05,
9+
b: 0.05,
10+
a: 1.,
11+
};
712
const WINDOW_WIDTH: u32 = 1280;
813
const WINDOW_HEIGHT: u32 = 720;
914
const GRAV_CONST: f32 = 0.5;
@@ -14,9 +19,27 @@ async fn main() {
1419
let mut world = World::new(GRAV_CONST, UPDATE_INTERVAL);
1520
set_window_size(WINDOW_WIDTH, WINDOW_HEIGHT);
1621

17-
world.add_object(Object::new(vec3( 5., 0., 0.), vec3(0., 0., 0.01), 1., 0.5, RED));
18-
world.add_object(Object::new(vec3( 0., 0., 0.), vec3(0., 0., 0.), 1., 0.5, GREEN));
19-
world.add_object(Object::new(vec3(-5., 0., 0.), vec3(0., 0., -0.01), 1., 0.5, BLUE));
22+
world.add_object(Object::new(
23+
vec3(5., 0., 0.),
24+
vec3(0., 0., 0.01),
25+
1.,
26+
0.5,
27+
RED,
28+
));
29+
world.add_object(Object::new(
30+
vec3(0., 0., 0.),
31+
vec3(0., 0., 0.),
32+
1.,
33+
0.5,
34+
GREEN,
35+
));
36+
world.add_object(Object::new(
37+
vec3(-5., 0., 0.),
38+
vec3(0., 0., -0.01),
39+
1.,
40+
0.5,
41+
BLUE,
42+
));
2043

2144
loop {
2245
clear_background(BG_COLOR);

src/object.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ impl ObjectPool {
128128
self.objects.pop()
129129
}
130130

131+
pub fn remove(&mut self, id: usize) {
132+
for i in 0..self.objects.len() {
133+
if self.objects[i].id != id {
134+
continue;
135+
}
136+
self.objects.swap_remove(i);
137+
return;
138+
}
139+
}
140+
131141
pub fn iter(&self) -> impl Iterator<Item = &Object> {
132142
self.objects.iter()
133143
}

src/physics.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
use macroquad::prelude::*;
22

33
pub fn get_grav_force(grav_const: f32, m1: f32, m2: f32, dist: Vec3) -> Vec3 {
4-
if dist.length_squared() == 0. { return Vec3::ZERO; }
4+
if dist.length_squared() == 0. {
5+
return Vec3::ZERO;
6+
}
57
let force = grav_const * m1 * m2 / dist.length_squared();
68
let dir = dist.normalize();
79
dir * force
810
}
911

1012
pub fn get_veloc(force: Vec3, mass: f32, time: f32) -> Vec3 {
11-
if mass == 0. || time == 0. { return Vec3::ZERO; }
13+
if mass == 0. || time == 0. {
14+
return Vec3::ZERO;
15+
}
1216
force / mass * time
1317
}
1418

1519
pub fn get_displ(force: Vec3, mass: f32, time: f32) -> Vec3 {
16-
if mass == 0. || time == 0. { return Vec3::ZERO; }
20+
if mass == 0. || time == 0. {
21+
return Vec3::ZERO;
22+
}
1723
force / mass * time * time
18-
}
24+
}

src/screen.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ impl Ray {
4141
}
4242
}
4343

44+
pub fn new_from_mouse(cam: &Camera3D, len: f32) -> Self {
45+
Self::new_from_cam(cam, get_mouse_pos(), len)
46+
}
47+
4448
pub fn raycast(&self, target_pos: Vec3, margin_of_err: f32) -> bool {
4549
if self.dir == Vec3::ZERO {
4650
return false;
@@ -61,6 +65,18 @@ impl Ray {
6165
(closest_point - target_pos).length() <= margin_of_err
6266
}
6367

68+
pub fn end(&self) -> Vec3 {
69+
self.origin + self.dir * self.len
70+
}
71+
72+
pub fn grid_intersect(&self) -> Vec3 {
73+
if self.dir.y >= 0. {
74+
return Vec3::ZERO;
75+
}
76+
77+
self.origin + (self.origin.y / self.dir.y.abs()) * self.dir
78+
}
79+
6480
#[cfg(debug_assertions)]
6581
pub fn draw(&self, color: Color) {
6682
draw_line_3d(self.origin, self.origin + self.dir * self.len, color);

0 commit comments

Comments
 (0)