This site requires JavaScript, please enable it in your browser!
Greenfoot back
JasonZhu
JasonZhu wrote ...

2013/12/2

Rounding Error

1
2
danpost danpost

2013/12/3

#
JasonZhu wrote...
Sure dan, thanks for all your help. By the way, I was agonizing over making an image mirrorHorizontally only once in an act method. Is this possible?
Of course it is possible. Just add an instance boolean field to determine if it has been done or not. Then:
if (/* field is false */)
{
    getImage().mirrorHorizontally();
    // change field to true
}
davmac davmac

2013/12/3

#
I've taken a quick look at your scenario. There is an inaccuracy error, but it is not caused by Greenfoot and is technically not a rounding error. It has to do with the way you instantiate Vectors using an angle and magnitude rather than with separate x/y components. For instance your GRAVITY vector:
    private static final Vector GRAVITY = new Vector(90,0.055);
Results in the following approximate values: double dx = 3.3677E-18 double dy = 0.055 As you see dx is very small, but it is not 0, and the error accumulates each time you apply gravity. Looking at the Vector class code, dx and dy are determined by:
        dx = length * Math.cos(Math.toRadians(direction));
        dy = length * Math.sin(Math.toRadians(direction));
So, the error comes from inaccuracy in the Math.cos(...) and/or Math.toRadians(...) method, which is part of Java and otherwise nothing to do with Greenfoot. You could solve this problem by using the alternative constructor which allows specifying dx/dy vector components exactly:
    private static final Vector GRAVITY = new Vector(0.0,0.055);
JasonZhu JasonZhu

2013/12/3

#
Thank you all for the time you put into assisting me find this error. It means a lot to me!
You need to login to post a reply.
1
2