Programming and Sin & Cos

Programming and mathematics are strongly interlinked. If you don’t see the connection yet, you should read this very interesting article about it. For more, click here.

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. I shall start with the very basics, but later on I’ll cover some more advanced programming techniques.
Let’s start with something that can sometimes be difficult to understand for beginners, because it is highly abstract – the vector. A vector can be visualized in different ways.

First of all, you can imagine it as an arrow to a point in space. In the case of two-dimensional space, you need two values to define the vector – one for the x-coordinate and one for the y-coordinate. In the case of three-dimensional space, you will need a third value for the z-coordinate. This article will mostly deal with two-dimensional space though. Three-dimensional space is more complicated, and I’m no expert in that.

In the figure above I have drawn a vector with an x-coordinate of 3 and a y-coordinate of 2. But these two values are not the end of the story. For example, if you draw this vector out on paper, you can measure the length of the vector as 3.6 and the angle between the vector and the x-axis as 34 degrees.

If you think about this further, you can see that you don’t even need the (x,y) coordinates of the vector if you already know its length and the angle it makes with the x-axis. It is perfectly possible to define a vector fully just by its length and angle.

If you use the x- and y-coordinates, you are using Cartesian coordinates. If you use the angle and length of the vector, you are using polar coordinates.

Let’s have an example. Suppose you are writing a top-down racing game (something like Micro Machines). You will need a way to store the velocity (speed and direction) of a racing car. And how do we do that? With a vector. This velocity vector is in fact the change in the racing car’s position from one frame to the next (see figure below). The question is, should we use Cartesian coordinates or polar coordinates for this vector?

Well, storing only the Cartesian coordinates has the advantage that it is very easy to calculate the new position of the racing car at each step. Suppose you store the (x,y) coordinates of the velocity vector in the variables vel_x and vel_y, and the position of the racing car in the variables pos_x and pos_y. All you need to do in the game loop is:

pos_x += vel_x;
pos_y += vel_y;

What could be simpler?

On the other hand, storing the length and angle of the velocity vector has its advantages, in that it makes it easier to implement the racing car controls. Think about it – if the player presses LEFT, you want the racing car to turn left. Supposing you store the angle in the integer car_angle, you could use the following code:

if (key[KEY_LEFT])
{
car_angle -= 1; // turn one degree to the left
}
if (key[KEY_RIGHT])
{
car_angle += 1; // turn one degree to the right
}

And how would you do that if you only stored x and y? You would need to change both of them, but how? That is a lot more difficult! Furthermore, if the player presses UP you want the racing car to go faster. You can achieve this by simply increasing the length of the vector. If you store x and y, you have to change both of them a bit, which is again more complicated.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...



The Wason Selection Task – Four Card Problem Interactive Video

You are shown a set of four cards. Each card has a number on one side, and a color on the other side. We would like to test the truth of the following statement.

If a card shows an even number on one face, then its opposite face is blue.

1 Star2 Stars3 Stars4 Stars5 Stars (5.00 from 2 votes)
Loading ... Loading ...



The Weaver’s Puzzle

Here is one of the hardest puzzles you will see on the internet today. Can you figure out what’s the correct solution or will you just check for the correct answer?

When the Weaver brought out a square piece of beautiful cloth, daintily embroidered with lions and castles, as depicted in the illustration, the pilgrims disputed among themselves as to the meaning of these ornaments. The Knight, however, who was skilled in heraldry, explained that they were probably derived from the lions and castles borne in the arms of Ferdinand III., the King of Castile and Leon, whose daughter was the first wife of our Edward I. In this he was undoubtedly correct. The puzzle that the Weaver proposed was this. “Let us, for the nonce, see,” saith he, “if there be any of the company that can show how this piece of cloth may be cut into four several pieces, each of the same size and shape, and each piece bearing a lion and a castle.” It is not recorded that anybody mastered this puzzle, though it is quite possible of solution in a satisfactory manner. No cut may pass through any part of a lion or a castle.


Source

1 Star2 Stars3 Stars4 Stars5 Stars (4.71 from 7 votes)
Loading ... Loading ...



Paradox Tuesday – Tennis Ball Paradox

Are you up for a real challenge? Find the flaw in this argument and let us know in the comments!

1 Star2 Stars3 Stars4 Stars5 Stars (5.00 from 1 votes)
Loading ... Loading ...



Paradox Tuesday – Hilbert’s Paradox of the grand Hotel

This hypothetical hotel has a countably infinite number of rooms, but they are all occupied. If someone is looking for a room, can the hotel accommodate him?

1 Star2 Stars3 Stars4 Stars5 Stars (5.00 from 2 votes)
Loading ... Loading ...



The Blue Eyed Islanders Puzzle

Do you like mathematical brain teasers? This one should keep you busy for a while.

There is an island upon which a tribe resides. The tribe consists of 1000 people, with various eye colours. Yet, their religion forbids them to know their own eye color, or even to discuss the topic; thus, each resident can (and does) see the eye colors of all other residents, but has no way of discovering his or her own (there are no reflective surfaces). If a tribesperson does discover his or her own eye color, then their religion compels them to commit ritual suicide at noon the following day in the village square for all to witness. All the tribespeople are highly logical and devout, and they all know that each other is also highly logical and devout (and they all know that they all know that each other is highly logical and devout, and so forth).

Of the 1000 islanders, it turns out that 100 of them have blue eyes and 900 of them have brown eyes, although the islanders are not initially aware of these statistics (each of them can of course only see 999 of the 1000 tribespeople).

One day, a blue-eyed foreigner visits to the island and wins the complete trust of the tribe.

One evening, he addresses the entire tribe to thank them for their hospitality.

However, not knowing the customs, the foreigner makes the mistake of mentioning eye color in his address, remarking “how unusual it is to see another blue-eyed person like myself in this region of the world”.

What effect, if anything, does this faux pas have on the tribe?


Source

1 Star2 Stars3 Stars4 Stars5 Stars (5.00 from 1 votes)
Loading ... Loading ...



Cool Math Facts #4

Learn another set of 5 cool math facts in a little over a minute from our new video!

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...



Quine’s paradox

Quine’s paradox is a paradox concerning truth values, attributed to Willard Van Orman Quine. It is related to the liar paradox as a problem, and it purports to show that a sentence can be paradoxical even if it is not self-referring and does not use demonstratives or indexicals (i.e. it does not explicitly refer to itself).

For another example, click here.

1 Star2 Stars3 Stars4 Stars5 Stars (5.00 from 1 votes)
Loading ... Loading ...



Paradox Tuesday – The Unstoppable Force Paradox

This paradox Tuesday we try to find an answer to the question: “What happens when an unstoppable force meets an immovable object?”

1 Star2 Stars3 Stars4 Stars5 Stars (3.00 from 2 votes)
Loading ... Loading ...



Why You Didn’t Die at Birth – Smarter Every Day 42 – YouTube

After living for 9 months in a liquid environment and then suddenly getting transported into the world by your mother, how do you think you made the transition? Find out from this great Smarter Every Day video.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...