// ----------------------------------------------------------------------------
// Blitz-Irrlicht 1.8.3 Wrapper by Ed Musolino 2015
// Based off of Frank Dodd's Irrlicht 1.7 Freebasic Wrapper (2006-2010)
// ----------------------------------------------------------------------------

//  This software is provided 'as-is', without any express Or implied
//  warranty.  In no event will the authors be held liable For any damages
//  arising from the use of this software.

//  Permission is granted To anyone To use this software For any purpose,
//  including commercial applications, And To alter it And redistribute it
//  freely, subject To the following restrictions:

//  1. The Origin of this software must Not be misrepresented; you must not
//     claim that you wrote the original software. If you use this software
//     in a product, an acknowledgement in the product documentation would be
//     appreciated but is Not required.
//  2. Altered source versions must be clearly marked as such, And must Not be
//     misrepresented as being the original software.
//  3. This notice may Not be removed Or altered from any source distribution.
//
// /////////////////////////////////////////////////////////////////////////////

#include "bIrrlicht.h"


extern "C"
{

// SPLINE SECTION
DLL_EXPORT float BBCALL iExtrapolatePoint( float time, float initpos, float vel,
										 float accel = 0, float maxvel = 0 )
{
	float point=0.0;
	float tmaxvel;
	float sgn;

    if (((int&)accel & 0x7FFFFFFF)==0) sgn = 0.f; // test exponent & mantissa bits: is input zero?
    else {
        float r = 1.0f;
        (int&)r |= ((int&)accel & 0x80000000); // mask sign bit in f, set it in r if necessary
        sgn = r;
    }

	if(maxvel !=0 && accel !=0)
	{
		tmaxvel = ((sgn)*maxvel-vel)/accel;
		if(tmaxvel=0) point = initpos +vel *time;
		else if(tmaxvel>0 && tmaxvel<time)
		{
			point = float(initpos + vel * tmaxvel + 0.5 * accel * pow(tmaxvel,2));
			tmaxvel = time - tmaxvel;
			point+=sgn * maxvel * tmaxvel;
		}
		else
		{
			point = float(initpos + vel * time + 0.5 * accel * pow(time,2));
		}
	}
	else
	{
		point = float(initpos + vel * time + 0.5 * accel * pow(time,2));
	}
	return point;

}

}
