Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add translation limit to wheel and prismatic joint #250

Draft
wants to merge 1 commit into
base: v2-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions example/Car.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,35 +149,33 @@ wheelBack.createFixture(new Circle(0.4), wheelFD);
var wheelFront = world.createDynamicBody(new Vec2(1.0, 0.4));
wheelFront.createFixture(new Circle(0.4), wheelFD);

const massWheelBack = wheelBack.getMass();
const massWheelFront = wheelFront.getMass();

const omega = 2.0 * Math.PI * HZ;

var springBack = world.createJoint(new WheelJoint({
motorSpeed : 0.0,
maxMotorTorque : 20.0,
enableMotor : true,
frequencyHz : HZ,
dampingRatio : ZETA
stiffness : massWheelBack * omega * omega,
damping : 2.0 * massWheelBack * ZETA * omega,
lowerTranslation : -0.25,
upperTranslation : 0.25,
enableLimit : true
}, car, wheelBack, wheelBack.getPosition(), new Vec2(0.0, 1.0)));

var springFront = world.createJoint(new WheelJoint({
motorSpeed : 0.0,
maxMotorTorque : 10.0,
enableMotor : false,
frequencyHz : HZ,
dampingRatio : ZETA
stiffness : massWheelFront * omega * omega,
damping : 2.0 * massWheelFront * ZETA * omega,
lowerTranslation : -0.25,
upperTranslation : 0.25,
enableLimit : true
}, car, wheelFront, wheelFront.getPosition(), new Vec2(0.0, 1.0)));

testbed.keydown = function() {
if (testbed.activeKeys.down) {
HZ = Math.max(0.0, HZ - 1.0);
springBack.setSpringFrequencyHz(HZ);
springFront.setSpringFrequencyHz(HZ);

} else if (testbed.activeKeys.up) {
HZ += 1.0;
springBack.setSpringFrequencyHz(HZ);
springFront.setSpringFrequencyHz(HZ);
}
};

testbed.step = function() {
if (testbed.activeKeys.right && testbed.activeKeys.left) {
springBack.setMotorSpeed(0);
Expand Down
21 changes: 9 additions & 12 deletions example/Prismatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* SOFTWARE.
*/

// The motor in this test gets smoother with higher velocity iterations.
// Test the prismatic joint with limits and motor options.

const { World, Vec2, PrismaticJoint, Edge, Box, Circle } = planck;

Expand All @@ -30,6 +30,8 @@ var world = new World(new Vec2(0, -10));
const testbed = planck.testbed();
testbed.start(world);

var ENABLE_LIMIT = true;
var ENABLE_MOTOR = false;
var MOTOR_SPEED = 10;

var ground = world.createBody();
Expand All @@ -41,22 +43,17 @@ var body = world.createBody({
angle : 0.5 * Math.PI,
allowSleep : false
});
body.createFixture(new Box(2.0, 0.5), 5.0);
body.createFixture(new Box(1.0, 1.5), 5.0);

// Bouncy limit
var axis = new Vec2(2.0, 1.0);
axis.normalize();
var joint = new PrismaticJoint({
motorSpeed : MOTOR_SPEED,
maxMotorForce : 10000.0,
enableMotor : true,
lowerTranslation : 0.0,
upperTranslation : 20.0,
enableLimit : true
}, ground, body, new Vec2(0.0, 0.0), axis);

// Non-bouncy limit
// (ground, body, new Vec2(-10.0, 10.0), new Vec2(1.0, 0.0));
enableMotor : ENABLE_MOTOR,
lowerTranslation : -10.0,
upperTranslation : 10.0,
enableLimit : ENABLE_LIMIT
}, ground, body, new Vec2(0.0, 0.0), new Vec2(1.0, 0.0));

world.createJoint(joint);

Expand Down
89 changes: 89 additions & 0 deletions example/WheelJoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* MIT License
* Copyright (c) 2019 Erin Catto
*
* 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.
*/

// Test the wheel joint with motor, spring, and limit options.

var { World, Vec2, Edge, Box, Circle, Polygon, RevoluteJoint, WheelJoint } = planck;

var testbed = planck.testbed();

var world = new World({
gravity : new Vec2(0, -10)
});

testbed.start(world);

var ground = world.createBody();
ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0);


var enableLimit = true;
var enableMotor = false;
var motorSpeed = 0.0;

var body = world.createBody({
type : 'dynamic',
position : new Vec2(0.0, 10.0),
allowSleep : false
});
body.createFixture(new Circle(2.0), 5.0);

var mass = body.getMass();
var hertz = 1.0;
var dampingRatio = 0.7;
var omega = 2.0 * Math.PI * hertz;

var joint = new WheelJoint({
motorSpeed : motorSpeed,
maxMotorTorque : 10000.0,
enableMotor : enableMotor,
stiffness : mass * omega * omega,
damping : 2.0 * mass * dampingRatio * omega,
lowerTranslation : -3.0,
upperTranslation : 3.0,
enableLimit : enableLimit
}, ground, body, new Vec2(0.0, 10.0), new Vec2(0.0, 1.0));

world.createJoint(joint);

testbed.step = function() {
var torque = joint.getMotorTorque(testbed.hz);
testbed.status({
"Motor Torque" : torque,
"Motor Speed" : joint.getMotorSpeed(),
});

if (testbed.activeKeys.right && testbed.activeKeys.left) {
joint.setMotorSpeed(0);
joint.enableMotor(true);
} else if (testbed.activeKeys.right) {
joint.setMotorSpeed(-10);
joint.enableMotor(true);
} else if (testbed.activeKeys.left) {
joint.setMotorSpeed(10);
joint.enableMotor(true);
} else {
joint.setMotorSpeed(0);
joint.enableMotor(false);
}
};
3 changes: 2 additions & 1 deletion example/list.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@
"VaryingFriction",
"VaryingRestitution",
"VerticalStack",
"Web"
"Web",
"WheelJoint"
]
5 changes: 5 additions & 0 deletions src/collision/shape/CircleShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const _CONSTRUCTOR_FACTORY = typeof CONSTRUCTOR_FACTORY === 'undefined' ? false

const temp = matrix.vec2(0, 0);

/**
* A solid circle shape
*/
export class CircleShape extends Shape {
static TYPE = 'circle' as const;
m_type: 'circle';
Expand Down Expand Up @@ -139,6 +142,8 @@ export class CircleShape extends Shape {

/**
* Cast a ray against a child shape.
* @note because the circle is solid, rays that start inside do not hit
* because the normal is not defined.
*
* @param output The ray-cast results.
* @param input The ray-cast input parameters.
Expand Down
6 changes: 4 additions & 2 deletions src/collision/shape/PolygonShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const center = matrix.vec2(0, 0);
const s = matrix.vec2(0, 0);

/**
* A convex polygon. It is assumed that the interior of the polygon is to the
* left of each edge. Polygons have a maximum number of vertices equal to
* A solid convex polygon. It is assumed that the interior of the polygon is to
* the left of each edge. Polygons have a maximum number of vertices equal to
* Settings.maxPolygonVertices. In most cases you should not need many vertices
* for a convex polygon. extends Shape
*/
Expand Down Expand Up @@ -323,6 +323,8 @@ export class PolygonShape extends Shape {

/**
* Cast a ray against a child shape.
* @note because the polygon is solid, rays that start inside do not hit
* because the normal is not defined.
*
* @param output The ray-cast results.
* @param input The ray-cast input parameters.
Expand Down
Loading
Loading