# Easy: Adding Angle To Line

This topic is 4975 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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.PositionX, Char.PositionY,
Char.PositionX + 35 * FloatToInt ( Maths.Sin ( Maths.DegreesToRadians ( Char.Angle ) ) ),
Char.PositionY + 35 * FloatToInt ( Maths.Cos ( Maths.DegreesToRadians ( Char.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 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 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... :)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634133
• Total Posts
3015746
×