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

Support Snap 2D to Pixel #445

Open
elvisish opened this issue Jan 10, 2025 · 10 comments
Open

Support Snap 2D to Pixel #445

elvisish opened this issue Jan 10, 2025 · 10 comments
Labels
2D Issues concerning 2D scenes bug Something isn't working phantom camera Related to PhantomCamera nodes

Comments

@elvisish
Copy link

elvisish commented Jan 10, 2025

Issue description

I have target set to the player sprite, move_and_slide() is in physics_process(),stretch set to viewport, physics interpolation turned on, snap to pixel turn on in PhantomCamera2D, snap 2d pixels and vertices to pixel turned on:

2025-01-10.11-45-11.mp4

Even with turning some of these off, it still stutters.

Steps to reproduce

  • target set to the player sprite
  • move_and_slide() is in physics_process()
  • stretch set to viewport
  • physics interpolation turned on
  • snap to pixel turn on in PhantomCamera2D
  • snap 2d pixels and vertices to pixel turned on

(Optional) Minimal reproduction project

No response

@ramokz
Copy link
Owner

ramokz commented Jan 12, 2025

It seems like it's specifically the last part of the setup that is the cause of the issue, i.e. Snap 2D Vertices to Pixel / Snap 2D Transforms to Pixel.

Not entirely sure what is needed to support it, but there is likely a way.

@ramokz ramokz added bug Something isn't working phantom camera Related to PhantomCamera nodes 2D Issues concerning 2D scenes labels Jan 12, 2025
@ramokz ramokz changed the title Jitter with 4.4.dev7 and physics interpolation turned on Support Snap 2D to Pixel Jan 12, 2025
@ramokz
Copy link
Owner

ramokz commented Jan 12, 2025

Investigating a bit further, there appears to be some general challenges with the Snap 2D to Pixel project setting: godotengine/godot-docs#7774. So not sure if this is something the addon can / should resolve, but still looking into it.

Out of curiosity, does disabling Snap 2D to Pixel not produce a desired result?

@elvisish
Copy link
Author

Investigating a bit further, there appears to be some general challenges with the Snap 2D to Pixel project setting: godotengine/godot-docs#7774. So not sure if this is something the addon can / should resolve, but still looking into it.

Out of curiosity, does disabling Snap 2D to Pixel not produce a desired result?

It still happens with both disabled:
image

2025-01-12.23-24-37.mp4

@ramokz
Copy link
Owner

ramokz commented Jan 13, 2025

That's strange.

Your setup sounds identical to all the example scenes.
Do you see the issue in those also?

@elvisish
Copy link
Author

That's strange.

Your setup sounds identical to all the example scenes. Do you see the issue in those also?

I matched all of the PhantomCamera2D settings and even tried floor(velocity) to avoid using floats, but it still stutters. Even the hierarchy is the same, I'm not sure what else could be causing it?

@ramokz
Copy link
Owner

ramokz commented Jan 13, 2025

Does the example scenes within the addon directory themselves run fine for you, or do they stutter as well?

Also, would you be able to share a small sample project / MRP? Can run some tests locally to see what's up.

@elvisish
Copy link
Author

Does the example scenes within the addon directory themselves run fine for you, or do they stutter as well?

Also, would you be able to share a small sample project / MRP? Can run some tests locally to see what's up.

With snap turned off, you might have to move back and forth a little to see it happen. Almost feels as though there's a floating point discrepancy?
mariotest.zip

@ramokz
Copy link
Owner

ramokz commented Jan 14, 2025

Running some quick checks, it appears not to be due to the Snap 2D to Pixel in Project Settings, but rather the Snap to Pixel in the PCam2D.

My guess is that it has something to with the character itself not moving in exact pixels, bur rather floats, which would suggest that the camera is getting confused as to which pixel it should snap to.

When I hardcode the position of the character by rounding its value, it does make the stutter go away:

# Rest of the _phyiscs_process code
move_and_slide()

position.x = round(position.x)
position.y = round(position.y)

handle_last_collision()
print(velocity.x)

It's a hacky way of getting around it, but my guess is that the velocity calculation that is happening in the move_and_slide function makes that inevitable, at least by default.

@elvisish
Copy link
Author

Running some quick checks, it appears not to be due to the Snap 2D to Pixel in Project Settings, but rather the Snap to Pixel in the PCam2D.

My guess is that it has something to with the character itself not moving in exact pixels, bur rather floats, which would suggest that the camera is getting confused as to which pixel it should snap to.

When I hardcode the position of the character by rounding its value, it does make the stutter go away:

# Rest of the _phyiscs_process code
move_and_slide()

position.x = round(position.x)
position.y = round(position.y)

handle_last_collision()
print(velocity.x)

It's a hacky way of getting around it, but my guess is that the velocity calculation that is happening in the move_and_slide function makes that inevitable, at least by default.

Is this the same as Snap 2D transforms to pixels? I wonder if it would be possible to figure out which order that happens, and somehow change when Phantom Camera updates. This works well without jitter (although I don't know if snapping is necessary when viewport scaling is used):
image
image

@ramokz
Copy link
Owner

ramokz commented Jan 16, 2025

Is this the same as Snap 2D transforms to pixels?

Not quite.
If you log the position of your playable character, you can see that it still moves in floating-point values even when that is enabled, which, I think, is attributed to the move_and_slide() function.

My guess is that either the Snap 2D Transform/Vertices to Pixel is being ignored by it, or that method just works differently and doesn't, at least by default, move in whole pixels.

Recorded an example below to show what the position of the sprite is when the rounding is disabled / enabled; both with Snap 2D enabled in Project Settings.

pixel-movement.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2D Issues concerning 2D scenes bug Something isn't working phantom camera Related to PhantomCamera nodes
Projects
None yet
Development

No branches or pull requests

2 participants