|
This thread is locked; no one can reply to it. |
1
2
|
Problem with sin cosine |
blargmob
Member #8,356
February 2007
|
Howdy, I read the post about "how to make an object move in the direction its pointing" and clicked on the link the directed me to an article about this kind of stuff becuase I also had that problem. So I read the article and noticed that I could pass a radian to the "sin()" and "cos()" functions, than multiply the return by the length of the velocity vector. But in order to make this work for me, I had to first convert the allegro degrees (256) of my object into normal degrees (360), than find the radian of the result and pass the final result to the two functions. This worked, but as I read further on, I noticed that "fsin()" and "fcos()" accepted allegro degrees as their parameters, I tried this and it didn't work.....any suggestions?:-/ --- |
ImLeftFooted
Member #3,935
October 2003
|
Quote: I tried this and it didn't work.....any suggestions?:-/ Yes actually, I suggest you post a small (ie <15 lines) bit of code that demonstrates your problem. |
LennyLen
Member #5,313
December 2004
|
Quote: but as I read further on, I noticed that "fsin()" and "fcos()" accepted allegro degrees as their parameters, I tried this and it didn't work.....any suggestions? What do you mean by "didn't work?" And were you actually using the correct function names, which are fixsin() and fixcos()? Here's the manual entry for fixsin() so you can see how it should be used. By the way, for modern computers with fast FPUs, using the fixed versions may actually be slower than their floating point counterparts.
|
blargmob
Member #8,356
February 2007
|
Well, the article said the "fsin()" and "fcos()" are the function names. I compile with no errors but when I run the program some super - duper - oober high number is produced. Also, when I try to declare the angle as "fixed" my compiler returns an error that says "fixed" is invalid and doesn't work. --- |
kazzmir
Member #1,786
December 2001
|
blargmob
Member #8,356
February 2007
|
I tried to pass the angle variable in the "fcos()" and "fsin()" functions using itofix but that didn't work either....help please!..... --- |
kazzmir
Member #1,786
December 2001
|
Show some friggin code! |
blargmob
Member #8,356
February 2007
|
--- |
kazzmir
Member #1,786
December 2001
|
gnolam
Member #2,030
March 2002
|
Quote: I had to first convert the allegro degrees (256) of my object into normal degrees (360), than find the radian of the result ... why not just convert directly between Allegro degrees and radians (* π / 128)? Of course, the best course of action is to keep everything in radians and only convert when drawing (* 128 / π). It's by far the easiest, most convenient and readable way. And for Eris's sake, use floats!
void and_then_draw_it(void) { rotate_sprite(buffer, shipsprite, shipx, shipy, ftofix(shipangle*128.0/M_PI)); }
-- |
blargmob
Member #8,356
February 2007
|
But if I stick with float point math I have to do conversions everry time I call sin() and cos(). --- |
gnolam
Member #2,030
March 2002
|
No. Unless you mean the float->double conversion in sin() and cos() (they take and return doubles), but even the overly anal language of C++ does that automagically without complaining. -- |
blargmob
Member #8,356
February 2007
|
gnolam said: sin() and cos() (they take and return doubles) No. Sin() and cos() both take radians, not doubles. (unless you meen the radian AS the double) And how come when I declare a variable with "fixed" my compiler returns an error saying that "fixed" isn't a class and doesn't exist? --- |
kazzmir
Member #1,786
December 2001
|
Quote: (unless you meen the radian AS the double) Yes, he meant radians as doubles. radians are a unit of measure, doubles are an encoding of numeric quantity, they are independant of each other. It would be like saying "im going to measure this in meters, not integers". Quote: And how come when I declare a variable with "fixed" my compiler returns an error saying that "fixed" isn't a class and doesn't exist? I forget exactly what causes this, are you including <allegro.h> before you use a fixed variable? |
Michael Jensen
Member #2,870
October 2002
|
Dont use fixed numbers, they're slower than just using doubles and only have a precision from ~-32000 to ~32000 -- just use itofix and ftofix when you need to pass them into allegro functions... Also as kazzmir said, radians, degrees, and allegro degrees are just units of measure. You can put radians into a double, a float or a fixed, just as well as you can put allegro degrees, or degrees, or meters, miles, or light years in. A radian is a number between 0 and ~6.2831 (PI*2), a degree is between 0 and 360, and an allegro degree is between 0 and 255 -- they're all the same and you can use basic fractional math to convert between them. For simplicity sake, you could do the following after including math.h (and make sure you include math.h or cos/sin won't work right): #include <math.h> #define DegToRad PI/180.0f #define RadToDeg 180.0f/PI then when you have a variable: double myvar = 180; //180 degrees double length = 10; //10 pixels and you need to pass it into cos or sin you can:
edit: #define DegToADeg 256.0f/360.0f #define ADegToDeg 360.0f/256.0f And Similarly to convert allegro degrees to radians: #define ADegToRad PI/128.0f #define RadToADeg 128.0f/PI
|
blargmob
Member #8,356
February 2007
|
Ah,ha! Thanks. That's what I was looking for. A simply and fast way to convert....thanks. (Can't believe I didn't think of that);) --- |
Tobias Dammers
Member #2,604
August 2002
|
On the radians / double issue again: The unit chosen is independent from the numeric encoding format you use: int, allegro::fixed, float, double, etc. My suggestion is to leave the fixed-point math alone altogether, and do everything using floats (or doubles) and radians. The only point where you really can't avoid 256-based fixed-point values is when you pass angles to allegro's drawing functions, in which case you should just take the float-point value and convert it on-the-fly, like this: #include <allegro.h> #include <math.h> // assuming angle to be a float: rotate_sprite(screen, sprite, 100, 100, ftofix(angle * 128.0f / M_PI));
--- |
X-G
Member #856
December 2000
|
Quote:
#define DegToADeg 256/360 Baaaaaaaad idea. 256/360 is an integer division, resulting in an integer. You want 256.0/360.0 or something similar. Quote: "Enemy fighters at 3 o'clock!") - each hour equals 15 degrees. Don't you mean 30 degrees? -- |
Tobias Dammers
Member #2,604
August 2002
|
'course. 30 degrees. I'll go edit, hehe. --- |
Michael Jensen
Member #2,870
October 2002
|
Quote: Baaaaaaaad idea. 256/360 is an integer division, resulting in an integer. You want 256.0/360.0 or something similar. Thank you, I'll edit that post and put 256.0f/360.0f -- etc, I've been using VB.NET where constants have to be declared with data types and this would work fine, and I've forgotten about some the annoyances of C... Public Const DegreesToADegrees As Double = 256 / 360
|
BAF
Member #2,981
December 2002
|
Even 256/360.0f would do. But, even that looks like horrible coding style to me (DegToADeg sounds like a function, not a constant, which should be const float blah = blah anyway ). I'd personally do #define DegToADeg(x) = (x) * 256/360.0f. |
Tobias Dammers
Member #2,604
August 2002
|
Quote: #define DegToADeg(x) = (x) * 256/360.0f. What about: const float DEG2ADEG_FAC = 256.0f / 360.0f; inline float DegToADeg(float x) { return x * DEG2ADEG_FAC; } ? --- |
BAF
Member #2,981
December 2002
|
That's even better. [edit] |
ImLeftFooted
Member #3,935
October 2003
|
Baf said: I'd personally do #define DegToADeg(x) = (x) * 256/360.0f.
#define DegToADeg(x) = (x) * (256/360.0f)
Makes it easier for a compiler to optimize your code. |
Matt Smith
Member #783
November 2000
|
Some versions of <math.h> define fcos() and fsin() as float versions (rather than double) of sin() and cos() This is why Allegro's fsin() and fcos() have been renamed to fixsin() and fixcos(). It is better to use the new names to avoid confusion (both by you and by your compiler). The old compatability defines may be removed by Allegro 5.0. |
|
1
2
|