ShaunJS

Fractional Collisions / Pixel Perfect

Mar 15th, 2016
6,305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //decimal fractions of hsp are stored in hsp_f
  2. //every frame this fraction is added back in and the new fraction removed again
  3. //this moves us in whole integers each frame that are still representative of decimal movement
  4. //for example, at a hsp of 0.1 you would move 1 pixel every 10 frames.
  5. //this resolves any issues of not detecting a collision at a fractional target (ie: no collision at 3.8, when there is at 4.0)
  6. //which sometimes caused the player after rounding, to be drawn overlapping walls by 1 pixel.
  7.  
  8. hsp_final = hsp + hsp_f;
  9. hsp_f = hsp_final - floor(abs(hsp_final))*sign(hsp_final);
  10. hsp_final -= hsp_f;
  11.  
  12. vsp_final = vsp + vsp_f;
  13. vsp_f = vsp_final - floor(abs(vsp_final))*sign(vsp_final);
  14. vsp_final -= vsp_f;
  15.  
  16. //same old place_meeting collision code.
  17. if (place_meeting(x+hsp_final,y,par_collide))
  18. {
  19.     var inc = sign(hsp_final);
  20.     while (!place_meeting(x+inc,y,par_collide)) x+= inc;
  21.     hsp_final = 0;
  22.     hsp = 0;
  23. }
  24. x+=hsp_final;
  25.  
  26. if (place_meeting(x,y+vsp_final,par_collide))
  27. {
  28.     var inc = sign(vsp_final);
  29.     while (!place_meeting(x,y+inc,par_collide)) y+= inc;
  30.     vsp_final = 0;
  31.     vsp = 0;
  32. }
  33. y+=vsp_final;
Advertisement
Add Comment
Please, Sign In to add comment