Advertisement
443eb9

Untitled

Oct 15th, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.58 KB | None | 0 0
  1. fn main() {
  2.     let mut app = App::new();
  3.     app.add_plugins(DefaultPlugins).add_systems(Startup, setup);
  4.     physiks::enable(&mut app);
  5.     app.run();
  6. }
  7.  
  8. fn setup(
  9.     mut commands: Commands,
  10.     mut meshes: ResMut<Assets<Mesh>>,
  11.     mut materials: ResMut<Assets<ColorMaterial>>,
  12. ) {
  13.     commands.spawn(Camera2dBundle {
  14.         camera_2d: Camera2d {
  15.             clear_color: ClearColorConfig::Custom(Color::BLACK),
  16.         },
  17.         ..default()
  18.     });
  19.  
  20.     commands.spawn((
  21.         RigidBody::new(1.0),
  22.         MaterialMesh2dBundle {
  23.             mesh: meshes.add(shape::Circle::new(50.0).into()).into(),
  24.             material: materials.add(ColorMaterial::from(Color::PURPLE)),
  25.             transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
  26.             ..default()
  27.         },
  28.     ));
  29.  
  30.     commands.insert_resource(Time::new(Instant::now()));
  31.     commands.insert_resource(FixedTime::new_from_secs(0.0));
  32.     commands.insert_resource(PhysicsConfig {
  33.         gravity: Vec3::NEG_Y * 9.8,
  34.     });
  35. }
  36.  
  37. pub fn enable(app: &mut App) {
  38.     app.add_systems(FixedUpdate, systems::gravity::gravity)
  39.         .add_systems(FixedUpdate, systems::movement::movement);
  40. }
  41.  
  42. pub fn gravity(mut query: Query<&mut RigidBody>, config: Res<PhysicsConfig>) {
  43.     for mut body in query.iter_mut() {
  44.         body.velocity += config.gravity;
  45.     }
  46. }
  47.  
  48. pub fn movement(mut query: Query<(&mut Transform, &RigidBody)>, time: Res<FixedTime>) {
  49.     for (mut transform, body) in query.iter_mut() {
  50.         transform.translation += body.velocity * time.period.as_secs_f32();
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement