views:

74

answers:

1

Hello,
I need something accurate I can plug equations in to if you can help. How would you apply the equation bellow? Thanks guys.

AVERAGE VELOCITY AND DISPLACEMENT
average velocity
V=X/T

displacement
x=v*T

more info


example

I have 30 seconds and a field that is 170 yards. What average velocity would I need my horse to travel at to reach the end of the field in 30 seconds. I moved the decimal places around and got this.

alt text

Here's what I tried 'the return value is close, but not close enough'
FLA here

var TIMER:int = 10;
var T:int = 0;
var V:int = 5.6;
var X:int = 0;
var Xf:int = 17000/10*2;
var timer:Timer = new Timer(TIMER,Xf);  
timer.addEventListener(TimerEvent.TIMER, incrementCounter); 
timer.start();  
function formatCount(i:int):String {
var fraction:int = Math.abs(i % 100);
var whole:int = Math.abs(i / 100); 
return ("0000000" + whole).substr(-7, 7) + "." + (fraction < 10 ? "0" + fraction : fraction);
}
function incrementCounter(event:TimerEvent) { 
T++; 
X = Math.abs(V*T);
text.text = formatCount(X);
}

tests

TARGET
5.6yards * 30seconds = 168yards

INTEGERS
135.00 in 30 seconds

MATH.ROUND
135.00 in 30 seconds

NUMBERS
140.00 in 30 seconds

control timer 'I tested with this and the clock on my desk'

var timetest:Timer = new Timer(1000,30);
var Dplus:int = 17000;
timetest.addEventListener(TimerEvent.TIMER, cow);
timetest.start();
function cow(evt:TimerEvent):void {
tx.text = String("30 SECONDS: " + timetest.currentCount);
if(timetest.currentCount> Dplus){
timetest.stop();
}
}

//far as I got...couldn't get delta to work...
T = (V*timer.currentCount);
X += Math.round(T);
+2  A: 

i think your problem is that you suppose that passed time = delay*count. That is not true. Timers on modern OSes are not precise, this is because many threads concur for CPU and time comes in fixed intervals, this is especially noticeable if timer interval is small.

So how to this? You should take actual time spans that happened between two ticks of timer, that will give you the time delta. Then x += v*deltaT.

Example:

function incrementCounter(event:TimerEvent) { 
   var now:int = new Date().getTime(); 
   X += Math.abs(V*(now - T));
   text.text = formatCount(X);
   T = now;
}
Andrey
@Andrey, how would I apply this to my code, I understand what you're saying.
VideoDnd
@VideoDnd: check edit
Andrey
@Andrey, good for accuracy, but not velocity. I made a test counter based on your idea.
VideoDnd