X-Git-Url: https://deadsoftware.ru/gitweb?p=netwar.git;a=blobdiff_plain;f=game.c;h=3acaa36a82a9a558f5501e349e5bc29c7e31171b;hp=36dccb41669cf628070979921560f8fdf93ec3d2;hb=f23bc975650813d88a819d403e1b9e1795086242;hpb=0afebb148532e32e6b47194e08e908beec45087e diff --git a/game.c b/game.c index 36dccb4..3acaa36 100644 --- a/game.c +++ b/game.c @@ -13,6 +13,11 @@ #define MAX_SPEED (0.3 / TICK) #define MAX_ROTATE (0.3 / TICK) +// Цифры взяты с потолка, но вроде неплохо подобраны +#define G 3.673e-4 +#define M_STAR 10.0 +#define M_SHIP 0.1 + Player g_player[MAX_PLAYERS]; Bullet g_bullet[MAX_BULLETS]; @@ -95,9 +100,17 @@ static void checkspacebound(float * a) { *a = x; } +static float length(float x1, float y1, float x2, float y2) { + return sqrtf(powf(x1 - x2, 2) + powf(y1 - y2, 2)); +} + static bool collide(float x1, float y1, float r1, float x2, float y2, float r2) { - float l = sqrtf(powf(x1 - x2, 2) + powf(y1 - y2, 2)); - return ((l - r1 - r2) <= 0); + return ((length(x1, y1, x2, y2) - r1 - r2) <= 0); +} + +static void gravity(float * vx, float * vy, float x, float y) { + *vx += copysign(G * M_STAR * M_SHIP / length(0, 0, x, y), x * -1); + *vy += copysign(G * M_STAR * M_SHIP / length(0, 0, x, y), y * -1); } void g_player_does(unsigned int id, DoesBits code) { @@ -122,12 +135,18 @@ void g_update() { g_player[i].x += g_player[i].vx; g_player[i].y += g_player[i].vy; g_player[i].r += g_player[i].vr; + + gravity(&g_player[i].vx, &g_player[i].vy, g_player[i].x, g_player[i].y); + checkspacebound(&g_player[i].x); checkspacebound(&g_player[i].y); ++g_player[i].shoot; if(g_player[i].shoot > PLAYER_SHOOT) g_player[i].shoot = PLAYER_SHOOT; + + if(collide(g_player[i].x, g_player[i].y, PLAYER_SIZE, 0, 0, STAR_SIZE)) + g_player[i].live = false; } }