1

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...

Raku
  • 25
  • 4

1 Answers1

0
  • 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.

Here is a testable example code that may help to solve your problem. Is it doing what you want?

function Calculate(event:MouseEvent):void
{

    var velocity :int = 4; //temp for testing
    var angle :int = 45; //temp for testing
    
    var t :Number = 0;
    var position :Array = new Array();
    var pos_idx :int = 0; //your position within the array
    
    var vy = velocity*Math.sin(angle/(180/Math.PI));
    
    //var Time = int(((2*vy)/9.81)*100)/100
    var Time :Number = 6.5; //temp small value for testing
    
    time_txt.text = Time;
    
    while(t <= Time)
    {
        position[ pos_idx ] = (vy * t) - 4.905 * (t * t);
        trace("position[" + pos_idx + "]: " + position[ pos_idx ] );
        
        t += 0.1;
        t = Number( t.toFixed(3) ); //rounding down the fraction part
        trace("t is: " + t);
        
        pos_idx += 1; //add +1 for next position within array
    }

}

About your issues:

(1)
The Array position being "undefined" issue is because an Array has positions that are whole numbers not fractions. Array means "group of things" (thing1, thing2, thing3 etc) so, for example, if your things are doors then either you look at Door #1 or at Door #2 but compiler will fail if you try to check at Door #0.8.

(2)
In programming languages, an integer is a whole number (use Number or Float or Long for fractions).

Your code var Time :int = int( ((2*vy)/9.81) * 100) / 100; may have a zero as result (eg: when angle is 45) because the result of ((2*vy)/9.81) * 100) / 100; is 0.5766416156465218 but if you cast that as int then the fraction part is now ignored so it rounds down to result of 0.

(3)
PS: Be careful when using traces in a While loop that runs for a long time. This may temporarily freeze your app if it has to report a trace every loop. The bigger the Time the longer the freeze.

Community
  • 1
  • 1
VC.One
  • 12,230
  • 4
  • 21
  • 51