Advertisement

math utilities

Started by January 16, 2005 10:14 PM
4 comments, last by LDenninger 19 years, 10 months ago
i tried to register math functions but always fail. r = engine->RegisterGlobalFunction("float sin (float)", asFUNCTION(sin), asCALL_CDECL); assert( r >= 0 ); i wonder if there is any available math functions in angelscript.
Regular "sin" returns double and wants a double argument. I'm not sure but this might make the registering fail since you declare it as returning float and wanting a float argument.

Try to register with "double sin(double)" instead, or you could register "sinf" with asFUNCTION(sinf). sinf is just like sin except that it returns float and accepts a float argument.
Advertisement
i tried double also, it failed too.

What will be the return value on success/failure?

(Ie is the assert correct with >= instead of >)
visit my website at www.kalmiya.com
This works (I just verified it):

int r = engine->RegisterGlobalFunction("float sin(float)", asFUNCTION(sinf), asCALL_CDECL); assert( r >= 0 );


You'll have to do '#include <math.h>' of course.

Kitt3n:

The return value is negative on error. So the correct test to do is 'r >= 0'.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Here's my math-collection, just in case anyone might find a use for it.
I really can't remember if I wrote it myself, if somebody else wrote it or even if it's straight from the AS-source :)
Comments & suggestions are always welcome ofcourse.

//---------------------------------------------------------------------------------------------------------------------//	Math utils//---------------------------------------------------------------------------------------------------------------------const float		asPI = M_PI;float			asRandF()											{ return float(rand()) / float(RAND_MAX); } // return a random float in range [0..1]float			asAbs(float inVal)									{ return ::fabs(inVal); }float			asMin(float inA, float inB)							{ return inA <= inB ? inA : inB; }float			asMax(float inA, float inB)							{ return inA >= inB ? inA : inB; }float			asFloor(float inVal)								{ return float(floor(inVal)); }float			asCeil(float inVal)									{ return float(ceil(inVal)); }float			asRound(float inVal)								{ return float(floor(inVal + 0.5f)); }float			asClamp(float inVal, float inMin, float inMax)		{ return (inVal < inMin ? inMin : (inVal > inMax ? inMax : inVal)); }void as_RegisterMathUtils(asIScriptEngine * outEngine){	// stupidity check	if (outEngine == NULL) return;	// A few common math functions, taken directly from the C runtime library	int r;	r = outEngine->RegisterGlobalProperty("const float PI",	const_cast<float *>(&asPI)); assert(r >= 0);	r = outEngine->RegisterGlobalFunction("float Abs(float)", asFUNCTION(asAbs), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float Sin(float)", asFUNCTION(::sinf), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float Cos(float)", asFUNCTION(::cosf), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float Sqrt(float)", asFUNCTION(::sqrtf), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float ArcSin(float)", asFUNCTION(::asinf), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float ArcCos(float)", asFUNCTION(::acosf), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float Tan(float)", asFUNCTION(::tanf), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float ArcTan(float)", asFUNCTION(::atanf), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float ArcTan2(float, float)", asFUNCTION(::atan2f), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float Log(float)", asFUNCTION(::logf), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float Pow(float, float)", asFUNCTION(::powf), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float Rand()", asFUNCTION(asRandF), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float Min(float, float)", asFUNCTION(asMin), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float Max(float, float)", asFUNCTION(asMax), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float Floor(float)", asFUNCTION(asFloor), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float Ceil(float)", asFUNCTION(asCeil), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float Round(float)", asFUNCTION(asRound), asCALL_CDECL); assert(r>=0);	r = outEngine->RegisterGlobalFunction("float Clamp(float, float, float)", asFUNCTION(asClamp), asCALL_CDECL); assert(r>=0);}
_-=[ "If there's anything more important than my ego around, I want it caught and shot now." ]=--=[ BeatHarness ]=-=[ Guerrilla Games ]=-=[ KillZone ]=-

This topic is closed to new replies.

Advertisement