Please login or register.

Login with username, password and session length
Advanced search  

News:

IRC channel - server: waelisch.de  channel: #wme (read more)

Author Topic: sin, cos & co.  (Read 6293 times)

0 Members and 1 Guest are viewing this topic.

McCoy

  • The cocido eater
  • Frequent poster
  • ****
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 365
  • Spurrrrrrring
    • View Profile
    • Spur Games
sin, cos & co.
« on: June 11, 2003, 08:53:12 AM »

There's any way to do trigonometric operations in WME, or I need an external DLL? I'm gonna do an small mini-game for my game, that need physics, so I need some trig...  :P
Logged

Click here to sign my sig!

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re:sin, cos & co.
« Reply #1 on: June 11, 2003, 09:27:29 AM »

No, there are currently no built-in trigonometry functions in WME.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

Jerrot

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 690
    • View Profile
Re:sin, cos & co.
« Reply #2 on: June 11, 2003, 10:39:26 AM »

Just wanted to check this out - can anybody tell me, what's wrong ?

C++:

Code: [Select]
#include <math.h>

extern "C" __declspec(dllexport) double xSin(double mynumber)
{
   return sin(mynumber);
}

WME:

Code: [Select]
external "trigonometry.dll" cdecl xSin(double);
actor.Talk(xSin(90));

As always I'm not sure about the var types. I also tried to replace the "double" in the WME script by "float", "int", "string", ... no errors, but the same result: "[null]". Why ?!
Logged
Mooh!

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re:sin, cos & co.
« Reply #3 on: June 11, 2003, 08:14:14 PM »

You forgot to specify a return type of the function.

Code: [Select]
external "trigonometry.dll" cdecl double xSin(double);

If you don't specify the return type, WME assumes no return value.

Otherwise your function works pretty good :-)
« Last Edit: June 11, 2003, 08:14:59 PM by Mnemonic »
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

Jerrot

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 690
    • View Profile
Re:sin, cos & co.
« Reply #4 on: June 12, 2003, 04:43:18 PM »

You forgot to specify a return type of the function.

*duh!* ::) thank you! :)

Anyway there is something strange - maybe I just forgot everything about maths, but I can't believe it. I created a "trigonometry.dll" with some functions from math.h as mentioned above. To be sure I made no mistake, here is the C code again:

Code: [Select]
extern "C" __declspec(dllexport) double  math_sin(double mynumber)
{
  return sin(mynumber);
}
extern "C" __declspec(dllexport) double  math_cos(double mynumber)
{
  return cos(mynumber);
}
extern "C" __declspec(dllexport) double  math_pow(double mynumber1, double mynumber2)
{
  return pow(mynumber1,mynumber2);
}

The pow() function is just for test purposes - but useful anyway. ;)
Now the script:

Code: [Select]
external "trigonometry.dll" cdecl double math_pow(double, double);
external "trigonometry.dll" cdecl double math_sin(double);
external "trigonometry.dll" cdecl double math_cos(double);
Game.Msg("sin(90) = " + math_sin(90));
Game.Msg("cos(90) = " + math_cos(90));
Game.Msg("pow(2,8) = " + math_pow(2,8));

The result:

pow(2,8) = 256.000000 (as expected!)

sin(90) = 0.893997 (what ?)
cos(90) = -0.448074 ( :o )

IMHO that's wrong! I'd expected them to be "0" and "1".
Changing the external lines to math_sin(int) or math_sin(float) didn't help, the values were wrong. I don't understand it !?
Logged
Mooh!

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re:sin, cos & co.
« Reply #5 on: June 12, 2003, 05:21:47 PM »

That's because those C functions accept the angle in radians, not degrees ;-)

You need to change it to something like:

Code: [Select]
#define PI 3.1415926535f

extern "C" __declspec(dllexport) double  math_sin(double mynumber)
{
  return sin(mynumber * (PI / 180.0f));
}

...to convert "mynumber" to radians.
« Last Edit: June 12, 2003, 05:39:41 PM by Mnemonic »
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

Jerrot

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 690
    • View Profile
Re:sin, cos & co.
« Reply #6 on: June 12, 2003, 10:33:21 PM »

That's because those C functions accept the angle in radians, not degrees ;-)

Actually I don't feel like I could take over the world today... ! Thanks for the explanations, I guess a change is not necessary with that knowledge, I totally forgot about the radian mode on my calculator... or in school, damn! ;)

@McCoy: If you haven't already done this by yourself, I'll post the source code and a link to a compiled version in the new plugins section. I guess all common trigonometric functions should be in there.
Logged
Mooh!

McCoy

  • The cocido eater
  • Frequent poster
  • ****
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 365
  • Spurrrrrrring
    • View Profile
    • Spur Games
Re:sin, cos & co.
« Reply #7 on: June 13, 2003, 08:46:09 AM »

thnx a lot man, i was planning to do it, but now i'm having my final exams so i dont have time. You saved me a lot of work! ;)

Just one thing... can you ad a little function like RadToDeg() and DegToRad() ? I prefer using degrees instead of radians.

The math for this are here, just in case  ;):

Rad. to Deg-> Deg=rad*180/pi
Deg to Rad->  Rad=deg*pi/180
Logged

Click here to sign my sig!

Jerrot

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 690
    • View Profile
Re:sin, cos & co.
« Reply #8 on: June 13, 2003, 12:32:24 PM »

Just one thing... can you ad a little function like RadToDeg() and DegToRad() ? I prefer using degrees instead of radians.

You could do them in WME, eh ? ;)
Anyway - I just added them, you can download v1.1 now...

Logged
Mooh!
 

Page created in 0.079 seconds with 20 queries.