Actionscript to C++ Conversion

Miscellaneous Forums/General Discussion/Actionscript to C++ Conversion

Hi,

I'm trying to convert Robert Penner's actionscript easing equations to C++ ( MinGW to be precise, since it's a BMax module I'm writing ) but I'm having trouble.

These are the actionscript lines in the trouble function :

t /= d/2;
if (t < 1) return c/2*t*t + b;
t--;
return -c/2 * (t*(t-2) - 1) + b;


I've converted that to:

extern "C" __cdecl float quad_easeinout(float t,float b , float c, float d) {
	if ((t/=d/2) < 1) return ((c/2)*(t*t)) + b;
	return (-c/2*((--t)*(t-2)-1)+b);
}

It's quite wrong though. I know nothing about actionscript and none of these languages seem to be consistent. Java, C++, ActionScript, they all seem to handle pre-increment and post-increment their own way, let alone order of calculation. It may even be a VC++/MinGW variation for all I know.

Well, Yeah - the only thing I noticed as a potential problem was this bit in the last line: (--t)*(t-2). Does C++ guarantee a left-to-right evaluation order? Is it possible the t-2 is calculated before the --t?

I'd just use a separate post increment to be sure, as used in the original. :)

Yeah, I've tried it with the post increment and the result is wrong that way too. I *think* the result is the same, but either way, that's not the source of my error.

What happens if you just put those four lines of actionscript into your C function?

I don't know actionscript, but it doesn't look like it needs converting.

t /= (d/2) Maybe?

Floyd: Yup, I had the same impression initially, but no go.

H&K: Also no go, I'm afraid. I tried a couple of permutations including some other things I was dubious about, but it doesn't seem to be that.

Are the ActionScript variables declared as double or single precision?

Do ActionScript operators have a different precedence to C++?

I'm out of ideas. :)

The variable t, presumably time, is being changed. Are these changes supposed to accumulate?

If so then you need to declare the function argument as float& t, otherwise the t in the function is just a local copy of the original.

Are the ActionScript variables declared as double or single precision?

It wouldn't matter, the mistake is far too large to be related to precision, even if that precision caused cumulative mistakes.

Do ActionScript operators have a different precedence to C++?

I'm guessing they do, which is why I was hoping someone around here might use it.

The variable t, presumably time, is being changed. Are these changes supposed to accumulate?

Nope, all variables are intended to be passed by value.

I don't think your if statement is going to work like that. You are not testing if t<1 you are testing if the equation is < 1 which means "true or false" (I would presume).

I think you need to break it into two lines.

t/=d/2;
if (t < 1) return ((c/2)*(t*t)) + b;
return (-c/2*((--t)*(t-2)-1)+b);

I'm sure I've got these converted to BMax somewhere, if it helps any?

Me rummages about on HD...

Aha...Here we go...
Function QuadraticEaseBoth!(time!, begin!, distance!, duration!)
	time :/ (duration / 2!)
	If time < 1 
		Return ((distance / 2!) * (time * time)) + begin
	Else
		time :- 1
		Return ((-distance / 2!) * ((time * (time - 2)) - 1)) + begin
	EndIf
End Function


Hmm, this page seems to say that multiplication takes precedence over divide, whereas they have equal precedence in C (you need to copy/paste link into address bar).

http://books.google.com/books?id=so1meEopDaEC&pg=PA87&lpg=PA87&dq=actionscript+operator+precedence+and+associativity&source=web&ots=FNnL8tZ-ca&sig=QMlM9shEA9G3F5ccuS_ABblnxhk#PPA88,M1

Taskmaster: I seem to be getting the same results either way. Perhaps it's part of the problem and I need to change other things too. I'll try more permutations.

Yan: Thanks, but that function gives me incorrect results too. Does it give you the results you expect?
If I call it with:

Print QuadraticEaseBoth(2.5,10,20,5)


I would expect to get 15, give or take, but it gives me 20. At 5,10,20,5 it gives me 30.

Big10P: very nice catch, I'll have a read of that and play with the code a bit, see if I can use that to solve it.

I would expect to get 15, give or take, but it gives me 20. At 5,10,20,5 it gives me 30.
That's correct, is it not?

Distance to travel is 20 so halfway would be 10, add on the start position of 10 and you get 20. The second one is 20 + 10 = 30.
??

Gah, I thought these equations were providing start and end. I didn't realize they were working on start and change.

Yep, after correcting my test ( doh! ) your version works when converted to C++. Thanks Yan!

I think I've completed the conversion now. I can't be bothered to organize it, document it, upload it, maintain it, etc, etc, but if anyone wants an easing module, just email me for it.