0
votes

I'm new to flash and can't find the bug in my code. I want a space ship to be able to accelerate in a vector, to not be able to accelerate over a max velocity, to keep that velocity vector when it stops accelerating but then suffer from friction (space dust ,) ). (It happens in a 2d stage.)

I think my math is correct, but I get a bug with velVector sometimes returns NaN - here's my code:

var friction:Number = .96;
var force:Number = .1;
var maxVel:Number = 3;
var accVector:Object = new Object();
var velVector:Object = new Object();
var velocity:Number;
var acceleration:Number;

If the ship points in the right direction it executes function 'accelerate' and if not it executes function 'drift'. It always executes 'moveship'

function accelerate():void {
    curRotation.vx = Math.cos(rotation/180*Math.PI);
    curRotation.vy = Math.sin(rotation/180*Math.PI);

    var angle:Number = Math.atan2(curRotation.vy, curRotation.vx);

    velocity = Math.sqrt(velVector.vx^2 + velVector.vy^2); //get velocity in both directions
    acceleration = (maxVel - velocity)/maxVel*force; //this is to make it harder to accelerate when it approaches maxvelocity

    accVector.vx = Math.cos(angle) * acceleration; //accelerate in each dimension
    accVector.vy = Math.sin(angle) * acceleration;

    velVector.vx += accVector.vx; //add acceleration to velocity
    velVector.vy += accVector.vy;
}

function drift():void {
    velVector.vx *= friction; //decrease velocity when not accelerating
    velVector.vy *= friction;       
}

function moveShip():void {
    trace("velocity", velocity)
    x += velVector.vx; //move
    y += velVector.vy;
}

Many thanks!

1
You angle variable is of type number, but you gave it a lowercase "n". Does your actual code have this same mistake?Adam Harte
nope, it doesn't. Sorry for thatuser2237931
I didn't know you could use ^2 (just tried it myself). However, it doesn't seem like it does what you want as "5^2" equals 7, not 25?mitim
You can use Math.pow(velVector.vx, 2);Adam Harte
I think you should perform extended logging in order to trap your vector throwing a NaN somewhere. Put a trace statement into enter frame listener, and a trace('theFunction called') in anything that changes velVector in any way. In order to avoid spam, put an enterframe trace like if (!nanflag) if (isNaN(velVector.vx)) { nanflag=true; trace('velVector.vx is NaN'); }Vesper

1 Answers

0
votes

I guess, your problem is in using of unsafe math operations without values validation. So, your velVector depends on accVector which depend on acceleration. You have a division operation counting "acceleration" val. That's it:

acceleration = (maxVel - velocity)/maxVel*force;

In AS3 you CAN divide by zero, and you'll get Infinity in such situation. But, there is also a chance to get NaN in case you are dividing zero by zero:

var acceleration:Number = 0 / 0;
trace(acceleration); //NaN

If you try doing something with NaN writing the result in Number or untyped variable (not int!), you'll always get NaN:

var a:Number = 5 * NaN;
trace(a); //NaN

So, I here is an example, which gives NaN:

acceleration = 0 / 0; //NaN

accVector.vx = Math.cos(angle) * acceleration; //NaN regardless on "angel" value

velVector.vx += accVector.vx; //NaN regardless on "velVector" value.

I hope, this will help.