Compare commits

...

8 Commits

Author SHA1 Message Date
Oliver Zell
abc24e36a4 refactor: use early returns 2023-08-04 15:47:05 +02:00
Oliver Zell
82e7aba5bc fix: fix anchor A formula for MotorJoint to include the offset 2023-08-04 15:31:34 +02:00
Oliver Zell
7b380f0143 docs: added documentation of damping parameter units 2023-08-03 21:07:12 +02:00
Oliver Zell
110576ee54 refactor!: remove getVertex method on shapes 2023-08-03 21:04:44 +02:00
Oliver Zell
83c949afae feat: body.setAwake now resets the sleep timer even if the body is awake 2023-08-03 20:54:11 +02:00
Oliver Zell
f79b308014 refactor: polygonal radius clarity 2023-08-03 20:27:08 +02:00
Oliver Zell
0a35b42e40 refactor: remove dead code 2023-08-03 20:18:08 +02:00
Oliver Zell
4c64f4d177 refactor: minor refactorings 2023-08-03 18:20:55 +02:00
14 changed files with 36 additions and 39 deletions

View File

@ -111,9 +111,6 @@ export const Distance = function (output: DistanceOutput, cache: SimplexCache, i
const saveB = []; // int[3]
let saveCount = 0;
let distanceSqr1 = Infinity;
let distanceSqr2 = Infinity;
// Main iteration loop.
let iter = 0;
while (iter < k_maxIters) {
@ -131,16 +128,6 @@ export const Distance = function (output: DistanceOutput, cache: SimplexCache, i
break;
}
// Compute closest point.
const p = simplex.getClosestPoint();
distanceSqr2 = p.lengthSquared();
// Ensure progress
if (distanceSqr2 >= distanceSqr1) {
// break;
}
distanceSqr1 = distanceSqr2;
// Get search direction.
const d = simplex.getSearchDirection();

View File

@ -37,6 +37,11 @@ import type { Vec2Value } from '../common/Vec2';
*/
export abstract class Shape {
m_type: ShapeType;
/**
* Radius of a shape. For polygonal shapes this must be b2_polygonRadius.
* There is no support for making rounded polygons.
*/
m_radius: number;
/** @internal */

View File

@ -148,6 +148,10 @@ export class ChainShape extends Shape {
_createLoop(vertices: Vec2Value[]): ChainShape {
_ASSERT && console.assert(this.m_vertices.length == 0 && this.m_count == 0);
_ASSERT && console.assert(vertices.length >= 3);
if (vertices.length < 3) {
return;
}
for (let i = 1; i < vertices.length; ++i) {
const v1 = vertices[i - 1];
const v2 = vertices[i];

View File

@ -102,11 +102,6 @@ export class CircleShape extends Shape {
return this.m_p;
}
getVertex(index: 0): Vec2 {
_ASSERT && console.assert(index == 0);
return this.m_p;
}
/**
* @internal
* @deprecated Shapes should be treated as immutable.

View File

@ -291,7 +291,7 @@ export const CollideEdgePolygon = function (manifold: Manifold, edgeA: EdgeShape
polygonBA.normals[i] = Rot.mulVec2(xf.q, polygonB.m_normals[i]);
}
const radius = 2.0 * Settings.polygonRadius;
const radius = polygonB.m_radius + edgeA.m_radius;
manifold.pointCount = 0;

View File

@ -104,11 +104,6 @@ export class PolygonShape extends Shape {
return this.m_radius;
}
getVertex(index: number): Vec2 {
_ASSERT && console.assert(0 <= index && index < this.m_count);
return this.m_vertices[index];
}
/**
* @internal
* @deprecated Shapes should be treated as immutable.
@ -206,6 +201,7 @@ export class PolygonShape extends Shape {
let ih = i0;
while (true) {
_ASSERT && console.assert(m < Settings.maxPolygonVertices);
hull[m] = ih;
let ie = 0;

View File

@ -70,12 +70,14 @@ export interface BodyDef {
* Linear damping is use to reduce the linear velocity. The
* damping parameter can be larger than 1.0 but the damping effect becomes
* sensitive to the time step when the damping parameter is large.
* Units are 1/time
*/
linearDamping?: number;
/**
* Angular damping is use to reduce the angular velocity.
* The damping parameter can be larger than 1.0 but the damping effect
* becomes sensitive to the time step when the damping parameter is large.
* Units are 1/time
*/
angularDamping?: number;
/**
@ -467,10 +469,8 @@ export class Body {
*/
setAwake(flag: boolean): void {
if (flag) {
if (this.m_awakeFlag == false) {
this.m_awakeFlag = true;
this.m_sleepTime = 0.0;
}
this.m_awakeFlag = true;
this.m_sleepTime = 0.0;
} else {
this.m_awakeFlag = false;
this.m_sleepTime = 0.0;

View File

@ -39,6 +39,7 @@ import { ContactImpulse, TimeStep } from "./Solver";
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
// Solver debugging is normally disabled because the block solver sometimes has to deal with a poorly conditioned effective mass matrix.
const DEBUG_SOLVER = false;
/**
@ -85,7 +86,7 @@ export type ContactCallback = (
/**
* Friction mixing law. The idea is to allow either fixture to drive the
* restitution to zero. For example, anything slides on ice.
* friction to zero. For example, anything slides on ice.
*/
export function mixFriction(friction1: number, friction2: number): number {
return Math.sqrt(friction1 * friction2);
@ -910,8 +911,7 @@ export class Contact {
// 01/07 on Box2D_Lite).
// Build the mini LCP for this contact patch
//
// vn = A * x + b, vn >= 0, , vn >= 0, x >= 0 and vn_i * x_i = 0 with i =
// 1..2
// vn = A * x + b, vn >= 0, x >= 0 and vn_i * x_i = 0 with i = 1..2
//
// A = J * W * JT and J = ( -n, -r1 x n, n, r2 x n )
// b = vn0 - velocityBias

View File

@ -194,8 +194,8 @@ export class Solver {
_ASSERT && console.assert(b.isActive() == true);
this.addBody(b);
// Make sure the body is awake.
b.setAwake(true);
// Make sure the body is awake (without resetting sleep timer).
b.m_awakeFlag = true;
// To keep islands as small as possible, we don't
// propagate islands across static bodies.

View File

@ -135,6 +135,9 @@ export class MotorJoint extends Joint {
// J = [-I -r1_skew I r2_skew ]
// Identity used:
// w k % (rx i + ry j) = w * (-ry i + rx j)
//
// r1 = offset - c1
// r2 = -c2
// Angle constraint
// Cdot = w2 - w1
@ -298,11 +301,10 @@ export class MotorJoint extends Joint {
const qB = Rot.neo(aB);
// Compute the effective mass matrix.
this.m_rA = Rot.mulVec2(qA, Vec2.neg(this.m_localCenterA));
this.m_rA = Rot.mulVec2(qA, Vec2.sub(this.m_linearOffset, this.m_localCenterA));
this.m_rB = Rot.mulVec2(qB, Vec2.neg(this.m_localCenterB));
// J = [-I -r1_skew I r2_skew]
// [ 0 -1 0 1]
// r_skew = [-ry; rx]
// Matlab
@ -315,6 +317,7 @@ export class MotorJoint extends Joint {
const iA = this.m_invIA;
const iB = this.m_invIB;
// Upper 2 by 2 of K for point to point
const K = new Mat22();
K.ex.x = mA + mB + iA * this.m_rA.y * this.m_rA.y + iB * this.m_rB.y * this.m_rB.y;
K.ex.y = -iA * this.m_rA.x * this.m_rA.y - iB * this.m_rB.x * this.m_rB.y;
@ -331,7 +334,6 @@ export class MotorJoint extends Joint {
this.m_linearError = Vec2.zero();
this.m_linearError.addCombine(1, cB, 1, this.m_rB);
this.m_linearError.subCombine(1, cA, 1, this.m_rA);
this.m_linearError.sub(Rot.mulVec2(qA, this.m_linearOffset));
this.m_angularError = aB - aA - this.m_angularOffset;

View File

@ -194,9 +194,8 @@ export class MouseJoint extends Joint {
* Use this to update the target point.
*/
setTarget(target: Vec2Value): void {
if (this.m_bodyB.isAwake() == false) {
this.m_bodyB.setAwake(true);
}
if (Vec2.areEqual(target, this.m_targetA)) return;
this.m_bodyB.setAwake(true);
this.m_targetA = Vec2.clone(target);
}

View File

@ -449,6 +449,7 @@ export class PrismaticJoint extends Joint {
* Enable/disable the joint motor.
*/
enableMotor(flag: boolean): void {
if (flag == this.m_enableMotor) return;
this.m_bodyA.setAwake(true);
this.m_bodyB.setAwake(true);
this.m_enableMotor = flag;
@ -458,6 +459,7 @@ export class PrismaticJoint extends Joint {
* Set the motor speed, usually in meters per second.
*/
setMotorSpeed(speed: number): void {
if (speed == this.m_motorSpeed) return;
this.m_bodyA.setAwake(true);
this.m_bodyB.setAwake(true);
this.m_motorSpeed = speed;
@ -467,6 +469,7 @@ export class PrismaticJoint extends Joint {
* Set the maximum motor force, usually in N.
*/
setMaxMotorForce(force: number): void {
if (force == this.m_maxMotorForce) return;
this.m_bodyA.setAwake(true);
this.m_bodyB.setAwake(true);
this.m_maxMotorForce = force;

View File

@ -302,6 +302,7 @@ export class RevoluteJoint extends Joint {
* Enable/disable the joint motor.
*/
enableMotor(flag: boolean): void {
if (flag == this.m_enableMotor) return;
this.m_bodyA.setAwake(true);
this.m_bodyB.setAwake(true);
this.m_enableMotor = flag;
@ -318,6 +319,7 @@ export class RevoluteJoint extends Joint {
* Set the motor speed in radians per second.
*/
setMotorSpeed(speed: number): void {
if (speed == this.m_motorSpeed) return;
this.m_bodyA.setAwake(true);
this.m_bodyB.setAwake(true);
this.m_motorSpeed = speed;
@ -334,6 +336,7 @@ export class RevoluteJoint extends Joint {
* Set the maximum motor torque, usually in N-m.
*/
setMaxMotorTorque(torque: number): void {
if (torque == this.m_maxMotorTorque) return;
this.m_bodyA.setAwake(true);
this.m_bodyB.setAwake(true);
this.m_maxMotorTorque = torque;

View File

@ -314,6 +314,7 @@ export class WheelJoint extends Joint {
* Enable/disable the joint motor.
*/
enableMotor(flag: boolean): void {
if (flag == this.m_enableMotor) return;
this.m_bodyA.setAwake(true);
this.m_bodyB.setAwake(true);
this.m_enableMotor = flag;
@ -323,6 +324,7 @@ export class WheelJoint extends Joint {
* Set the motor speed, usually in radians per second.
*/
setMotorSpeed(speed: number): void {
if (speed == this.m_motorSpeed) return;
this.m_bodyA.setAwake(true);
this.m_bodyB.setAwake(true);
this.m_motorSpeed = speed;
@ -339,6 +341,7 @@ export class WheelJoint extends Joint {
* Set/Get the maximum motor force, usually in N-m.
*/
setMaxMotorTorque(torque: number): void {
if (torque == this.m_maxMotorTorque) return;
this.m_bodyA.setAwake(true);
this.m_bodyB.setAwake(true);
this.m_maxMotorTorque = torque;