Advertisement Jump to content
Sign in to follow this  
d h k

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.

If you intended to correct an error in the post then please contact us.

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.
   |    |
   |____| ------

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 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
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!