Monday, October 13, 2014

Fish and fly physics and stuff

Fixed fly speed calculation. Not 100% perfect, but passable. Needs fine tuning regarding what we want
the fish's position to be when catching fly.
The problem was not visualizing the game world properly. I passed the wrong arguments to the calculation method, and the actual calculation was faulty as well. Once I drew it down, it was easy to see what was wrong. It’s an easy time = distance / velocity calculation, but I used the fish’s x-position + the frog’s x-position to get the distance, which is not the correct distance from fish to frog. Since the origin(0, 0) is in the upper left corner, the fish’s x-position originates from that corner as well. To get the position where we want the fish and fly to intersect, I therefore need to subtract the frog’s x-position, not add it. 

Visualizing the speed calculation.
 
As can be seen here, the frog and fish now align properly at the frog's position(still incorrect y-pos).
I also fixed the fly’s y-position. Before we only set the y-pos to fish’s maximum position that we get from the fish jump calculations. But that is just a distance in pixels, namely the distance from the bottom of the screen to the fish’s y max. Again, the origin is in the top left corner, so the fly would be positioned according to the fish jump height but with the top as origin instead of the bottom. I changed it to set the fly y-pos to the height of the screen subtracted by the fish’s y max to get a better representation. 
 
The fish jump height being used incorrectly.
Lastly, I had to change when the calculations take place. Since the fly is reset before the fish, the calculations to get the new fly speed and y-pos needs to wait until the fish is reset as well. Otherwise it will use the old values of the fish to calculate the new numbers. 
 
Fly and fish align correctly at the frog's position with correct y-pos. Still requires fine tuning.