I made a projectile motion simulation in ACS3 and one of the functions involves creating a multidimensional array which plots the time against the height of the ball, given an input angle and velocity.
When I traced the values, the 't' variable is supposed to have increments of 0.1, however, in some cases it gives me approximate values (e.g. instead of 0.8, it gives me 0.7999999...). Furthermore, the value of the array is 'undefined'.
I don't know what else there is to try, because if the variable in the loop is t = t + 0.1;, then it shouldn't show any approximate values.
I also tried to add velocity*Math.sin(angle/(180/Math.PI)) instead of vy (the y velocity component).
shoot.addEventListener(MouseEvent.CLICK, Calculate);
function Calculate(event:MouseEvent):void{
var t = 0;
var position:Array = new Array();
var vy = velocity*Math.sin(angle/(180/Math.PI));
var Time = int(((2*vy)/9.81)*100)/100
time_txt.text = Time;
while (t <= Time){
position[t]= (vy*t)-4.905*(t*t);
t = t + 0.1;
trace(t);
trace(position[t]);
}
}
I expect the console to print the proper time intervals (0.1,0.2,0.3...) along with the calculated position value instead of 'undefined' (which is based off of a SUVAT equation). So expected results would look like this:
0.1
(calculated position at time 0.1)
0.2
(calculated position at time 0.2)
0.3
(calculated position at time 0.3)
... and continuing on until the variable 't' is larger than the calculate 'Time'.
Instead I get:
0.1
undefined
0.2
undefined
0.30000000000000004 // Bottom line is... what's the deal with this?
undefined // Or this?
0.4
undefined
0.5
undefined
0.6
undefined
0.7
undefined
0.7999999999999999
undefined
0.8999999999999999
undefined
etc...