Hey there! Although I’m not new to As3 I’m also not good at it. To get a better understanding of it all i decided to make a sandbox from scratch. I already got some basic functionality in but my velocity is for some reason set to NaN when distance is 0 and acceleration is set to 2 meters per a second. To understand how faulty that is, the program translates your resolution to real life meters. :wasted:
Any way could some one explain to me why im getting these values, and better yet, what i can do to stop them.
Code:
//Used to calculate d.
var OBJECT_OLD_X;
var OBJECT_OLD_Y;
//Used to calculate pixel to meter ratio
var DPI = Capabilities.screenDPI;
var RES_X = Capabilities.screenResolutionX;
var RES_Y = Capabilities.screenResolutionY;
var RATIO_X = (RES_X/DPI)/39.370;
var RATIO_Y = (RES_Y/DPI)/39.370;
var RATIO_X_PIXEL = RATIO_X/RES_X;
var RATIO_Y_PIXEL = RATIO_Y/RES_Y;
//Used to tell program what direction the object was moving in.
var NEGITIVE_X = 1;
var NEGITIVE_Y = 1;
//A few physics vars.
//Distence.
var d;
//Mass, Currently set to the mass of a tenis ball. (Or so i beleve.)
var m = 0.058;
//Velocity.
var v;
//Acceleration
var a;
//Momentum
var p;
//Event listeners linked to drag&drop and onEnterFrame snipets.
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);
OBJECT.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
//Does most of the updating.
function fl_EnterFrameHandler(event:Event):void
{
//Originaly Stoped a & d from being NaN. Doesnt work in some instences.
/*if(a == false)
{
a = 0.00001;
}*/
/*if(d == false)
{
d = 0.00001;
}*/
//Calculates distence. ^2 Doesnt work for what ever reason so i was forced to copy paste and multiply.
d = Math.sqrt((((OBJECT.x - OBJECT_OLD_X)*RATIO_X_PIXEL)*((OBJECT.x - OBJECT_OLD_X)*RATIO_X_PIXEL))+(((OBJECT.y - OBJECT_OLD_Y)*RATIO_Y_PIXEL)*((OBJECT.y - OBJECT_OLD_Y)*RATIO_Y_PIXEL)));
//Physics stuffs, nothing to see here.
v = (d*d)/(d*(1/30));
p = v*m;
a = ((d*v)/(d*(1/30)))^2;
//Sets the NEGITIVE vars to 1 or -1 based on positions.
if(OBJECT.x - OBJECT_OLD_X < 0)
{
NEGITIVE_X = -1;
}
else if(OBJECT.X - OBJECT_OLD_X > 0)
{
NEGITIVE_X = 1;
}
if(OBJECT.Y - OBJECT_OLD_Y < 0)
{
NEGITIVE_Y = -1;
}
else if(OBJECT.Y - OBJECT_OLD_Y < 0)
{
NEGITIVE_Y = 1;
}
//Moves OBJECT based on vars.
OBJECT.x = OBJECT.x + p*NEGITIVE_X + a*NEGITIVE_X;
OBJECT.y = OBJECT.y + p*NEGITIVE_Y + a*NEGITIVE_Y;
//Some debugging tools.
trace("Distence = " + d + " meters");
trace("Velocity = " + v + " meters/s");
trace("Aceleration = " + a + " meters/s");
//trace(RATIO_X);
//trace(RATIO_Y);
//trace(RATIO_X_PIXEL);
//trace(RATIO_Y_PIXEL);
//Updates OBJECT OLD for next frame.
OBJECT_OLD_X = OBJECT.x;
OBJECT_OLD_Y = OBJECT.y;
}
//Drag and drop snippet. Used for debuggig.
function fl_ClickToDrag(event:MouseEvent):void
{
OBJECT.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
OBJECT.stopDrag();
}
Thanks in advance, David.
EDIT: I forgot to say that when my OBJECT is not moving it’s positioned at 0, 0.