Giter VIP home page Giter VIP logo

Comments (8)

ikudrickiy avatar ikudrickiy commented on May 25, 2024 1

@eonarheim I'll honestly be surprised if the new method won't be faster. I'll try to visualize the concept by creating an interactive demo. We can refer to it in the code comments.

from excalibur.

ikudrickiy avatar ikudrickiy commented on May 25, 2024 1
if (
  this.bottom <= other.top ||
  other.bottom <= this.top ||
  this.right <= other.left ||
  other.right <= this.left
)
  // there is no collision
  return null;

// the path that must be taken when moving in the direction to escape the collision
// these are positive if return hasn't happen
let topPath = this.bottom - other.top;
let bottomPath = other.bottom - this.top;
let leftPath = this.right - other.left;
let rightPath = other.right - this.left;

let minIndex = SOMETHING.getMinIndex([
  topPath,
  bottomPath,
  leftPath,
  rightPath,
]);

switch (minIndex) {
  case 0:
    return new Vector(0, -topPath);
  case 1:
    return new Vector(0, bottomPath);
  case 2:
    return new Vector(-leftPath, 0);
  case 3:
    return new Vector(rightPath, 0);
}

This fails faster. I prefer this.

P.S: This logic is also so clear no demo comment is needed :)

from excalibur.

ikudrickiy avatar ikudrickiy commented on May 25, 2024
function getMinIndex(arr: Array<number>) {
    if (arr.length === 0)
        return -1; // is it legal?

    let min = arr[0];
    let min_i = 0;

    for (let i = 1; i < arr.length; i++) {
        if (arr[i] < min) {
            min = arr[i];
            min_i = i;
        }
    }
    
    return min_i;
}

from excalibur.

eonarheim avatar eonarheim commented on May 25, 2024

@ikudrickiy I like this refactor, much more readable. As long as it produces the same results as the previous algorithm (and the tests pass) I see no reason not to make an optimization.

An additional win would be if this has better performance characteristics as well 😎

from excalibur.

ikudrickiy avatar ikudrickiy commented on May 25, 2024

@eonarheim demo is ready:
https://www.geogebra.org/m/gx9j4wnd
Mirroring ruins the demo xD
Don't try to mirror a rectangle!

from excalibur.

ikudrickiy avatar ikudrickiy commented on May 25, 2024

After studying the model, it turned out that the code can be simplified to

// compute the pathes needed to get ahead of the other box in that direction
// if path <= 0 it means this box is already ahead in that direction
let topPath = this.bottom - other.top;
let bottomPath = other.bottom - this.top;
let leftPath = this.right - other.left;
let rightPath = other.right - this.left;

if (topPath <= 0 || bottomPath <= 0 || leftPath <= 0 || rightPath <= 0)
  return null;

let minIndex = SOMETHING.getMinIndex([topPath, bottomPath, leftPath, rightPath]);

switch(minIndex) { 
   case 0:
      return new Vector(0, -topPath);
   case 1:
      return new Vector(0, bottomPath);
   case 2:
      return new Vector (-leftPath, 0);
   case 3:
      return new Vector (rightPath, 0);
} 

from excalibur.

ikudrickiy avatar ikudrickiy commented on May 25, 2024

Also I found out the function used to send null instead of Vector.Zero, so I did the same

from excalibur.

ikudrickiy avatar ikudrickiy commented on May 25, 2024

I think this function comes to the Utils.ts

from excalibur.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.