Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- this.linearCollision = function(incX, incY, collidingObject) {
- // Calculate the resultant vectors
- var vectorX = this.centreX - collidingObject.centreX;
- var vectorY = this.centreY - collidingObject.centreY;
- if (Math.abs(vectorY) > Math.abs(vectorX)) { // If the y vector is longer than the x vector (abs to remove negatives)
- if (vectorY > 0) { // If the y vector is pointing downwards
- this.y = collidingObject.bottom;
- }
- else { // If the y vector is pointing upwards
- this.y = collidingObject.y - this.height; // Set the y value to the top of the colliding shape
- }
- }
- else { // If the x vector is longer than the y vector
- if (vectorX > 0) { // If the x vector is pointing right
- this.x = collidingObject.right;
- }
- else { // If the x vector is pointing left
- this.x = collidingObject.x - this.width;
- }
- }
- incX = (0-incX);
- incY = (0-incY);
- return [incX, incY];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement