home *** CD-ROM | disk | FTP | other *** search
- <head>
- <title="...forever...">
- <font=monaco10.fnt>
- <font=newy36.fnt>
- <font=time24.fnt>
- <image=back.raw w=256 h=256 t=-1>
- <buf=3090>
- <bgcolor=-1>
- <background=0>
- <link_color=000>
- <module=console.mod>
- <pal=back.pal>
- colors:
- 251 - black
- </head>
- <body>
- <frame x=0 y=0 w=640 h=3090 b=-1 c=-1>
-
-
- -- - -- --------------------------
- <f1><c000> Math in games <f0>
- ----------------------- -- -- - --
-
- dirty tricks used since the Stone Age by game programmers
- compiled by Saulot/ Nokturnal/ Chosneck Magazine
-
-
- NUMBERS
- ------------------
- Integers
- When storing objects coordinates or values of other kind as integers we lack
- precision. The following trick is handy when, during sprite animation, we want
- to move given sprite less than one pixel per frame or move more than one pixel
- per frame. Do NOT use pauses in your code! All of this can be done by scaling
- the number system you are using. For example you can divide the horizontal
- position by 100 before drawing the sprite on the screen. This forces the sprite
- to be drawn once per two frames. In this manner you can achieve wide range of
- velocities.
-
- Fixed point math
- Dividing of the integer by scaling ratio is rather CPU hungry operation. The
- same thing can be done in another way. If scaling ratio is the power of 2, then
- dividing process can be achieved by simple bit shifting operations. Fixed point
- numbers are simple integers which represent both integer part and fraction
- separated with binary point (exactly like decimal point, but in binary system)
- on the earlier specified, constant position. When programmers are talking about
- fast integer graphics, they often have in mind fixed point numbers. Most
- frequently used powers of 2 are 256 and 65536, but others are also used. They
- can be very efficient on many systems. Naming convention of fixed point numbers
- covers the amount of bits for integer and fraction part separated by binary
- point:
- 12.4 - 12 bits of integer part
- 4 bits of fraction part
- This gives us the range (unsigned) of 0-4095.9375 with step 0.0625 (1/16 or
- 2^(-4)).
-
- Addition and substraction is quite simple. We can operate on them like on
- integers (they only have to be compatible, that means that they must have the
- same scaling ratio). If we want to multiply the fixedpoint numbers, we must
- perform a logical shift to the right before any action.
-
- As a good introduction to fixed point math you should read the article about
- fast interpolation for m68k processors by Dynacore /TSCC.
-
- Integers - flexible ranges
- One of major disadvantages when using the fixed point numbers is that they lack
- precision in wide range of numbers: they cannot hold very small or very big
- numbers and with very small numbers they are simply inaccurate. To get around
- this the floating point numbers are used. The floating point number consists of
- signed exponent which describes the shift of the whole range and mantissa (a
- signed value).
- We can transform the floating point number to fixed point number by shifting of
- the mantissa by a amount of bits specified in exponent (to the left for
- positive exponent and to the right for negative one). The floating point
- numbers are supported in hardware by external, math coprocessors like MC68881,
- MC68882. The Motorola 68040 and 68060 (not all versions) possess their own,
- built-in, internal coprocessors. The use of coprocessors on Atari's (maybe
- excluding those equipped with 68060) should be cautious, because their aren't
- lightning fast and under some circumstances can slow the things down. But they
- are invaluable for table's precalculations and they are very accurate. The
- programming languages like C/C++ support float (32 bits), double (64 bits) and
- long double (80 bits). The machines without coprocessors can also use the
- floating point numbers, but all the operations are handled by software and not
- hardware, so it slows down everything considerably.
-
- DISCRETENESS
- ---------------------------
- Discreteness is a way of talking about values in computer games which in
- most cases are integers. The time isn't continuous, it's divided on frames
- 1/50 per second for PAL systems (1/60 for NTSC). Sprite positions aren't
- continuous too, they are displayed on the pixel boundary. Using of
- float/fixed point numbers for storing object coordinates means that they are
- stored with greater accuracy than they are displayed on screen. When we are
- scrolling the whole screen, we have to stop it's movement, if this movement
- turns out to be too small (for example less than 1/8 pixel per frame) to
- avoid flickering. To take into account spatial discreetnes we should stop the
- screen scrolling rather in half of pixel, rather than on the edge. When
- the scrolling will start from the beginning, it will have the chance to move in
- both directions (in one or second way).
-
- Temporal discreetness - game frames
- The conception of game frames is fundamental for traditional games programming.
- Of course there is possibility of writing asynchronous driven games, but most
- games depend on master clock's ticks occuring fixed amount of times per second.
- Each tick, the game executes one cycle of thinking. Each object can move a
- little, some decisions can be made, the screen can be refreshed. Operations
- which you were used to do normally in one go have to be performed in
- step-by-step manner. How it will be when you will "miss the frame" and game
- will take much more than 1 frame of game cycle? (in most cases it is te fault
- of overloaded graphics routines). How to catch-up and avoid the game slowing
- down? There are two methods: launching the game engine several times and
- scaling of game speed.
-
- The first method is quite simple - you measure the amount of missed frames,
- then launch the game code adequate amount of frames to catch things up. This
- works fine, because game routines are much quickier than routines responsible
- for display. If game code is very complex or game works on a slow machine this
- method won't work. Solution like this is used succesfully on consoles.
-
- Next method (it works on slower machines) is based on measuring how long each
- frame will take and multiplying the speed of each game by adequate number. This
- method is used on PCs, that's because they vary between themselves in CPU
- speed.
-
- In modern games, the game frame isn't the same as dispaying frame. Game is
- going on in a thread separated from displaying code and in this manner
- shouldn't be affected by displaying code, slowed down by too big amount of
- objects or too complex scene. To track down the eventual problems a different
- approach is needed, similar to launching game engine several times, but it
- requires a lot of additional thought. Shared variables, those written by game
- code and read by part responsible for display should be double buffered to
- wipe-out problems with synchronisation.
-
- GEOMETRY
- --------
- Classical geometry or Euclidean geometry is based on right triangle and it's
- angles. In following examples the theta will describe the main angle and phi
- any other angles. There are two vertices called A and B. There are three sides:
- 2 x adjacent, and the longest side - hypotenuse.
-
- *
- |/ *
- | phi *
- | *
- | *
- |.) 90ן theta / *
- +------------------------*
-
- theta + phi + 90ן = 180ן
- phi = 90ן - theta
-
- That means that one angle can be calculated from the other. We can be specified
- by the lenght of one or two sides. That leads us to two systems of coordinates:
- polar and carthesian. (about them later)
-
- TRIGONOMETRY - those sinuses and cosinuses
- What does it mean "trigonometric functions" ? Simply speaking they are ratio of
- right-triangle sides near/ opposite to specified angle. Calculating of those
- functions is quite CPU time consuming task (it's stupid dividing after all),
- then the best solution is to precalculate and store all the values in look-up
- table. Those functions are becoming handy when translating between classic
- carthesian and polar coordinates.
-
- Distance calculation
- Pythagorean theorem tells that square of hypotenuse is equal to sum of squares
- of legs of right triangle. It is very useful in computer games, because
- accurate calculation of distance between two points is possible.
-
- Let's say that we have object A at (x1,y2) and object B at (x2,y2). According
- to Pythagorean theorem, the distance between A and B is equal to
- ((x2-x1)²+(y2-y1)²). If you know how to calculate square roots then this method
- is well suited for you. Specifying of the angle between two points is a real
- nightmare, because there are four possibilities depending in which quarter the
- adequate angle is located. If you know the angle thetha (e.g. you calculated it
- earlier), you can calculate the distance between two points by dividing of the
- adjacent side (near thetha angle) by cos(thetha)(if thetha<45ן) or dividing
- lenght of right triangle's leg (opposite to thetha angle) by sin(thetha)(if
- thetha>45ן).
-
- Systems of coordinates
- Concept of space in computer games requires numeric measure for marking
- position and directions. There are many solutions, but few of them are used.
-
- Carthesian coordinates
- It's most frequently used system. Carthesian coordinates consist of values
- horizontal (X), vertical (Y) or for 3D games depth (Z axis). Between each axis
- there is an 90ן angle. Coordinates can be represented as a vector [X,Y,Z],
- which is displayed horizontally while using it to multiplying matrixes.
-
- Polar coordinates
- Polar coordinates consist of angle and scalar value. They can be useful when
- storing the velocities of objects in 2D games (in sport games for example). The
- person can run in specified direction and change the angle by specified amount
- in each frame, maintaining the speed at constant level. We can transform polar
- coordinates to carthesian system by multiplying scalar by sinus or cosinus of
- negated angle to get proper horizontal and vertical coordinates. (angle 0 is
- North, 90 is EAST). Angles can be stored in different formats. Circle consists
- of 2*pi radians. Sailors use 360 degrees, french dictators 400 grad, and coders
- 256 units, because it fits into one byte.
-
- Quaternions 3D rotations that work
- Quaternions are 4 dimensional numbers (exactly like complex numbers are 2
- dimensional). They have a set of mathematical operations tied only for them
- like addition, substraction, multiply etc. They are very useful with 3D
- rotations. In "Descent" for example you can rotate around axis, no matter in
- what direction you face. The quaternions let you do things like that.
-
- <link=art53b.scr>Go to NEXT PART</l>
- </frame>
- </body>
-