Skip to content

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

@CtrlAltCuteness

Description

@CtrlAltCuteness

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-

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions