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