Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: spook1 on Fri 09/04/2004 14:33:32

Title: sin/cos script returns large numbers?
Post by: spook1 on Fri 09/04/2004 14:33:32
I have written a small funtion to calculate the positionof a shuttle, steered in a caterpillar kind of way.
You can move left-side or right side or both sides.
I calculate the relative trnaslation, compared to the front of the shuttle, and using the relative translation and the orientation of the shuttle, I want to calculate the abvsolute position.

My function returns values of over 400 though, for an expression like:

Cos(orientation)*-1.
Any ideas?

Here is the code:
function shuttle_position(){

int dxrelx = 0;
int dxrely = 0;
int dyrelx = 0;
int dyrely = 0;

relative_position_change(move_left, move_right);

position.orientation = position.orientation + positionchange.orientation;
 if (position.orientation > 6){
   position.orientation = position.orientation - 6;
 }

dxrely = MultiplyNumbers(GetCos(position.orientation),positionchange.y);
dxrelx = MultiplyNumbers(GetSin(position.orientation),positionchange.x);
dyrelx = MultiplyNumbers(GetCos(position.orientation),positionchange.x);
dyrely = MultiplyNumbers(GetSin(position.orientation),positionchange.y);

Display("dx = %d,%d", dxrelx,dxrely);
Display("dy = %d,%d", dyrelx,dyrely);

position.x = position.x +  dxrely + dxrelx;
position.y = position.y + dyrelx + dyrely;



 return (position);

}
Title: Re:sin/cos script returns large numbers?
Post by: Pumaman on Fri 09/04/2004 17:13:07
Are you using some sort of floating point plugin? If so, you can't use  %d  to display one of the special floating point numbers - you need to convert it back to an int first (or you could try %f, but I'm not sure if that would work).
Title: Re:sin/cos script returns large numbers?
Post by: Kweepa on Fri 09/04/2004 17:39:14
It doesn't look like a floating point plugin...
But then it could just be a big mess.

Note the use of +, - and > which wouldn't work with floating point numbers.
Title: Re:sin/cos script returns large numbers?
Post by: spook1 on Fri 09/04/2004 18:38:06
Quote from: SteveMcCrea on Fri 09/04/2004 17:39:14
It doesn't look like a floating point plugin...
But then it could just be a big mess.

Note the use of +, - and > which wouldn't work with floating point numbers.

Indeed, I am not aware of the floating point plugin. I just use the calculator plugin to try and calculate these sin and cosine functions. i was already suspecting that I was making a teriible mistake. I am not much of a programmer (yet ? :-)).

Can you help me and explain where the "big mess" starts?

Where can I download the floating point plugin?? It is not on the pluginpage: http://www.adventuregamestudio.co.uk/games.php?category=21

EDIT: I have found a floating point dll: http://www.mccrea.demon.co.uk/step/ags/AGS_Maya.dll

Now I have a question left: How should I implement it? Or is there much better way to proceed in my apporach?

I am not familiar with C++ or a similar language. Should I buy a book about C#, C++, ??

Regards,
Martijn
Title: Re:sin/cos script returns large numbers?
Post by: RickJ on Fri 09/04/2004 20:24:28
Martijn,

If the plugin is returning integer values the results may be multiplied by 100 to keep 2 decimal place fraction.   If this is the case you just need to divide by 100 at some point.   

The floating point plugin is probably a better bet since I think you can have more control of the result.   

[edit]
Here is the thread that has some details of use.   
http://www.adventuregamestudio.co.uk/yabb/index.php?board=2;action=display;threadid=12673;start=msg151648#msg151648
Title: Re:sin/cos script returns large numbers?
Post by: Kweepa on Fri 09/04/2004 20:57:15
That's my plugin :)

Here's how you'd write some of your code with it:


position.orientation = fadd(position.orientation, positionchange.orientation);
if (fgreater(position.orientation, int_to_float(6))){
position.orientation = fsub(position.orientation, int_to_float(6));
}

dxrely = fmul(cos(position.orientation), positionchange.y);

// I haven't tested this but it should work CJ says it just passes it to the C sprintf function
Display("dx = %f,%f", dxrelx,dxrely);

position.x = fadd(position.x, fadd(dxrely, dxrelx));


One thing you should keep in mind is that cos() takes radians so you should convert from degrees with:


int radians = fmul(fdiv(pi(), int_to_float(180)), degrees);


See this thread for the rest of the functions in the plugin:

http://www.adventuregamestudio.co.uk/yabb/index.php?board=2;action=display;threadid=12673 (http://www.adventuregamestudio.co.uk/yabb/index.php?board=2;action=display;threadid=12673)

Steve
Title: Re:sin/cos script returns large numbers?
Post by: RickJ on Fri 09/04/2004 21:28:56
Steve,

I have taken the liberty of making the following document for your plugin.  Please take a look and see if I got everything right.  

Thanks,
Rick


Download
http://www.mccrea.demon.co.uk/step/ags/AGS_Maya.dll (http://www.mccrea.demon.co.uk/step/ags/AGS_Maya.dll)

Working with Floating Point
The floating point plugin performs floating point operation on floating point numbers.  Since AGS does not have a floating point data type the plugin uses integer variables to store floating point numbers.   Conversion functions are provided to convert from one format to another.   Since the compiler no longer differentiates between these two data types it is probably best to contrive a naming convention that makes this distinction.   For example all variables that are to contain floating point numbers can begin with  underscore characters or some other prefix.  

Ags Extensions
These functions supplement the AGS built-in functions and are not involved with floating point numbers.

Title: Re:sin/cos script returns large numbers?
Post by: Kweepa on Fri 09/04/2004 21:48:07
Looks perfect Rick.
I've never been good at documentation :)
Title: Re:sin/cos script returns large numbers?
Post by: Gregjazz on Fri 09/04/2004 22:24:24
Yeah, both Maya and the calculator plugin returns radians. I don't like radians myself, so I made a quick little function that would convert it to degrees. Much more handier for myself. :)

I would recommend Maya over the calculator plugin. It's much better.
Title: Re:sin/cos script returns large numbers?
Post by: RickJ on Sat 10/04/2004 00:18:56
Geoffkhan:  Thanks for the verification.  I agree with your recommendation as well.  As far as radians vs degrees it pretty much depends upon what you are doing.   Many engineering and physics problems are much simplified when expressed in radians.  Other kinds of problems an uses are better expressed using degrees.  I believe that the intel floating point processor uses radians and that Steve just exposed this functionality to AGS.  

Steve:  I have one question about your pluing.  How have you handled overflow and undeflow conditions?   Thanks..

Title: Re:sin/cos script returns large numbers?
Post by: Kweepa on Sat 10/04/2004 01:33:30
Damn, I shouldn't have left the name as Maya - that's just the codename of my current game.
All the trig functions take radians, and the log is a natural log.

As for overflow and underflow, I do nothing. I just call C math library functions directly. So divide by zero is going to be a problem too...