yaaaa rebota todo
This commit is contained in:
@@ -18,6 +18,7 @@ Ball::Ball(float x, float vx, float vy, color_t color, Texture *texture)
|
|||||||
g = GRAVITY;
|
g = GRAVITY;
|
||||||
onFloor = false;
|
onFloor = false;
|
||||||
stopped = false;
|
stopped = false;
|
||||||
|
loss = ((rand() % 30) * 0.01f) + 0.6f;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
@@ -37,16 +38,16 @@ void Ball::update()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualiza la posición
|
// Aplica la gravedad a la velocidad
|
||||||
x += vx;
|
if (!onFloor && (y - SCREEN_HEIGHT) < BALL_SIZE * 2)
|
||||||
y += vy;
|
|
||||||
|
|
||||||
// Aplica la gravedad
|
|
||||||
if (!onFloor)
|
|
||||||
{
|
{
|
||||||
vy += g;
|
vy += g;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Actualiza la posición en función de la velocidad
|
||||||
|
x += vx;
|
||||||
|
y += vy;
|
||||||
|
|
||||||
// Comprueba las colisiones con el lateral izquierdo
|
// Comprueba las colisiones con el lateral izquierdo
|
||||||
if (x < 0)
|
if (x < 0)
|
||||||
{
|
{
|
||||||
@@ -69,13 +70,11 @@ void Ball::update()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Comprueba las colisiones con la parte inferior
|
// Comprueba las colisiones con la parte inferior
|
||||||
// if (y + h > SCREEN_HEIGHT)
|
if (y + h > SCREEN_HEIGHT)
|
||||||
if ((y + h > SCREEN_HEIGHT) && (vy > 0))
|
|
||||||
{
|
{
|
||||||
// y = SCREEN_HEIGHT - h;
|
y = SCREEN_HEIGHT - h;
|
||||||
vy = -vy * LOSS;
|
vy = -vy * loss;
|
||||||
std::cout << vy << std::endl;
|
if (abs(vy) < 0.1f)
|
||||||
if (abs(vy) < 0.5f)
|
|
||||||
{
|
{
|
||||||
vy = 0.0f;
|
vy = 0.0f;
|
||||||
onFloor = true;
|
onFloor = true;
|
||||||
@@ -90,7 +89,7 @@ void Ball::update()
|
|||||||
{
|
{
|
||||||
vx = 0.0f;
|
vx = 0.0f;
|
||||||
stopped = true;
|
stopped = true;
|
||||||
exit(0);
|
//exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ private:
|
|||||||
color_t color; // Color de la pelota
|
color_t color; // Color de la pelota
|
||||||
bool onFloor; // Indica si la pelota está ya en el suelo
|
bool onFloor; // Indica si la pelota está ya en el suelo
|
||||||
bool stopped; // Indica si la pelota ha terminado de moverse;
|
bool stopped; // Indica si la pelota ha terminado de moverse;
|
||||||
|
float loss; // Coeficiente de rebote. Pérdida de energía en cada rebote
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#define SCREEN_HEIGHT 240
|
#define SCREEN_HEIGHT 240
|
||||||
#define BALL_SIZE 8
|
#define BALL_SIZE 8
|
||||||
#define GRAVITY 0.2f
|
#define GRAVITY 0.2f
|
||||||
#define LOSS 0.75f
|
|
||||||
|
|
||||||
struct color_t
|
struct color_t
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,14 +16,14 @@ int test[5] = {1, 10, 100, 500, 1000};
|
|||||||
bool shouldExit = false;
|
bool shouldExit = false;
|
||||||
Uint32 ticks = 0;
|
Uint32 ticks = 0;
|
||||||
|
|
||||||
void initBalls()
|
void initBalls(int value)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < test[0]; ++i)
|
for (int i = 0; i < test[value]; ++i)
|
||||||
{
|
{
|
||||||
const int sign = ((rand() % 2) * 2) - 1;;
|
const int sign = ((rand() % 2) * 2) - 1;;
|
||||||
const float x = SCREEN_WIDTH / 2 - BALL_SIZE / 2;
|
const float x = (rand()%(SCREEN_WIDTH/2)) + (SCREEN_WIDTH / 4);
|
||||||
const float vx = (((rand() % 20) + 10) * 0.1f) * sign;
|
const float vx = (((rand() % 20) + 10) * 0.1f) * sign;
|
||||||
const float vy = ((rand() % 40) - 20) * 0.1f;
|
const float vy = ((rand() % 60) - 30) * 0.1f;
|
||||||
const color_t color = {(rand() % 192) + 32, (rand() % 192) + 32, (rand() % 192) + 32};
|
const color_t color = {(rand() % 192) + 32, (rand() % 192) + 32, (rand() % 192) + 32};
|
||||||
Ball *b = new Ball(x, vx, vy, color, texture);
|
Ball *b = new Ball(x, vx, vy, color, texture);
|
||||||
balls.push_back(b);
|
balls.push_back(b);
|
||||||
@@ -88,7 +88,7 @@ bool init()
|
|||||||
texture = new Texture(renderer, "resources/pelota.png");
|
texture = new Texture(renderer, "resources/pelota.png");
|
||||||
ticks = SDL_GetTicks();
|
ticks = SDL_GetTicks();
|
||||||
srand (time(NULL));
|
srand (time(NULL));
|
||||||
initBalls();
|
initBalls(2);
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user