The-DevBlog / bevy_third_person_camera

A third person camera crate written for Bevy.
Apache License 2.0
78 stars 18 forks source link

Just early attempt at some sort of physics engine checker #21

Closed Sirmadeira closed 4 months ago

Sirmadeira commented 7 months ago

Will add example, to check if it avoids jittering or not

Sirmadeira commented 7 months ago

@AndrewCS149 Please check it out and give me some good critique

The-DevBlog commented 7 months ago

@Sirmadeira thanks for the PR. I will check this out tomorrow

The-DevBlog commented 7 months ago

@Sirmadeira

Im a little concerned with adding bevy rapier as a dependency. I would ideally like to leave that out. Im wondering if there is another way to approach the problem so that we can avoid adding bevy rapier. Could you post a screenshot/video someone demoing the issue that you are facing so I can better understand what you are trying achieve? I'd definitely love to work with you on this and figure out a solution.

Sirmadeira commented 6 months ago

The issue is simple and it is also described at bevy xpbd docs at https://docs.rs/bevy_xpbd_2d/latest/bevy_xpbd_2d/#why-does-my-camera-following-jitter

Basically since the most physics systems are stored in the PostUpdate status system, the transform of the camera and the player often jitter because of 1 frame delays.

impl Plugin for CameraPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(Startup, spawn_camera);
        app.add_systems(Update, 
        (toggle_cursor,
        orbit_mouse.run_if(orbit_condition),
        zoom_mouse.run_if(zoom_condition))
        .chain());
        app.add_systems(Update, sync_player_camera.after(PhysicsSet::StepSimulation));
    }
}

So a simple app system like this if it runs the sync_player_camera in Update the player entity will jitter a lot. If a characters movement is physics engine dependent. Perhaps finding a bevy system that is guaranteed to run after the physics systems in rapier or xpbd would be the ideal solution. But I tested so many pre-available and couldn't find one that fits the category. Perhaps creating a manual one could avoid importing the engine as an dependency. I will take a shot at it It wont occur in controllers made solely using bevy which are rare to be honest. Here is a sample video

Gravação de tela de 11-04-2024 12:56:41.webm

The-DevBlog commented 6 months ago

@Sirmadeira

Okay I see. Yeah that is very clearly an issue. I'm going to do my best to look at this tonight. Just bear with me as my work schedule has been a little hectic this week.

The-DevBlog commented 6 months ago

@Sirmadeira

HUGE apologies on the massive delay. I began working this last night. Would you be able to provide a sample repo that I could checkout? Im having difficulties reproducing the issue. I added a Rigid Body and a Collider to my player entity, but im not seeing any frame jittering.