Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // form equation of line of the rail
- float rail_slope = rail_dir.y / rail_dir.x;
- // y - y1 = m*(x - x1) => y = m*x + (m*-x1 + y1)
- // y = m*x + b
- float rail_y_int = rail_slope * -a_point_on_rail.x + a_point_on_rail.y; // y-intercept, or |b|
- // form equation of line of translation
- float translation_slope = - 1 / rail_slope; // a perpendicular line's slope is the opposite reciprocal
- // the player will always be on the line of translation
- float translation_y_int = translation_slope * -player_pos.x + player_pos.y;
- // now find the intersection between the two lines, which is the new location of the player
- // f(x) = m1*x + b1; g(x) = m1*x + b1;
- // intersection: m1*x + b1 = m2*x + b2 => m1*x - m2*x = b2 - b1
- // => x (m1 - m2) = b2 - b1 => x = (b2 - b1) / (m1 - m2);
- // y = m1*x
- float snapped_x = (translation_y_int - rail_y_int) / (rail_slope - translation_slope);
- float snapped_y = rail_slope * snapped_x;
- player_pos = new Vector2(snapped_x, snapped_y);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement