From 7b2b09c42fdb8dfad2ffd208b9460a9235cc6119 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Sat, 26 Nov 2022 17:11:20 -0800 Subject: [PATCH] Sun just added more mass christmas fatass --- gamestate.c | 59 +++++++++++++++++++++++++++++++++++++++++------------ main.c | 2 +- types.h | 7 ++++--- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/gamestate.c b/gamestate.c index f566380..a14765a 100644 --- a/gamestate.c +++ b/gamestate.c @@ -1805,6 +1805,15 @@ float batteries_use_energy(GameState *gs, Entity *grid, float *energy_left_over, return energy_to_use; } +float sun_dist_no_gravity() +{ + // return (GRAVITY_CONSTANT * (SUN_MASS * mass / (distance * distance))) / mass; + // 0.01f = (GRAVITY_CONSTANT * (SUN_MASS / (distance_sqr))); + // 0.01f / GRAVITY_CONSTANT = SUN_MASS / distance_sqr; + // distance = sqrt( SUN_MASS / (0.01f / GRAVITY_CONSTANT) ) + return sqrtf( SUN_MASS / (GRAVITY_SMALLEST / GRAVITY_CONSTANT) ); +} + float entity_mass(Entity *m) { if (m->body != NULL) @@ -1818,27 +1827,44 @@ float entity_mass(Entity *m) } } -float sun_gravity_accel_at_point(V2 p, Entity *entity_with_gravity) +V2 sun_gravity_accel_for_entity(Entity *entity_with_gravity) { - if (V2length(V2sub(p, SUN_POS)) > SUN_NO_MORE_ELECTRICITY_OR_GRAVITY) - return 0.0f; + if (V2length(V2sub(entity_pos(entity_with_gravity), SUN_POS)) > sun_dist_no_gravity()) + return (V2){0}; V2 rel_vector = V2sub(entity_pos(entity_with_gravity), SUN_POS); float mass = entity_mass(entity_with_gravity); assert(mass != 0.0f); - float distance = V2length(rel_vector); + float distance_sqr = V2lengthsqr(rel_vector); // return (GRAVITY_CONSTANT * (SUN_MASS * mass / (distance * distance))) / mass; // the mass divides out - return (GRAVITY_CONSTANT * (SUN_MASS / (distance * distance))); + float accel_magnitude = (GRAVITY_CONSTANT * (SUN_MASS / (distance_sqr))); + V2 towards_sun = V2normalize(V2scale(rel_vector, -1.0f)); + return V2scale(towards_sun, accel_magnitude); +} + +void entity_set_velocity(Entity *e, V2 vel) +{ + assert(e->body != NULL); + cpBodySetVelocity(e->body, v2_to_cp(vel)); } void entity_ensure_in_orbit(Entity *e) { assert(e->body != NULL); - cpVect pos = v2_to_cp(V2sub(entity_pos(e), SUN_POS)); - cpFloat r = cpvlength(pos); - cpFloat v = cpfsqrt(sun_gravity_accel_at_point(cp_to_v2(pos), e) / r) / r; - cpBodySetVelocity(e->body, cpvmult(cpvperp(pos), v)); + V2 gravity_accel = sun_gravity_accel_for_entity(e); + if (V2length(gravity_accel) > 0.00f) + { + float dist = V2length(V2sub(entity_pos(e), SUN_POS)); + V2 orthogonal_to_gravity = V2normalize(V2rotate(gravity_accel, PI / 2.0f)); + V2 wanted_vel = V2scale(orthogonal_to_gravity, sqrtf(V2length(gravity_accel) * dist)); + + cpBodySetVelocity(e->body, v2_to_cp(wanted_vel)); + } + // cpVect pos = v2_to_cp(V2sub(entity_pos(e), SUN_POS)); + // cpFloat r = cpvlength(pos); + // cpFloat v = cpfsqrt(sun_gravity_accel_at_point(cp_to_v2(pos), e) / r) / r; + // cpBodySetVelocity(e->body, cpvmult(cpvperp(pos), v)); } V2 box_vel(Entity *box) @@ -1893,7 +1919,7 @@ void create_bomb_station(GameState *gs, V2 pos, enum BoxType platonic_type) BOX_AT_TYPE(grid, ((V2){-BOX_SIZE * 6.0, -BOX_SIZE * 2.0}), BoxExplosive); BOX_AT_TYPE(grid, ((V2){-BOX_SIZE * 6.0, -BOX_SIZE * 3.0}), BoxExplosive); BOX_AT_TYPE(grid, ((V2){-BOX_SIZE * 6.0, -BOX_SIZE * 5.0}), BoxExplosive); - + entity_ensure_in_orbit(grid); } @@ -2317,8 +2343,15 @@ void process(GameState *gs, float dt) if (e->body != NULL) { - cpVect g = cpvmult(pos_rel_sun, -sun_gravity_accel_at_point(entity_pos(e), e) / (sqdist * cpfsqrt(sqdist))); - cpBodyUpdateVelocity(e->body, g, 1.0f, dt); + + // cpVect g = cpvmult(pos_rel_sun, -sun_gravity_accel_at_point(entity_pos(e), e) / (sqdist * cpfsqrt(sqdist))); + // sun gravitational pull + V2 accel = sun_gravity_accel_for_entity(e); + V2 new_vel = entity_vel(gs, e); + new_vel = V2add(new_vel, V2scale(accel, dt)); + cpBodySetVelocity(e->body, v2_to_cp(new_vel)); + // cpBodySetVelocity(e->body, ) + // cpBodyUpdateVelocity(e->body, g, 1.0f, dt); } } @@ -2446,7 +2479,7 @@ void process(GameState *gs, float dt) cur_box->sun_amount = clamp01(V2dot(box_facing_vector(cur_box), V2normalize(V2sub(SUN_POS, entity_pos(cur_box))))); // less sun the farther away you are! - cur_box->sun_amount *= lerp(1.0f, 0.0f, clamp01(V2length(V2sub(entity_pos(cur_box), SUN_POS)) / SUN_NO_MORE_ELECTRICITY_OR_GRAVITY)); + cur_box->sun_amount *= lerp(1.0f, 0.0f, clamp01(V2length(V2sub(entity_pos(cur_box), SUN_POS)) / sun_dist_no_gravity())); energy_to_add += cur_box->sun_amount * SOLAR_ENERGY_PER_SECOND * dt; } } diff --git a/main.c b/main.c index 85c72b9..6b9b3f7 100644 --- a/main.c +++ b/main.c @@ -1956,7 +1956,7 @@ static void frame(void) draw_circle((V2){0}, INSTANT_DEATH_DISTANCE_FROM_SUN); set_color(BLUE); - draw_circle((V2){0}, SUN_NO_MORE_ELECTRICITY_OR_GRAVITY); + draw_circle((V2){0}, sun_dist_no_gravity()); } sgp_set_color(1.0f, 1.0f, 1.0f, 1.0f); diff --git a/types.h b/types.h index bfb492b..db5aa02 100644 --- a/types.h +++ b/types.h @@ -42,15 +42,15 @@ #define VISION_RADIUS 12.0f #define MAX_SERVER_TO_CLIENT 1024 * 512 // maximum size of serialized gamestate buffer #define MAX_CLIENT_TO_SERVER 1024 * 10 // maximum size of serialized inputs and mic data -#define GRAVITY_CONSTANT 5.0f +#define GRAVITY_CONSTANT 0.1f +#define GRAVITY_SMALLEST 0.01f // used to determine when gravity is clamped to 0.0f #define SUN_RADIUS 10.0f -#define SUN_NO_MORE_ELECTRICITY_OR_GRAVITY 200.0f #define INSTANT_DEATH_DISTANCE_FROM_SUN 2000.0f #define SUN_POS ((V2){50.0f, 0.0f}) #ifdef NO_GRAVITY #define SUN_MASS 0.0f #else -#define SUN_MASS 1000000.0f +#define SUN_MASS (10000.0f) #endif #define SOLAR_ENERGY_PER_SECOND 0.09f #define DAMAGE_TO_PLAYER_PER_BLOCK 0.1f @@ -417,6 +417,7 @@ void process_fixed_timestep(GameState *gs); void process(struct GameState *gs, float dt); // does in place Entity *closest_box_to_point_in_radius(struct GameState *gs, V2 point, float radius, bool (*filter_func)(Entity *)); uint64_t tick(struct GameState *gs); +float sun_dist_no_gravity(); // all of these return if successful or not bool server_to_client_serialize(struct ServerToClient *msg, unsigned char *bytes, size_t *out_len, size_t max_len, Entity *for_this_player, bool to_disk);