Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The lower the FPS, the higher the speed. #506

Closed
Niko-n-DEV opened this issue May 12, 2024 · 1 comment
Closed

The lower the FPS, the higher the speed. #506

Niko-n-DEV opened this issue May 12, 2024 · 1 comment

Comments

@Niko-n-DEV
Copy link

When the app is defocused, moving objects accelerate to insane speeds.

Video
https://cdn.discordapp.com/attachments/1239146961131536404/1239146963287412757/Video_2024-05-12_12-22.mp4?ex=6641dd0a&is=66408b8a&hm=82adeb09305799e09eb994e08043e06eb715b6cec33aaafcebc565c9243137ea&

#[derive(Event)]
pub struct MovementEntity(pub Entity, pub Vec3, pub f32); // 0 - Entity, 1 - Diraction, 2 - Speed

fn handle_move(
    mut query: Query<(
        &mut EntityBase, 
        &mut Transform,
        &mut Velocity
    )>,
    mut event:      EventReader<MovementEntity>,
    mut dir_event:  EventWriter<DirectionChangeEvent>,
) {
    if event.is_empty() {
        return;
    }

    for event in event.read() {
        if let Ok((mut entity_base, mut transform, mut velocity)) = query.get_mut(event.0) {
            if event.1 != Vec3::ZERO {
                let move_var = event.1 / event.1.length();
                velocity.linvel = move_var.truncate() * event.2;
                entity_base.position = Position(transform.translation);
            } else {
                transform.translation = entity_base.position.0
            }
        }
    }
}

fn inertia_attenuation(
    mut query: Query<&mut Velocity, With<EntityBase>>,
    time: Res<Time>
) {
    if query.is_empty() {
        return;
    }

    let damping_coefficient = 100.;

    for mut vel in query.iter_mut() {
        if vel.linvel != Vec2::ZERO {
            vel.linvel.x *= damping_coefficient * time.delta_seconds();
            vel.linvel.y *= damping_coefficient * time.delta_seconds();
        }
    }
}
@Niko-n-DEV
Copy link
Author

My mistake, dumping coefficient was set incorrectly and delta time is not needed there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant