nb109

Pixel Collisions

Dec 22nd, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Creator (and original version at): https://pastebin.com/u/ShaunJS */
  2.  
  3. //decimal fractions of hsp are stored in hsp_f
  4. //every frame this fraction is added back in and the new fraction removed again
  5. //this moves us in whole integers each frame that are still representative of decimal movement
  6. //for example, at a hsp of 0.1 you would move 1 pixel every 10 frames.
  7. //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)
  8. //which sometimes caused the player after rounding, to be drawn overlapping walls by 1 pixel.
  9.  
  10. hsp_final = hsp + hsp_f;
  11. hsp_f = hsp_final - floor(abs(hsp_final))*sign(hsp_final);
  12. hsp_final -= hsp_f;
  13.  
  14. vsp_final = vsp + vsp_f;
  15. vsp_f = vsp_final - floor(abs(vsp_final))*sign(vsp_final);
  16. vsp_final -= vsp_f;
  17.  
  18. //same old place_meeting collision code.
  19. if (place_meeting(x+hsp_final,y,par_collide))
  20. {
  21.     var inc = sign(hsp_final);
  22.     while (!place_meeting(x+inc,y,par_collide)) x+= inc;
  23.     hsp_final = 0;
  24.     hsp = 0;
  25. }
  26. x+=hsp_final;
  27.  
  28. if (place_meeting(x,y+vsp_final,par_collide))
  29. {
  30.     var inc = sign(vsp_final);
  31.     while (!place_meeting(x,y+inc,par_collide)) y+= inc;
  32.     vsp_final = 0;
  33.     vsp = 0;
  34. }
  35. y+=vsp_final;
Advertisement
Add Comment
Please, Sign In to add comment