Decided i wanted some physics in my game, without actually using a physics engine (to keep it from getting ”floaty”). Made a function to handle 2 entities slamming into eachother, calculating the speed and direction in x-coordinates after impact and outputting a timer for both objects to be in the ”pushed” state and then make an easing equation for moving them.
ONTO THE CODE!
///scr_entity_collision(mass_a, spd_a, obj_a, mass_b, spd_b, obj_b)
//Handle collisions between objects
//Should be run after collision is checked in movement-states for each interacting entity
//Input movement should be gathered during step, before new collision movespeed resolves.
//GET input args
mass_a =argument0;
spd_a =argument1;
obj_a =argument2;
mass_b =argument3;
spd_b =argument4;
obj_b =argument5;
//Set time-variable t to be local only
coll_t_passed=0;
coll_t_max=1;
//Calculate impact impulse on collision between object a and b
//Object A
// Formula is: obj_a_exit_velocity = ((obj_a_mass-obj_b_mass)/(obj_a_mass+obj_b_mass)*obj_a_initial_velocity)+((obj_a_mass-obj_b_mass)/(obj_a_mass+obj_b_mass)*obj_b_initial_velocity)
obj_a_exit_speed = ((mass_a-mass_b)/(mass_a+mass_b)*spd_a)+((mass_a-mass_b)/(mass_a+mass_b)*spd_b);
//Object B
// Formula is: obj_b_exit_velocity = ((2*obj_a_mass)/(obj_a_mass+obj_b_mass)*obj_a_initial_velocity)-((obj_a_mass-obj_b_mass)/(obj_a_mass+obj_b_mass))
obj_b_exit_speed = ((2*mass_a)/(mass_a+mass_b)*spd_a)-((mass_a-mass_b)/(mass_a+mass_b));
//Apply 10% elasticity reduction per time unit to forces, if needed
//obj_a_exit_speed=(obj_a_exit_speed/10)*9
//obj_b_exit_speed=(obj_b_exit_speed/10)*9
//Calculate time for easing function to stop
coll_t_max_a=10*(spd_a-obj_a_exit_speed);
coll_t_max_b=10*(spd_b-obj_b_exit_speed);
//Calculate magnitude of hit as percentage of max runspeed, for screenshake or something i guess
obj_a_magnitude = obj_a_exit_speed / 15 ;
obj_b_magnitude = obj_b_exit_speed / 15 ;
//Calculate distance moved
//Dont forget to add a timer for each exit-object that checks if the other object (a/b) has reached max, if it has, AND current one does, reset timer, otherwise if max timer is hit, just switch state
obj_a_dist=obj_a_exit_speed*coll_t_max_a;
obj_b_dist=obj_b_exit_speed*coll_t_max_b;
//Calculate exit speedfunction and duration for A and B, apply to arrays
//Easing functions requires timer to increase coll_t_passed for each frame, and the state to end when col_t_passed = col_t_max
obj_a_spdfunction=obj_a_dist*(coll_t_passed*coll_t_passed*coll_t_passed*coll_t_passed*coll_t_passed+1);
obj_b_spdfunction=obj_a_dist*(coll_t_passed*coll_t_passed*coll_t_passed*coll_t_passed*coll_t_passed+1);