thombruce / verse

🚀 A universe in progress
Other
8 stars 0 forks source link

Background Parallax #96

Closed thombruce closed 9 months ago

thombruce commented 9 months ago

Related: Issue #85 + PR #95

As part of an effort to upgrade to Bevy 0.12, I have removed bevy_tiling_background which was previously responsible for...

  1. The tiling background (this is now reimplemented directly using only native Bevy utilities)
  2. A sort of parallax effect between this tiling background and foreground elements

This parallax effect is now absent, and we should seek to reimplement it... somehow.

thombruce commented 9 months ago

Perhaps take a look at how .with_movement_scale() worked; this is the method used for this effect previously:

image

thombruce commented 9 months ago

Take a look at how the bevy-parallax crate achieves its horizontal parallax effect: https://github.com/Corrosive-Games/bevy-parallax/blob/main/src/lib.rs#L65

There's not much to it. The background is following the camera, moving by some scale offset by a multiplicative factor.

Now, I can't just reuse the same follow_ logic that I did to have the camera follow the player as that matches the new coordinates exactly (it has no sense of how far has been moved - it is sort of teleporting each tick to find the player) but...

If I could somehow get the distance moved by the camera/player since the last update, then I could shift the background by this amount multiplied by a scaling factor... We could use an event to communicate that information.

But do we need to get the distance change? Can't we just divide the distance from the origin by a scaling factor and this... should be good for all possible distances?

new_pos = camera_pos  * parallax_factor // where parallax_factor is in the range 0.0...1.0

A factor of 1.0 would appear to be fully static (matching the ship and camera) and a factor of 0.0 would appear to match the star and planets (the resting frame).

So consider adding a Parallax(pub f32) component and a pub fn move_parallax function that multiplies the camera position by that f32 each frame to get the new position of the parallaxed entity. The reason we wrap a pub f32 is so that we can have different layers with different parallax factors.

That works... but only for static layers. It may be a worthwhile future exercise to consider how this might be achieved for layers that contain objects in motion.