247: GameDev: 1.1 Plus 2.2 Is Not 3.3. What?

Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categorie:

Be careful with floating point numbers when building games. You could be in for some big surprises if you don’t understand how computers work with fractional values. You might be able to write an app to keep track of your friend’s addresses without using any floating point numbers. But most games will need math that uses fractional values. That is if you want any kind of game with smooth motion. If your game consist of typing letters in words, then maybe you won’t have to worry about this. But if you want those letters to move slowly across the screen as the user drags them into place, then you’ll need to use fractional values. You might think that you can get away from this by just having really small whole numbers. Maybe you track individual pixels. The problem is that computers are really fast these days and can do a lot of work even between pixels. You might find that the computer tries to move the image by 1 thousandth of a pixel. If you only work with whole numbers, then this becomes zero. And your image will be stuck without moving at all because the computer is so fast that it’s always trying to move it by a tiny amount. Instead of limiting yourself to whole numbers, you’ll need to understand how to use fractional values. And that means you need to understand how to use floating point numbers. You should also listen to episode #112 about the float data type, or you can read the full transcript below. I try not to repeat information between episodes so you’ll find both this episode and the earlier one have something for you. Transcript You could be in for some big surprises if you don’t understand how computers work with fractional values. You might be able to write an app to keep track of your friend’s addresses without using any floating point numbers. But most games will need math that uses fractional values. That is if you want any kind of game with smooth motion. If your game consist of typing letters in words, then maybe you won’t have to worry about this. But if you want those letters to move slowly across the screen as the user drags them into place, then you’ll need to use fractional values. You might think that you can get away from this by just having really small whole numbers. Maybe you track individual pixels. The problem is that computers are really fast these days and can do a lot of work even between pixels. You might find that the computer tries to move the image by 1 thousandth of a pixel. If you only work with whole numbers, then this becomes zero. And your image will be stuck without moving at all because the computer is so fast that it’s always trying to move it by a tiny amount. Instead of limiting yourself to whole numbers, you’ll need to understand how to use fractional values. And that means you need to understand how to use floating point numbers. They’re called floating point because the decimal point that separates the whole number portion from the fractional number portion can move around as needed. Actually, I completely made up that last part. It seems reasonable to me and this is how I’ve always thought of floating point numbers. But I really have no idea where the name comes from. So I don’t repeat earlier content, you’ll want to listen to episode #112 where I described the float data type. In this episode, I just wanted to bring the topic to your attention again. Especially, since I’m describing game development topics and some of the math you’ll need to use. Early computers were really good with adding, subtracting, multiplying, and dividing whole numbers. They still are. You can represent all the values 0 up through the maximum value without missing any numbers. I mean, it would be really bad if computers, say, had trouble with 8. To a computer, all the numbers are just a series of binary 1’s and 0’s. They can even represent negative numbers through something

Visit the podcast's native language site