Add benchmark for polygon collision test and add test runner for Firefox

This commit is contained in:
Florian Rival
2018-12-26 13:45:45 +01:00
parent 704eaacc7e
commit f39af51fda
5 changed files with 44 additions and 10 deletions

View File

@@ -118,8 +118,8 @@ gdjs.Polygon.createRectangle = function(width, height) {
};
/**
* Do a collision test between two polygons.<br>
* Please note that polygons must <b>convexes</b>!
* Do a collision test between two polygons.
* Please note that polygons must *convexes*!
*
* Uses <a href="http://en.wikipedia.org/wiki/Hyperplane_separation_theorem">Separating Axis Theorem </a>.<br>
* Based on <a href="http://www.codeproject.com/Articles/15573/2D-Polygon-Collision-Detection">this</a>
@@ -168,17 +168,18 @@ gdjs.Polygon.collisionTest = function(p1,p2) {
gdjs.Polygon.project(axis, p2, minMaxB);
//If the projections on the axis do not overlap, then their is no collision
if (gdjs.Polygon.distance(minMaxA[0], minMaxA[1], minMaxB[0], minMaxB[1]) > 0) {
var dist = gdjs.Polygon.distance(minMaxA[0], minMaxA[1], minMaxB[0], minMaxB[1]);
if (dist > 0) {
result.collision = false;
result.move_axis[0] = 0;
result.move_axis[1] = 0;
return result;
}
var dist = Math.abs(gdjs.Polygon.distance(minMaxA[0], minMaxA[1], minMaxB[0], minMaxB[1]));
var absDist = Math.abs(dist);
if (dist < minDist) {
minDist = dist;
if (absDist < minDist) {
minDist = absDist;
move_axis[0] = axis[0];
move_axis[1] = axis[1];
}

View File

@@ -4,15 +4,16 @@ These are the tests for the GDevelop JavaScript game engine.
Make sure you have [Node.js](https://nodejs.org/) installed. Update dependencies:
```
```bash
cd GDJS/tests
npm install
```
Then launch tests:
```
npm test
```bash
npm test #This will use Chrome Headless
npm test:firefox #To run tests using Firefox
```
## About the tests

View File

@@ -0,0 +1,24 @@
describe('gdjs.Polygon', function() {
it('benchmark gdjs.Polygon.collisionTest between two polygons', function() {
this.timeout(20000);
var rect1 = gdjs.Polygon.createRectangle(32, 40);
var rect2 = gdjs.Polygon.createRectangle(32, 40);
var rect3 = gdjs.Polygon.createRectangle(32, 40);
rect2.move(20, 20);
rect3.move(50, 50);
const benchmarkSuite = makeBenchmarkSuite({
benchmarksCount: 60,
iterationsCount: 60000,
});
benchmarkSuite
.add('collisionTest between two overlapping rectangles', i => {
gdjs.Polygon.collisionTest(rect1, rect2);
})
.add('collisionTest between two non overlapping rectangles', i => {
gdjs.Polygon.collisionTest(rect1, rect3);
});
console.log(benchmarkSuite.run());
});
});

View File

@@ -1906,6 +1906,12 @@
"which": "1.3.1"
}
},
"karma-firefox-launcher": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/karma-firefox-launcher/-/karma-firefox-launcher-1.1.0.tgz",
"integrity": "sha512-LbZ5/XlIXLeQ3cqnCbYLn+rOVhuMIK9aZwlP6eOLGzWdo1UVp7t6CN3DP4SafiRLjexKwHeKHDm0c38Mtd3VxA==",
"dev": true
},
"karma-mocha": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/karma-mocha/-/karma-mocha-1.3.0.tgz",

View File

@@ -5,7 +5,8 @@
"main": "index.js",
"private": true,
"scripts": {
"test": "./node_modules/karma/bin/karma start --browsers ChromeHeadless --single-run"
"test": "./node_modules/karma/bin/karma start --browsers ChromeHeadless --single-run",
"test:firefox": "./node_modules/karma/bin/karma start --browsers Firefox --single-run"
},
"keywords": [
"HTML5",
@@ -23,6 +24,7 @@
"devDependencies": {
"karma": "^1.7.1",
"karma-chrome-launcher": "^2.2.0",
"karma-firefox-launcher": "^1.1.0",
"karma-mocha": "^1.3.0"
}
}