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

Use Math.round in drawing functions instead of Math.floor (or its shortcut: ~~) #577

Closed
parasyte opened this issue Oct 5, 2014 · 4 comments
Milestone

Comments

@parasyte
Copy link
Collaborator

parasyte commented Oct 5, 2014

There's a small issue with sub-pixel gravity and Math.floor that can cause "bounciness" on a single pixel. See for an example: #538 (comment)

The fix is simple; use Math.round instead. This ticket is here to start a discussion around whether we want to start standardizing on using Math.round in places where we've used the inversion shortcut with double-tilde: ~~. I'm for using Math.round, and it's probably even easier for JIT engines to optimize than the double-inversion operations.

@obiot obiot added this to the 1.2.0 milestone Oct 5, 2014
@obiot
Copy link
Member

obiot commented Oct 5, 2014

I'm ok with it, as I was saying in the other ticket, yes the performances with ~~ is faster... but with the performance improvements brought by the quadtree, I suppose that we could lose a tiny bit of speed here in exchange for proper drawing position :)

@parasyte
Copy link
Collaborator Author

parasyte commented Oct 5, 2014

Looks like you're right about performance: http://jsperf.com/math-round-vs-double-tilde

So we'll just add 0.5 before the double-inversion. ☺️

@obiot
Copy link
Member

obiot commented Oct 5, 2014

Huh... I never thought about that trick :p
Some other places in melonjs could probably also benefit from it

@parasyte
Copy link
Collaborator Author

parasyte commented Oct 5, 2014

@obiot I'll close this one, since it resolves the bug as reported. I also made an earlier change to replace Math.round with the same trick. There might be other areas in drawing where sub-pixel positions cause bounciness, but I think Entity is the biggest one.

@parasyte parasyte closed this as completed Oct 5, 2014
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

No branches or pull requests

2 participants