Comments (8)
@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.
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.
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.
@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.
@eonarheim demo is ready:
https://www.geogebra.org/m/gx9j4wnd
Mirroring ruins the demo xD
Don't try to mirror a rectangle!
from excalibur.
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.
Also I found out the function used to send null
instead of Vector.Zero
, so I did the same
from excalibur.
I think this function comes to the Utils.ts
from excalibur.
Related Issues (20)
- NuGeT package install does not add Excalibur files to .NET project HOT 2
- Add getGlobalOldPos() helper to Actors HOT 2
- Support anchor in pixels HOT 5
- Child entity should inherit transform.coordPlane of parent HOT 1
- Scene transitions may not start in correct state when fixedUpdateFps is set HOT 2
- Feature request - Suppress canvas injection from the Engine constructor HOT 5
- ParticleEmitter ignores z-index HOT 2
- Typo in documentation for Vector.rotate() HOT 1
- Code examples on website are shown in light and dark mode always HOT 5
- Wrong hex color values HOT 2
- excaliburjs.com/docs "Edit this page" link is generated incorrectly HOT 3
- Optimise Side.fromDirection(direction: Vector): Side HOT 1
- Optimise BoundingBox rayCast methods HOT 1
- Optimise BoundingBox.contains(val: any): boolean
- Correct the BoundingBox.intersect(other: BoundingBox): Vector comments positions HOT 3
- Tag BoudingBox.getPoints() as a readonly array return HOT 2
- Passing sceneInstance to "Engine.director.getSceneName" does not work it.
- bug(actions): repeatForever doesn't like negative offsets
- v0.29.2 introduces Graphics (Polygon / Circle) glitch in combination with Animations HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from excalibur.