2021-03-06 15:31:29 -08:00

175 lines
4.9 KiB
JavaScript

/*
* Planck.js
* The MIT License
* Copyright (c) 2021 Erin Catto, Ali Shakiba
*
* 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.
*/
var _DEBUG = typeof DEBUG === 'undefined' ? false : DEBUG;
var _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
module.exports = CircleShape;
var common = require('../util/common');
var options = require('../util/options');
var Math = require('../common/Math');
var Transform = require('../common/Transform');
var Rot = require('../common/Rot');
var Vec2 = require('../common/Vec2');
var AABB = require('../collision/AABB');
var Settings = require('../Settings');
var Shape = require('../Shape');
CircleShape._super = Shape;
CircleShape.prototype = Object.create(CircleShape._super.prototype);
CircleShape.TYPE = 'circle';
Shape.TYPES[CircleShape.TYPE] = CircleShape;
function CircleShape(a, b) {
if (!(this instanceof CircleShape)) {
return new CircleShape(a, b);
}
CircleShape._super.call(this);
this.m_type = CircleShape.TYPE;
this.m_p = Vec2.zero();
this.m_radius = 1;
if (typeof a === 'object' && Vec2.isValid(a)) {
this.m_p.set(a);
if (typeof b === 'number') {
this.m_radius = b;
}
} else if (typeof a === 'number') {
this.m_radius = a;
}
}
CircleShape.prototype._serialize = function() {
return {
type: this.m_type,
p: this.m_p,
radius: this.m_radius,
};
};
CircleShape._deserialize = function(data) {
return new CircleShape(data.p, data.radius);
};
CircleShape.prototype.getRadius = function() {
return this.m_radius;
}
CircleShape.prototype.getCenter = function() {
return this.m_p;
}
CircleShape.prototype.getVertex = function(index) {
_ASSERT && common.assert(index == 0);
return this.m_p;
}
CircleShape.prototype.getVertexCount = function(index) {
return 1;
}
/**
* @deprecated
*/
CircleShape.prototype._clone = function() {
var clone = new CircleShape();
clone.m_type = this.m_type;
clone.m_radius = this.m_radius;
clone.m_p = this.m_p.clone();
return clone;
}
CircleShape.prototype.getChildCount = function() {
return 1;
}
CircleShape.prototype.testPoint = function(xf, p) {
var center = Vec2.add(xf.p, Rot.mulVec2(xf.q, this.m_p));
var d = Vec2.sub(p, center);
return Vec2.dot(d, d) <= this.m_radius * this.m_radius;
}
// Collision Detection in Interactive 3D Environments by Gino van den Bergen
// From Section 3.1.2
// x = s + a * r
// norm(x) = radius
CircleShape.prototype.rayCast = function(output, input, xf, childIndex) {
var position = Vec2.add(xf.p, Rot.mulVec2(xf.q, this.m_p));
var s = Vec2.sub(input.p1, position);
var b = Vec2.dot(s, s) - this.m_radius * this.m_radius;
// Solve quadratic equation.
var r = Vec2.sub(input.p2, input.p1);
var c = Vec2.dot(s, r);
var rr = Vec2.dot(r, r);
var sigma = c * c - rr * b;
// Check for negative discriminant and short segment.
if (sigma < 0.0 || rr < Math.EPSILON) {
return false;
}
// Find the point of intersection of the line with the circle.
var a = -(c + Math.sqrt(sigma));
// Is the intersection point on the segment?
if (0.0 <= a && a <= input.maxFraction * rr) {
a /= rr;
output.fraction = a;
output.normal = Vec2.add(s, Vec2.mul(a, r));
output.normal.normalize();
return true;
}
return false;
}
CircleShape.prototype.computeAABB = function(aabb, xf, childIndex) {
var p = Vec2.add(xf.p, Rot.mulVec2(xf.q, this.m_p));
aabb.lowerBound.set(p.x - this.m_radius, p.y - this.m_radius);
aabb.upperBound.set(p.x + this.m_radius, p.y + this.m_radius);
}
CircleShape.prototype.computeMass = function(massData, density) {
massData.mass = density * Math.PI * this.m_radius * this.m_radius;
massData.center = this.m_p;
// inertia about the local origin
massData.I = massData.mass
* (0.5 * this.m_radius * this.m_radius + Vec2.dot(this.m_p, this.m_p));
}
CircleShape.prototype.computeDistanceProxy = function(proxy) {
proxy.m_vertices.push(this.m_p);
proxy.m_count = 1;
proxy.m_radius = this.m_radius;
};