randomize ball launch velocity

This commit is contained in:
Zack Buhman 2025-12-05 13:35:19 -06:00
parent 5299fa66af
commit 8468789a34
3 changed files with 35 additions and 9 deletions

View File

@ -6,7 +6,7 @@
extern "C" {
#endif
void launch_ball(struct game_state * state);
void launch_ball(struct game_state * state, double time);
void reset_level(struct game_state * state);
void update(struct game_state * state, double time);

View File

@ -217,8 +217,10 @@ int main()
}
}
double time = glfwGetTime();
if (!last_x_press && x_press) {
launch_ball(&state);
launch_ball(&state, time);
}
last_x_press = x_press;
@ -229,7 +231,6 @@ int main()
if (state.paddle_x > 12 - extent)
state.paddle_x = 12 - extent;
double time = glfwGetTime();
update(&state, time);
glEnable(GL_BLEND);

View File

@ -12,16 +12,43 @@
#include "math/float_types.hpp"
#include "math/transform.hpp"
void launch_ball(struct game_state * state)
static uint64_t xorshift_state = 0x1234567812345678;
uint64_t xorshift64(uint64_t x)
{
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
return x;
}
void launch_ball(struct game_state * state, double time)
{
if (state->balls_launched >= MAX_BALLS)
return;
if (xorshift_state == 0x1234567812345678) {
double timed = time;
uint64_t timei = *((uint64_t *)&timed);
xorshift_state = xorshift64(xorshift_state + timei);
}
xorshift_state = xorshift64(xorshift_state);
uint64_t rx = (xorshift_state >> 0) & 0xffffffff;
double orientation = rx;
double x = cos(orientation) - sin(orientation);
double y = sin(orientation) + cos(orientation);
vec2 d = normalize(vec2(x, y)) * 0.2f;
if (d.y < 0.1)
d.y = 0.1;
if (fabsf(d.x) > 0.05)
d.x = 0.05f * fabsf(d.x) / d.x;
struct ball_state& ball = state->balls[state->balls_launched];
ball.ball_x = state->paddle_x;
ball.ball_y = 25.0;
ball.ball_dx = 0.1;
ball.ball_dy = -0.1;
ball.ball_y = 25.0f;
ball.ball_dx = d.x;
ball.ball_dy = -d.y;
state->balls_launched += 1;
}
@ -31,8 +58,6 @@ void reset_level(struct game_state * state)
state->paddle_x = 0.0;
state->paddle_y = 26.0;
launch_ball(state);
state->start_time = 0.0;
assert(src_level_level1_data_size == 13 * 28);