scratchfoundation / scratch-flash

Open source version of the Scratch 2.0 project editor. This is the basis for the online and offline versions of Scratch found on the website.
https://scratch.mit.edu
GNU General Public License v2.0
1.33k stars 512 forks source link

Tan of 90 lets sprites go past normal position limits #1389

Closed jwzimmer-zz closed 5 years ago

jwzimmer-zz commented 6 years ago

Discussed with @cwillisf, reported by user -Handle-. If you use tan(90) for sprite position, the value will be a very large number and the sprite will not appear within the visible part of the stage.

Repro: https://scratch.mit.edu/projects/212387052/#editor

screen shot 2018-03-27 at 9 45 51 am

@cwillisf said:

Kenny2github commented 6 years ago

tan(x) where x is 90 technically should have the same result as x/0, i.e. NaN. However, values of x approaching 90 should get progressively larger (e.g. tan(89) ~= 57.3 but tan(89.99) ~=5730).

TheLogFather commented 6 years ago

I've come across this numerous times, and it's probably worth noting that it has nothing to do with tan, specifically – it's all to do with the large numbers.

I've always suspected this issue comes down to rounding errors when performing the calculations on such large values (i.e. maths operations on x & y in function keepOnStage() in ScratchSprite.as) – the final values still end up outside the expected range (i.e. not within screen limits).

TheLogFather commented 6 years ago

I think a fairly quick-n-easy fix might be to assume that the dimensions of the sprite will not exceed some suitably large value (a million pixels?), and then check if the x and/or y positions do exceed this by somewhat more (positively or negatively). If so, then bound the co-ordinate without any reference to the actual supplied x and/or y (i.e. using only the sprite's bounding box, along with the sign(s) of the relevant x/y co-ord(s) to tell which screen edge to bound it on).