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
Conversation
…nsions instead of odd ones.
…stracted to sprite position.
There was a problem hiding this 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( |
There was a problem hiding this comment.
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.
// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this explanation.
There was a problem hiding this comment.
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).
Thanks again for investigating this! |
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.