Проблема с физикой в Арканоиде
Недавно занялся по гайдам делать арканоид. Возникла проблема с физикой шарика. Функция automoveBall() должна определять местоположение шарика в пространстве и заставлять его отталкиваться от стен, но что то пошло не так и шарик просто скользит по стенке до угла и остается там.
#include <stdio.h>
#include <Windows.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
const double M_PI = 3.14;
#define shirina 65
#define visota 25
typedef struct
{
int x, y;
int w;
}TRacket;
typedef struct
{
float x, y;
int ix, iy;
float alfa;
float speed;
}TBall;
char mass[visota][shirina + 1];
TRacket racket;
TBall ball;
void moveBall(int x, int y)
{
ball.x = x;
ball.y = y;
ball.ix = (int)round(ball.x);
ball.iy = (int)round(ball.y);
}
void initBall()
{
moveBall(2, 2);
ball.alfa = -2;
ball.speed = 1;
}
void putBall()
{
mass[ball.iy][ball.ix] = '*';
}
void automoveBall()
{
TBall bl = ball;
if (ball.alfa < 0) ball.alfa += (M_PI * 2);
if (ball.alfa > (M_PI * 2))ball.alfa -= (M_PI * 2);
moveBall(ball.x + cos(ball.alfa) * ball.speed, ball.y + sin(ball.alfa) * ball.speed);
if ((mass[ball.iy][ball.ix] == '#') || (mass[ball.iy][ball.ix] == '@'))
{
if ((ball.ix != bl.ix) && (ball.iy != bl.iy))
{
if (mass[bl.iy][ball.ix] == mass[ball.iy][bl.ix])
bl.alfa = (bl.alfa + M_PI);
else
{
if (mass[bl.iy][ball.ix] == '#')
bl.alfa = (2 * M_PI - bl.alfa) + M_PI;
else
bl.alfa = (2 * M_PI - bl.alfa);
}
}
else if (ball.iy == bl.iy)
bl.alfa = (2 * M_PI - bl.alfa) + M_PI;
else
bl.alfa = (2 * M_PI - bl.alfa);
ball = bl;
automoveBall();
}
}
void initRacket()//Данные ракетки
{
racket.w = 12;
racket.x = (shirina - racket.w) / 2;
racket.y = visota - 1;
}
void PutRacket()//Появление ракетки
{
for (int i = racket.x; i < racket.x + racket.w; i++)
mass[racket.y][i] = '@';
}
void init()//Карта
{
for (int i = 0; i < shirina; i++)
mass[0][i] = '#';
mass[0][shirina] = '\0';
strncpy_s(mass[1], mass[0], shirina + 1);
for (int i = 1; i < shirina-1; i++)
mass[1][i] = ' ';
for (int i = 2; i < visota; i++)
strncpy_s(mass[i], mass[1], shirina+1);
}
void show() //Отображение карты
{
for (int i = 0; i < visota; i++)
{
printf("%s", mass[i]);
if (i < visota - 1)printf("\n");
}
}
void moveRacket(int x)
{
racket.x = x;
if (racket.x < 1)
racket.x = 1;
if (racket.x + racket.w >= shirina)
racket.x = shirina - 1 - racket.w;
}
void setcur(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO structCursorInfo;
GetConsoleCursorInfo(handle, &structCursorInfo);
structCursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(handle, &structCursorInfo);
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main()//Игра
{
char put;
BOOL run = FALSE;
//pres();
// pres1();
initRacket();
initBall();
do
{
setcur(0, 0);
if (run)
automoveBall();
init();
PutRacket();
putBall();
show();
if (GetKeyState('A')<0)moveRacket(racket.x - 1);
if (GetKeyState('D')<0)moveRacket(racket.x + 1);
if (GetKeyState('W') < 0)run=TRUE;
if(!run)
moveBall(racket.x + racket.w / 2, racket.y-1 );
Sleep(10);
}
while (GetKeyState (VK_ESCAPE)>=0);
return(0);
}