108 lines
3.0 KiB
JavaScript
Raw Permalink Normal View History

2016-03-23 22:59:54 -04:00
/*
2021-01-25 22:41:21 -08:00
* MIT License
* Copyright (c) 2019 Erin Catto
2016-03-23 22:59:54 -04:00
*
2021-01-25 22:41:21 -08:00
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
2016-03-23 22:59:54 -04:00
*/
2017-03-18 18:37:00 -04:00
planck.testbed('DistanceTest', function(testbed) {
2017-04-06 11:47:38 -04:00
var pl = planck, Vec2 = pl.Vec2, Transform = pl.Transform;
var Distance = pl.Distance;
2017-01-22 14:36:51 -05:00
var DistanceInput = Distance.Input;
var DistanceOutput = Distance.Output;
var SimplexCache = Distance.Cache;
2016-03-23 22:59:54 -04:00
2017-04-06 11:47:38 -04:00
var world = pl.World();
2016-03-23 22:59:54 -04:00
2018-10-20 23:34:22 -07:00
var transformA = pl.Transform();
transformA.p.set(0.0, -0.2);
2016-03-23 22:59:54 -04:00
2018-10-20 23:34:22 -07:00
var polygonA = pl.Box(10.0, 0.2);
2016-03-23 22:59:54 -04:00
2018-10-20 23:34:22 -07:00
var positionB = Vec2(12.017401, 0.13678508);
var angleB = -0.0109265;
var transformB = pl.Transform(positionB, angleB);
var polygonB = pl.Box(2.0, 0.1);
2016-03-23 22:59:54 -04:00
2017-01-22 14:36:51 -05:00
var bodyA = world.createBody();
2018-10-20 23:34:22 -07:00
var fixA = bodyA.createFixture(polygonA);
2016-03-23 22:59:54 -04:00
2017-01-22 14:36:51 -05:00
var bodyB = world.createBody();
2018-10-20 23:34:22 -07:00
var fixB = bodyB.createFixture(polygonB);
2016-03-23 22:59:54 -04:00
2017-01-22 14:36:51 -05:00
testbed.step = function() {
var input = new DistanceInput();
2018-10-20 23:34:22 -07:00
input.proxyA.set(polygonA, 0);
input.proxyB.set(polygonB, 0);
input.transformA = transformA;
input.transformB = transformB;
2017-01-22 14:36:51 -05:00
input.useRadii = true;
2016-03-23 22:59:54 -04:00
2017-01-22 14:36:51 -05:00
var cache = new SimplexCache();
2016-03-23 22:59:54 -04:00
2017-01-22 14:36:51 -05:00
var output = new DistanceOutput();
2016-03-23 22:59:54 -04:00
2017-01-22 14:36:51 -05:00
Distance(output, cache, input);
2017-01-25 00:35:14 -05:00
testbed.status("Distance", output.distance);
testbed.status("Iterations", output.iterations);
2017-01-22 14:36:51 -05:00
2018-10-20 23:34:22 -07:00
bodyA.setTransform(transformA);
bodyB.setTransform(transformB);
2017-01-22 14:36:51 -05:00
var x1 = output.pointA;
var x2 = output.pointB;
2017-01-23 00:18:22 -05:00
testbed.drawPoint(x1, 4.0, testbed.color(1.0, 0.0, 0.0));
testbed.drawPoint(x2, 4.0, testbed.color(1.0, 1.0, 0.0));
2017-01-22 14:36:51 -05:00
};
testbed.keydown = function() {
if (testbed.activeKeys['left']) {
2018-10-20 23:34:22 -07:00
positionB.x -= 0.1;
2017-01-25 00:35:14 -05:00
}
2017-01-22 14:36:51 -05:00
2017-01-25 00:35:14 -05:00
if (testbed.activeKeys['right']) {
2018-10-20 23:34:22 -07:00
positionB.x += 0.1;
2017-01-25 00:35:14 -05:00
}
2017-01-22 14:36:51 -05:00
2017-01-25 00:35:14 -05:00
if (testbed.activeKeys['down']) {
2018-10-20 23:34:22 -07:00
positionB.y -= 0.1;
2017-01-25 00:35:14 -05:00
}
2017-01-22 14:36:51 -05:00
2017-01-25 00:35:14 -05:00
if (testbed.activeKeys['up']) {
2018-10-20 23:34:22 -07:00
positionB.y += 0.1;
2017-01-25 00:35:14 -05:00
}
2017-01-22 14:36:51 -05:00
2017-01-25 00:35:14 -05:00
if (testbed.activeKeys['Z']) {
2018-10-20 23:34:22 -07:00
angleB += 0.1;
2017-01-25 00:35:14 -05:00
}
2017-01-22 14:36:51 -05:00
2017-01-25 00:35:14 -05:00
if (testbed.activeKeys['X']) {
2018-10-20 23:34:22 -07:00
angleB -= 0.1;
2017-01-22 14:36:51 -05:00
}
2016-03-23 22:59:54 -04:00
2018-10-20 23:34:22 -07:00
transformB.set(positionB, angleB);
2017-01-22 14:36:51 -05:00
};
2016-03-23 22:59:54 -04:00
2017-01-22 14:36:51 -05:00
return world;
2016-03-23 22:59:54 -04:00
});