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

Fix the camera rounding to work when following objects with even dimensions #3789

Merged
merged 6 commits into from Apr 1, 2022

Conversation

D8H
Copy link
Collaborator

@D8H D8H commented Mar 30, 2022

Fix the camera rounding to work when following objects with even dimensions instead of odd ones.

I actually used ceil because otherwise, when the character jumps, it seems to move from one pixel on screen.

Direct following

Linear following

There will be 2 alerts next to each other, but I think it's not an issue.

image

@D8H D8H marked this pull request as ready for review March 30, 2022 19:05
@D8H D8H requested a review from 4ian as a code owner March 30, 2022 19:05
Copy link
Owner

@4ian 4ian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 👍

@@ -85,10 +85,10 @@ namespace gdjs {
// the sprite one and it changes in which direction sprites are rounded.
// It makes sprites rounding inconsistent with each other
// and they seems to move on pixel left and right.
this._pixiContainer.position.x = Math.round(
this._pixiContainer.position.x = Math.ceil(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a link to this PR in the comment so that we can find the context back and why ceil was chosen.

Comment on lines +89 to +109
// PIXI uses a floor function on sprites position on the screen,
// so a floor must be applied on the camera position too.
// According to the above calculus,
// _pixiContainer.position is the opposite of the camera,
// this is why the ceil function is used floor(x) = -ceil(-x).
//
// When the camera directly follows an object,
// given this object dimension is even,
// the decimal part of onScenePosition and cameraPosition are the same.
//
// Doing the calculus without rounding:
// onScreenPosition = onScenePosition - cameraPosition
// onScreenPosition = 980.75 - 200.75
// onScreenPosition = 780
//
// Doing the calculus with rounding:
// onScreenPosition = floor(onScenePosition + ceil(-cameraPosition))
// onScreenPosition = floor(980.75 + ceil(-200.75))
// onScreenPosition = floor(980.75 - 200)
// onScreenPosition = floor(780.75)
// onScreenPosition = 780
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this explanation.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent! Love this example, that's very clear now (notably the fact that there is a minus in the positioning, so ceil instead of floor).

@4ian
Copy link
Owner

4ian commented Mar 31, 2022

Thanks again for investigating this!

@D8H D8H merged commit 41241d7 into 4ian:master Apr 1, 2022
@D8H D8H deleted the FixCameraRounding branch April 1, 2022 10:01
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

Successfully merging this pull request may close these issues.

None yet

2 participants