|
What's wrong with this code? |
blargmob
Member #8,356
February 2007
|
Howdy, I made a small program to test angled sprite movement. It works and all, except for I can tell that on some angles when I shoot the laser or move the ship in the direction the ship points, it's off slightly. I'm using float variables as the speed for both the laser and the ship.
--- |
CursedTyrant
Member #7,080
April 2006
|
What's shipangle/.7111111 doing there? AFAIK it should be like spaceship.xspeed = 8*cos(shipangle*M_PI/180); spaceship.yspeed = 8*sin(shipangle*M_PI/180); , assuming that shipangle is in degrees, which, as far as I can see, is true. The same for the laser. --------- |
blargmob
Member #8,356
February 2007
|
CursedTyrant said: What's shipangle/.7111111 doing there? It's there because shipangle is in allegro-degrees, not normal-degrees. --- |
Kris Asick
Member #1,424
July 2001
|
In that case, you want: With an additive value also applied based on which direction you want as 0. --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
CursedTyrant
Member #7,080
April 2006
|
Hm, I've never played with those. Still, this is the only line in your code where it could be wrong IMO. Maybe .7111111 is not 100% accurate (as it should be, anyway), which would explain your problem? Then again, I never used allegro's rotating functions, so it's just a wild guess. EDIT: Beaten, but it seems I was correct --------- |
blargmob
Member #8,356
February 2007
|
Kris Asick said: In that case, you want: spaceship.xspeed = 8 * cos(M_PI*2/256*shipangle); I tried that, and it was still a bit off. --- |
BAF
Member #2,981
December 2002
|
Where did you get the .711111 from? |
blargmob
Member #8,356
February 2007
|
BAF said: Where did you get the .711111 from? Each normal-degree is .7111111111111111111 etc. of an allegro-degree. To convert, you divide the allegro-degrees by .7111111111 etc.:) --- |
Kris Asick
Member #1,424
July 2001
|
Quote: I tried that, and it was still a bit off. What exactly are you setting M_PI to? If you're only setting it to 3.14, try adding as many significant digits as you can. M_PI = 3.141592654 You may also try casting to avoid conversion order problems. Lastly, xspeed and yspeed must be floating point values for any realm of accuracy, and so must the x and y coordinates of the object. If they're not, change those immediately. --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
BAF
Member #2,981
December 2002
|
What is the M_PI/180 for then? It seems like you are doing a ton of funky conversions. |
blargmob
Member #8,356
February 2007
|
Kris Asick said: What exactly are you setting M_PI to? If you're only setting it to 3.14, try adding as many significant digits as you can. M_PI = 3.141592654 You may also try casting to avoid conversion order problems. spaceship.xspeed = 8 * cos(M_PI*2/256*(float)shipangle); Lastly, xspeed and yspeed must be floating point values for any realm of accuracy, and so must the x and y coordinates of the object. If they're not, change those immediately.
M_PI is already set to.....something. I didn't set it. It set itself. --- |
BAF
Member #2,981
December 2002
|
What's the error? You probably have to cast to an int, which isn't really a big deal. |
blargmob
Member #8,356
February 2007
|
BAF said: You probably have to cast to an int Cast to an int? What? --- |
Kris Asick
Member #1,424
July 2001
|
Quote: If I make the X and Y cords float value instead of int, I get an error when I pass the values to draw_sprite() and other render functions. You need to cast floating point values to integers for those functions like so: draw_sprite(buffer.SPRITE,spaceship.SPRITE,(int)spaceship.x,(int)spaceship.y); . That should do it. Some compilers handle simple casts like that automatically, others don't. --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
BAF
Member #2,981
December 2002
|
Compilers won't handle casts. They will automatically promote values to more precision (coercion), but you have to explicitly cast if you are losing precision. |
Kris Asick
Member #1,424
July 2001
|
Quote: Compilers won't handle casts. You sure about that? I happen to know for a fact that the error blargmob is getting wouldn't happen in MSVC 6.0, which is the compiler I use. I pass floating point values routinely to Allegro functions without casting them and they work perfectly fine. But I know from reading other posts that not all compilers do that. --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
blargmob
Member #8,356
February 2007
|
Ah Ha! I just had to make everything a float value and cast as int. Thanks guys, problem solved.;D;D;D;D;D;D --- |
BAF
Member #2,981
December 2002
|
Quote: You sure about that? I happen to know for a fact that the error blargmob is getting wouldn't happen in MSVC 6.0, which is the compiler I use. I pass floating point values routinely to Allegro functions without casting them and they work perfectly fine. But I know from reading other posts that not all compilers do that. I should have said "AFAIK" and "any standards compliant compiler." I think I'm covered now. Also, I vote for smiley free week again. |
gnolam
Member #2,030
March 2002
|
Yes, it was a while ago now, wasn't it? I'll get right on it. -- |
|