sin/cos script returns large numbers?

Started by spook1, Fri 09/04/2004 14:33:32

Previous topic - Next topic

spook1

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);

}

Pumaman

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).

Kweepa

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.
Still waiting for Purity of the Surf II

spook1

#3
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

RickJ

#4
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

Kweepa

#5
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

Steve
Still waiting for Purity of the Surf II

RickJ

#6
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

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.

  • import int GetTextExtentX(int font, string text);
  • import int GetTextExtentY(int font, string text);

    Conversion Functions
    The following functions provide the ability to convert integer numbers to/from floating point numbers.  Floating point results and parameters are stored in integer variables.  

  • import int int_to_float(int a);
  • import int ints_to_float(int units, int thousandths);
  • import int float_to_int(int a);

    Floating Point Functions
    The following functions operate exclusively on floating point numbers stored in integer variables.

  • import int fadd(int a, int b);
  • import int fsub(int a, int b);
  • import int fmul(int a, int b);
  • import int fdiv(int a, int b);
  • import int fneg(int a);
  • import int fabs(int a);
  • import int fcomp(int a, int b);
  • import int fgreater(int a, int b);
  • import int fless(int a, int b);
  • import int sin(int a);
  • import int cos(int a);
  • import int tan(int a);
  • import int asin(int a);
  • import int acos(int a);
  • import int atan(int a);
  • import int atan2(int y, int x);
  • import int exp(int a);
  • import int log(int a);
  • import int pow(int a, int b);
  • import int sqrt(int a);
  • import int pi();


    [edit]
    One question Steve.  Above you mention that cos takes radians, presumably this is also true of the other trig functions.  Can you verify if this is true.  

Kweepa

Looks perfect Rick.
I've never been good at documentation :)
Still waiting for Purity of the Surf II

Gregjazz

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.

RickJ

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..


Kweepa

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...
Still waiting for Purity of the Surf II

SMF spam blocked by CleanTalk