95 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

/*
2021-01-25 22:41:21 -08:00
* MIT License
* Copyright (c) 2019 Erin Catto
*
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.
*/
2017-03-18 18:37:00 -04:00
planck.testbed('BodyTypes', function(testbed) {
var pl = planck, Vec2 = pl.Vec2;
2017-03-30 20:47:31 -04:00
var world = pl.World(Vec2(0, -10));
var SPEED = 3.0;
var ground = world.createBody();
ground.createFixture(pl.Edge(Vec2(-20.0, 0.0), Vec2(20.0, 0.0)));
// Define attachment
2018-10-20 23:34:22 -07:00
var attachment = world.createDynamicBody(Vec2(0.0, 3.0));
attachment.createFixture(pl.Box(0.5, 2.0), 2.0);
// Define platform
2018-10-20 23:34:22 -07:00
var platform = world.createDynamicBody(Vec2(-4.0, 5.0));
2018-10-20 23:34:22 -07:00
platform.createFixture(
pl.Box(0.5, 4.0, Vec2(4.0, 0.0), 0.5 * Math.PI),
{ friction : 0.6, density : 2.0 }
);
world.createJoint(pl.RevoluteJoint({
maxMotorTorque : 50.0,
enableMotor : true
2018-10-20 23:34:22 -07:00
}, attachment, platform, Vec2(0.0, 5.0)));
world.createJoint(pl.PrismaticJoint({
maxMotorForce : 1000.0,
enableMotor : true,
lowerTranslation : -10.0,
upperTranslation : 10.0,
enableLimit : true
2018-10-20 23:34:22 -07:00
}, ground, platform, Vec2(0.0, 5.0), Vec2(1.0, 0.0)));
// Create a payload
2017-03-30 20:47:31 -04:00
var payload = world.createDynamicBody(Vec2(0.0, 8.0));
2018-10-20 23:34:22 -07:00
payload.createFixture(
pl.Box(0.75, 0.75),
{ friction : 0.6, density : 2.0 }
);
testbed.keydown = function(code, char) {
if (char === 'Z') {
2018-10-20 23:34:22 -07:00
platform.setDynamic();
} else if (char === 'X') {
2018-10-20 23:34:22 -07:00
platform.setStatic();
} else if (char === 'C') {
2018-10-20 23:34:22 -07:00
platform.setKinematic();
platform.setLinearVelocity(Vec2(-SPEED, 0.0));
platform.setAngularVelocity(0.0);
}
};
testbed.step = function(settings) {
// Drive the kinematic body.
2018-10-20 23:34:22 -07:00
if (platform.isKinematic()) {
var p = platform.getTransform().p;
var v = platform.getLinearVelocity();
if ((p.x < -10.0 && v.x < 0.0) || (p.x > 10.0 && v.x > 0.0)) {
v.x = -v.x;
2018-10-20 23:34:22 -07:00
platform.setLinearVelocity(v);
}
}
};
testbed.info('Z: Dynamic, X: Static, C: Kinematic');
return world;
});