Sign in to follow this  
d h k

Easy: Adding Angle To Line

Recommended Posts

Hello everybody, my problem is pretty easy and still it got me stuck for good... I am creating a game in the overhead- or godview that for example real-time strategy games like Command&Conquer or StarCraft use. Now I need to display a line of sight for a unit. Every unit has a (float) angle and I just need to draw a line for a certain length coming "out" of the unit in the direction of the units angle. Now I figured that I'd need to get the coordinates of a point away from the unit and then draw a line from the unit to there.
    ____
   |    |
   |UNIT|-
   |____| ------
                ------
                      -----P1



I am sure I need to do that by using the Sin and Cos functions, but somehow it just does not work out... This is what I have so far - should all be pretty much self-explanatory I hope.
RawDrawLine ( Char[i].PositionX, Char[i].PositionY, 
			Char[i].PositionX + 35 * FloatToInt ( Maths.Sin ( Maths.DegreesToRadians ( Char[i].Angle ) ) ),
			Char[i].PositionY + 35 * FloatToInt ( Maths.Cos ( Maths.DegreesToRadians ( Char[i].Angle ) ) ) );


But that does not work correctly at all, it returns -1 or 0 - kind of random... And don't worry about the language, it's a scripting language.

Share this post


Link to post
Share on other sites
You're converting your return value from Sine or Cosine to an Integer immediately, and only then are you multiplying it by some value. This is your problem. Sine and Cosine return values from -1.0 to 1.0. If you convert these to an Integer immediately, you'll get either -1, 0, or 1, as everything else in that range is not an Integer. Move the "35 *" inside the FloatToInt() function, and see if that helps. This way, you'll get something in the range of -35.0 to 35.0, which will get rounded to an Integer after that point, providing you with -35, -34, -33, ..., 35, which is what you want.

Share this post


Link to post
Share on other sites
That did the trick, thanks a lot!

And I kept on thinking about if I switched COS and SIn the wrong way or something - sometimes I feel dumb... :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this