A matematicas nos games digitais

A matemática é fundamental para a programação e conseqüentemente para o desenvolvimento dos games. Por meio da programação podemos criar formas para o game. Para desenhar um circulo temos:
void draw_circle ()
{
int x, y;
int length = 50;
float angle = 0.0;
float angle_stepsize = 0.1;

// go through all angles from 0 to 2 * PI radians
while (angle < 2 * PI)
{
// calculate x, y from a vector with known length and angle
x = length * cos (angle);
y = length * sin (angle);

putpixel (screen,
x + SCREEN_W / 2, y + SCREEN_H / 2,
makecol (255, 255, 255));
angle += angle_stepsize;

Esse é um exemplo de trigonometria aplicada a games eletrônicos. O código acima contem seno e coseno, o resultado desse código é a imagem abaixo.

Para dar uma idéia melhor o código abaixo representa o seno:
void draw_sine ()
{
int length = 50;
fixed x, y;
fixed angle = 0;
fixed angle_stepsize = itofix (5);

while (fixtoi(angle) < 256)
{
// the angle is plotted along the x-axis
x = angle;
// the sine function is plotted along the y-axis
y = length * fsin (angle);

putpixel (screen,
fixtoi (x), fixtoi (y) + SCREEN_H / 2,
makecol (255, 255, 255));

angle += angle_stepsize;
}
}

A figura abaixo representa o código:

Abaixo temos representadas as curvas seno e coseno juntas:

Todos esses conceitos aplicados a um jogo real de corrida temos:
void racing_car ()
{
// length and angle of the racing car's velocity vector
fixed angle = itofix (0);
fixed length = itofix (0);
// x- and y-coordinates of the velocity vector
fixed vel_x, vel_y;

// x- and y-position of the racing car
fixed x = itofix (SCREEN_W / 2);
fixed y = itofix (SCREEN_H / 2);

while (!key[KEY_ESC])
{
// erase the old image
circlefill (screen, fixtoi(x), fixtoi(y), 10, makecol (0, 0, 0));

// check the keys and move the car
if (key[KEY_UP] && length itofix (0))
length -= ftofix (0.005);
if (key[KEY_LEFT])
angle = (angle – itofix (1)) & 0xFFFFFF;
if (key[KEY_RIGHT])
angle = (angle + itofix (1)) & 0xFFFFFF;

// calculate the x- and y-coordinates of the velocity vector
vel_x = fmul (length, fcos (angle));
vel_y = fmul (length, fsin (angle));

// move the car, and make sure it stays within the screen
x += vel_x;
if (x >= itofix (SCREEN_W)) x -= itofix(SCREEN_W);
if (x = itofix (SCREEN_H)) y -= itofix(SCREEN_H);
if (y w – 1);
int mask_y = (tile->h – 1);

// step for points in space between two pixels on a horizontal line
fixed line_dx, line_dy;

// current space position
fixed space_x, space_y;

for (screen_y = 0; screen_y h; screen_y++)
{
// first calculate the distance of the line we are drawing
distance = fmul (params.space_z, params.scale_y) /
(screen_y + params.horizon);
// then calculate the horizontal scale, or the distance between
// space points on this horizontal line
horizontal_scale = fdiv (distance, params.scale_x);

// calculate the dx and dy of points in space when we step
// through all points on this line
line_dx = fmul (-fsin(angle), horizontal_scale);
line_dy = fmul (fcos(angle), horizontal_scale);

// calculate the starting position
space_x = cx + fmul (distance, fcos(angle)) – bmp->w/2 * line_dx;
space_y = cy + fmul (distance, fsin(angle)) – bmp->w/2 * line_dy;

// go through all points in this screen line
for (screen_x = 0; screen_x w; screen_x++)
{
// get a pixel from the tile and put it on the screen
putpixel (bmp, screen_x, screen_y,
getpixel (tile,
fixtoi (space_x) & mask_x,
fixtoi (space_y) & mask_y));
// advance to the next position in space
space_x += line_dx;
space_y += line_dy;

Bibliografia
http://www.unidev.com.br/phpbb3/viewtopic.php?f=61&t=41616
http://www.helixsoft.nl/articles/circle/sincos.htm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: