1
- use bevy:: { core:: FixedTimestep , prelude:: * } ;
1
+ use bevy:: { core:: FixedTimestep , pbr :: AmbientLight , prelude:: * , render :: camera :: Camera } ;
2
2
use rand:: { thread_rng, Rng } ;
3
3
4
4
#[ derive( Debug , Hash , PartialEq , Eq , Clone , StageLabel ) ]
@@ -10,6 +10,10 @@ fn main() {
10
10
App :: new ( )
11
11
. insert_resource ( Msaa { samples : 4 } )
12
12
. add_plugins ( DefaultPlugins )
13
+ . insert_resource ( AmbientLight {
14
+ brightness : 0.03 ,
15
+ ..Default :: default ( )
16
+ } )
13
17
. add_startup_system ( generate_bodies)
14
18
. add_stage_after (
15
19
CoreStage :: Update ,
@@ -19,11 +23,12 @@ fn main() {
19
23
. with_system ( interact_bodies)
20
24
. with_system ( integrate) ,
21
25
)
26
+ . add_system ( look_at_star)
27
+ . insert_resource ( ClearColor ( Color :: BLACK ) )
22
28
. run ( ) ;
23
29
}
24
30
25
31
const GRAVITY_CONSTANT : f32 = 0.001 ;
26
- const SOFTENING : f32 = 0.01 ;
27
32
const NUM_BODIES : usize = 100 ;
28
33
29
34
#[ derive( Component , Default ) ]
@@ -32,6 +37,8 @@ struct Mass(f32);
32
37
struct Acceleration ( Vec3 ) ;
33
38
#[ derive( Component , Default ) ]
34
39
struct LastPos ( Vec3 ) ;
40
+ #[ derive( Component ) ]
41
+ struct Star ;
35
42
36
43
#[ derive( Bundle , Default ) ]
37
44
struct BodyBundle {
@@ -52,33 +59,33 @@ fn generate_bodies(
52
59
subdivisions : 3 ,
53
60
} ) ) ;
54
61
55
- let pos_range = 1.0 ..15.0 ;
56
62
let color_range = 0.5 ..1.0 ;
57
63
let vel_range = -0.5 ..0.5 ;
58
64
59
65
let mut rng = thread_rng ( ) ;
60
66
for _ in 0 ..NUM_BODIES {
61
- let mass_value_cube_root : f32 = rng. gen_range ( 0.5 .. 4.0 ) ;
62
- let mass_value: f32 = mass_value_cube_root * mass_value_cube_root * mass_value_cube_root ;
67
+ let radius : f32 = rng. gen_range ( 0.1 .. 0.7 ) ;
68
+ let mass_value = radius . powi ( 3 ) * 10. ;
63
69
64
70
let position = Vec3 :: new (
65
71
rng. gen_range ( -1.0 ..1.0 ) ,
66
72
rng. gen_range ( -1.0 ..1.0 ) ,
67
73
rng. gen_range ( -1.0 ..1.0 ) ,
68
74
)
69
75
. normalize ( )
70
- * rng. gen_range ( pos_range. clone ( ) ) ;
76
+ * rng. gen_range ( 0.2f32 ..1.0 ) . powf ( 1. / 3. )
77
+ * 15. ;
71
78
72
79
commands. spawn_bundle ( BodyBundle {
73
80
pbr : PbrBundle {
74
81
transform : Transform {
75
82
translation : position,
76
- scale : Vec3 :: splat ( mass_value_cube_root * 0.1 ) ,
83
+ scale : Vec3 :: splat ( radius ) ,
77
84
..Default :: default ( )
78
85
} ,
79
86
mesh : mesh. clone ( ) ,
80
87
material : materials. add (
81
- Color :: rgb_linear (
88
+ Color :: rgb (
82
89
rng. gen_range ( color_range. clone ( ) ) ,
83
90
rng. gen_range ( color_range. clone ( ) ) ,
84
91
rng. gen_range ( color_range. clone ( ) ) ,
@@ -101,29 +108,40 @@ fn generate_bodies(
101
108
}
102
109
103
110
// add bigger "star" body in the center
111
+ let star_radius = 1. ;
104
112
commands
105
113
. spawn_bundle ( BodyBundle {
106
114
pbr : PbrBundle {
107
- transform : Transform {
108
- scale : Vec3 :: splat ( 0.5 ) ,
109
- ..Default :: default ( )
110
- } ,
115
+ transform : Transform :: from_scale ( Vec3 :: splat ( star_radius) ) ,
111
116
mesh : meshes. add ( Mesh :: from ( shape:: Icosphere {
112
117
radius : 1.0 ,
113
118
subdivisions : 5 ,
114
119
} ) ) ,
115
- material : materials. add ( ( Color :: ORANGE_RED * 10.0 ) . into ( ) ) ,
120
+ material : materials. add ( StandardMaterial {
121
+ base_color : Color :: ORANGE_RED ,
122
+ emissive : ( Color :: ORANGE_RED * 2. ) ,
123
+ ..Default :: default ( )
124
+ } ) ,
116
125
..Default :: default ( )
117
126
} ,
118
- mass : Mass ( 1000 .0) ,
127
+ mass : Mass ( 500 .0) ,
119
128
..Default :: default ( )
120
129
} )
121
- . insert ( PointLight {
122
- color : Color :: ORANGE_RED ,
123
- ..Default :: default ( )
130
+ . insert ( Star )
131
+ . with_children ( |p| {
132
+ p. spawn_bundle ( PointLightBundle {
133
+ point_light : PointLight {
134
+ color : Color :: WHITE ,
135
+ intensity : 400.0 ,
136
+ range : 100.0 ,
137
+ radius : star_radius,
138
+ ..Default :: default ( )
139
+ } ,
140
+ ..Default :: default ( )
141
+ } ) ;
124
142
} ) ;
125
143
commands. spawn_bundle ( PerspectiveCameraBundle {
126
- transform : Transform :: from_xyz ( 0.0 , 10.5 , -20 .0) . looking_at ( Vec3 :: ZERO , Vec3 :: Y ) ,
144
+ transform : Transform :: from_xyz ( 0.0 , 10.5 , -30 .0) . looking_at ( Vec3 :: ZERO , Vec3 :: Y ) ,
127
145
..Default :: default ( )
128
146
} ) ;
129
147
}
@@ -136,7 +154,7 @@ fn interact_bodies(mut query: Query<(&Mass, &GlobalTransform, &mut Acceleration)
136
154
let delta = transform2. translation - transform1. translation ;
137
155
let distance_sq: f32 = delta. length_squared ( ) ;
138
156
139
- let f = GRAVITY_CONSTANT / ( distance_sq * ( distance_sq + SOFTENING ) . sqrt ( ) ) ;
157
+ let f = GRAVITY_CONSTANT / distance_sq;
140
158
let force_unit_mass = delta * f;
141
159
acc1. 0 += force_unit_mass * * m2;
142
160
acc2. 0 -= force_unit_mass * * m1;
@@ -156,3 +174,16 @@ fn integrate(mut query: Query<(&mut Acceleration, &mut Transform, &mut LastPos)>
156
174
transform. translation = new_pos;
157
175
}
158
176
}
177
+
178
+ fn look_at_star (
179
+ mut camera : Query < & mut Transform , ( With < Camera > , Without < Star > ) > ,
180
+ star : Query < & Transform , With < Star > > ,
181
+ ) {
182
+ let mut camera = camera. single_mut ( ) ;
183
+ let star = star. single ( ) ;
184
+ let new_rotation = camera
185
+ . looking_at ( star. translation , Vec3 :: Y )
186
+ . rotation
187
+ . lerp ( camera. rotation , 0.1 ) ;
188
+ camera. rotation = new_rotation;
189
+ }
0 commit comments