Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. // form equation of line of the rail
  2. float rail_slope = rail_dir.y / rail_dir.x;
  3. // y - y1 = m*(x - x1) => y = m*x + (m*-x1 + y1)
  4. // y = m*x + b
  5. float rail_y_int = rail_slope * -a_point_on_rail.x + a_point_on_rail.y; // y-intercept, or |b|
  6.  
  7. // form equation of line of translation
  8. float translation_slope = - 1 / rail_slope; // a perpendicular line's slope is the opposite reciprocal
  9. // the player will always be on the line of translation
  10. float translation_y_int = translation_slope * -player_pos.x + player_pos.y;
  11.  
  12. // now find the intersection between the two lines, which is the new location of the player
  13. // f(x) = m1*x + b1; g(x) = m1*x + b1;
  14. // intersection: m1*x + b1 = m2*x + b2 => m1*x - m2*x = b2 - b1
  15. // => x (m1 - m2) = b2 - b1 => x = (b2 - b1) / (m1 - m2);
  16. // y = m1*x
  17. float snapped_x = (translation_y_int - rail_y_int) / (rail_slope - translation_slope);
  18. float snapped_y = rail_slope * snapped_x;
  19. player_pos = new Vector2(snapped_x, snapped_y);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement