Sin & Cos: The Programmer's Pals!

102 bookmarks. First posted by gnaritas march 2008.

This article will explain sine, cosine, vectors, atan2, and some useful special effects such as how to make homing missiles and how bitmap rotation works.

gamedev
programming
trigonometry
math
sine
cosine
march 2018 by jakobb

In this article I shall discuss several game programming techniques, all revolving around a central theme: the sine and cosine functions.

programming
math
development
gamedev
games
algo
via:popular
march 2018 by jonlabelle

Okay, so now we know there are two ways to store a vector - with polar coordinates and with Cartesian coordinates - and that in this case they both have their advantages. So which one do we actually use? Well, it wouldn't be a big problem if we knew a way to calculate the angle and speed from the x- and y-coordinates, and vice versa.

And I wouldn't be writing this article if that wasn't possible!

First I'll talk about converting from polar to Cartesian coordinates. It is, of course, also possible to convert the other way, but I will talk about that later on.

There are two functions available to us to accomplish this. These functions are sine and cosine (sin and cos). Whoa! You didn't see that coming, did you?

The sine can be used to calculate the y-coordinate of a vector, and the cosine can be used to calculate the x-coordinate. (Sometimes you see this the other way round, and one or both coordinate values may be negated. I encourage you to think about the effect this would have when you have learned more about the functions.) The sin() and cos() functions take only one parameter: the angle. They return a number between -1 and 1. If you multiply this number by the length of the vector, you will get the exact Cartesian coordinates of the vector. So your code will look like this:

speed_x = speed_length * cos (speed_angle);

speed_y = speed_length * sin (speed_angle);

So that's it: for a racing game you just store the angle and the length of the velocity vector. You adjust these according to the player's input, and you calculate the x- and y-coordinates when you are ready to update the position of the racing car.

games
algorithm
engineering
And I wouldn't be writing this article if that wasn't possible!

First I'll talk about converting from polar to Cartesian coordinates. It is, of course, also possible to convert the other way, but I will talk about that later on.

There are two functions available to us to accomplish this. These functions are sine and cosine (sin and cos). Whoa! You didn't see that coming, did you?

The sine can be used to calculate the y-coordinate of a vector, and the cosine can be used to calculate the x-coordinate. (Sometimes you see this the other way round, and one or both coordinate values may be negated. I encourage you to think about the effect this would have when you have learned more about the functions.) The sin() and cos() functions take only one parameter: the angle. They return a number between -1 and 1. If you multiply this number by the length of the vector, you will get the exact Cartesian coordinates of the vector. So your code will look like this:

speed_x = speed_length * cos (speed_angle);

speed_y = speed_length * sin (speed_angle);

So that's it: for a racing game you just store the angle and the length of the velocity vector. You adjust these according to the player's input, and you calculate the x- and y-coordinates when you are ready to update the position of the racing car.

march 2018 by janpeuker

march 2018
by kj

HelixSoft Home Follow me on twitter! @mpvaniersel amarillion@yahoo.com Introduction In this article I shall discuss several game programming techniques, all…

from instapaper
march 2018 by alexdunae

HelixSoft Home Follow me on twitter! @mpvaniersel amarillion@yahoo.com Introduction In this article I shall discuss several game programming techniques, all…

from instapaper
march 2018 by peterwhelan

march 2018
by telega

Sin & Cos: The Programmer's Pals! amarillion@yahoo.com Introduction In this article I shall discuss several game programming techniques, all revolving around a central theme: the sine and cosine functions. This article will explain sine, cosine, vectors, atan2, and some useful special effects suc

gamedev
geometry
october 2014 by umrain

Learn how to program some gaming algorithms.

coding
development
games
game
programming
code
february 2011 by jbydesign

In this article I shall discuss several game programming techniques, all revolving around a central theme: the sine and cosine functions. This article will explain sine, cosine, vectors, atan2, and some useful special effects such as how to make homing missiles and how bitmap rotation works.

article
gamedev
math
programming
sine
cosine
3d
graphics
february 2011 by raphman

In this article I set out to answer some of the most common questions on sine and cosine, or trigonometry in general. I could give you a more mathematical explanation of sine and cosine, but I wanted this article to be of practical use to game programmers, especially to Allegro game programmers, not to give an encyclopedic description of abstract mathematics.

math
programming
february 2011 by joshcarter

tags

3d algo algorithm alt animation archiv arduino article articles bookmarks_bar c circle code coding cos cosine cs development drawing education engineering game-development game-programming game gamedev games game_development geometry graphics halite2018 javascript learning math mathematics maths pal programming reference sin sine tech theory to-read tolearn toread trig trigonometry tutorial vector via:popular video-games