Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- let mut app = App::new();
- app.add_plugins(DefaultPlugins).add_systems(Startup, setup);
- physiks::enable(&mut app);
- app.run();
- }
- fn setup(
- mut commands: Commands,
- mut meshes: ResMut<Assets<Mesh>>,
- mut materials: ResMut<Assets<ColorMaterial>>,
- ) {
- commands.spawn(Camera2dBundle {
- camera_2d: Camera2d {
- clear_color: ClearColorConfig::Custom(Color::BLACK),
- },
- ..default()
- });
- commands.spawn((
- RigidBody::new(1.0),
- MaterialMesh2dBundle {
- mesh: meshes.add(shape::Circle::new(50.0).into()).into(),
- material: materials.add(ColorMaterial::from(Color::PURPLE)),
- transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
- ..default()
- },
- ));
- commands.insert_resource(Time::new(Instant::now()));
- commands.insert_resource(FixedTime::new_from_secs(0.0));
- commands.insert_resource(PhysicsConfig {
- gravity: Vec3::NEG_Y * 9.8,
- });
- }
- pub fn enable(app: &mut App) {
- app.add_systems(FixedUpdate, systems::gravity::gravity)
- .add_systems(FixedUpdate, systems::movement::movement);
- }
- pub fn gravity(mut query: Query<&mut RigidBody>, config: Res<PhysicsConfig>) {
- for mut body in query.iter_mut() {
- body.velocity += config.gravity;
- }
- }
- pub fn movement(mut query: Query<(&mut Transform, &RigidBody)>, time: Res<FixedTime>) {
- for (mut transform, body) in query.iter_mut() {
- transform.translation += body.velocity * time.period.as_secs_f32();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement