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

Execution speed improvement for coords far from x=1, z=1 in 'isCollidedHorizontally' #50

Open
CtrlAltCuteness opened this issue Apr 10, 2017 · 0 comments

Comments

@CtrlAltCuteness
Copy link

CtrlAltCuteness commented Apr 10, 2017

I noticed in VertexClientPE.Utils.Player.isCollidedHorizontally was the while(x < 1) x += 1; and those similar ones directly next to it could be optimized.
I don't know if readability is extemely important, but as I noticed that I had one rounding error when testing just the first two whiles for x (and this was very close to the target number as well in whole numbers, and the fractional part was somewhere between zero to two digits long), my optimized fix actually would help speed up the game if the input value is normally going to be high, and especially if the Javascript environment is already running slow.

Note that your code might have the chance to be rewritten (optimized while still being readable) as well around there if the number actually stays accurate enough. This is mainly about the if(Math.round(x * 100) == 31) x -= 0.01; section below it, yet I cannot be certain if it really is no longer necessary as I haven't tested the fix myself.

Now this is a sample to replace that offending line (and its opposite direction) I first mentioned.

// The equiv. for your current 'while(x < 1) x += 1;' and 'while(x > 1) x -= 1;'
x = x - Math.floor(x) || 1;
// Use the following instead if the range is really '[0, 1)'
x -= Math.floor(x);

-snip-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants