Sunday, February 18, 2024

Bevy LERP

use std::f32::consts::PI;
use bevy::prelude::*;

#[derive(Component, Debug)]
pub struct MainCamera;

pub fn camera(
    mut commands: Commands
) {
    commands.spawn((
        Camera2dBundle::default(),
        MainCamera
    ));
}

#[derive(Resource, Default)]
pub struct RotAngle {
    value: f32,
}

pub fn drawbox2d(
    mut gizmos: Gizmos,
    keyboard_input: Res<ButtonInput<KeyCode>>,
    mut rotangle: ResMut<RotAngle>,
    time: Res<Time>,
) {
    //input - modify angle
    if keyboard_input.pressed(KeyCode::ArrowLeft) {
        info!("'<-' currently pressed");
        //PI / 2 == 90 degrees, PI == 180 degrees
        rotangle.value = rotangle.value.lerp(PI / 2.0, 4.0 * time.delta_seconds());
        info!("rotation: {}", rotangle.value * (180.0 / PI));
    } else {
        rotangle.value = rotangle.value.lerp(0.0, 4.0 * time.delta_seconds());
    }
    //draw
    gizmos.rect_2d(Vec2::ZERO, rotangle.value, Vec2::splat(200.0), Color::WHITE);
    gizmos.arrow_2d(Vec2::ZERO, Vec2::from_angle(rotangle.value) * 200.0, Color::RED);
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, camera)
        .add_systems(Update, drawbox2d)
        .init_resource::<RotAngle>()
        .run();
}

 


 >> press [ArrowLeft]

No comments:

Post a Comment

godot test 10