Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. this.linearCollision = function(incX, incY, collidingObject) {
  2. // Calculate the resultant vectors
  3. var vectorX = this.centreX - collidingObject.centreX;
  4. var vectorY = this.centreY - collidingObject.centreY;
  5.  
  6. if (Math.abs(vectorY) > Math.abs(vectorX)) { // If the y vector is longer than the x vector (abs to remove negatives)
  7. if (vectorY > 0) { // If the y vector is pointing downwards
  8. this.y = collidingObject.bottom;
  9. }
  10. else { // If the y vector is pointing upwards
  11. this.y = collidingObject.y - this.height; // Set the y value to the top of the colliding shape
  12. }
  13. }
  14. else { // If the x vector is longer than the y vector
  15. if (vectorX > 0) { // If the x vector is pointing right
  16. this.x = collidingObject.right;
  17. }
  18. else { // If the x vector is pointing left
  19. this.x = collidingObject.x - this.width;
  20. }
  21. }
  22.  
  23. incX = (0-incX);
  24. incY = (0-incY);
  25.  
  26. return [incX, incY];
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement