SubZeroWins wrote: |
---|
I always move the ImageBottom first before checking the if(<=), so shouldn't <= ALWAYS be true if the distance for both moves are equal and that there is no loss of precision? |
In other words, when you reach the if statement you have only moved ImageBottom but not yet moved ImageTop.
If you where to print the result of the <= expression at the end of each frame (after you have moved both ImageBottom and ImageTop) then I think you would see it is often false. It becomes true after you have moved ImageBottom and then false again after you have moved ImageTop. But if dt is smaller than usual it could happen that ImageBottom is not moved sufficiently for the <= comparison to become true which means ImageTop won't get moved that time.
SubZeroWins wrote: |
---|
Below I added "MOVE BOTTOM" and "MOVE TOP" to show you precisely how the values and checks carry over. Proof that the dt and the distance to move is EXACTLY the same for both the top and bottom. |
This proves my point.
First you have
ImageBottom.left=1024.81 and
ImageTop.left=1065.02:
At this time the <= expression would be
1024.81 <= 1065.02 - .4f * 150
=
1024.81 <= 1005.2
=
false |
But then you update the position of ImageBottom so that
ImageBottom.left=1004.87 and
ImageTop.left=1065.02:
And now the <= expression would be
1004.87 <= 1065.02 - .4f * 150
=
1004.87 <= 1005.2
=
true |
Since it is now true when the if statement is executed it goes ahead and moves ImageTop so that you have
ImageBottom.left=1004.87 and
ImageTop.left=1045.08:
Which brings you back to
1004.87 <= 1045.08 - .4f * 150
=
1004.87 <= 985.08
=
false |
And then it continues like this...
... until
ImageBottom.left=924.386 and
ImageTop.left=964.602:
924.386 <= 964.602 - .4f * 150
=
924.386 <= 904.602
=
false |
And after ImageBottom has been moved you get
ImageBottom.left=904.859 and
ImageTop.left=964.602:
904.859 <= 964.602 - .4f * 150
=
904.859 <= 904.602
=
false |
It's still false (!) and that's is why ImageTop is NOT moved here.
Why did this happen here? Well, if you look at the dt value 0.016273 it's smaller than it has been before. It was just too small for ImageBottom to move enough for the <= if condition to become true. That's all there is to it.
SubZeroWins wrote: |
---|
You seem to be more certain that it is not loss of precision? Even the getGlobalBounds() rounds off to the 3rd decimal place and loses some value, although minor. Maybe that combined with some loss in the subtraction and checking? |
I'm not saying it doesn't happen. It's just not "the problem".
You should always expect there to be loss of precision/rounding errors when working with floating-point numbers.
No matter how hard you try there will always be some of it.
A robust bug-free solution need to be written in a way that it behaves well even when things are a little off.
The solution to the problem is not to try and minimize rounding errors. Instead you need to change the way you handle the movement, because the "logic" is simply wrong.
I have already offered a possible solution in an earlier post. I don't know if you ever tried it. I haven't tested it so it's of course possible that I made some mistake or forgot to think about something.
In games I think it's common to move all objects first and then handle collisions afterwards. Not sure if it's helpful for what you're doing but perhaps something to consider if you're planning to have many objects moving around affecting each other.