top of page
Search
cedarcantab

Studying Box2D-Lite in Javascript, Part XX.2: Distance to Edge



box2d.b2EdgeShape.prototype.ComputeDistance = function(xf, p, normal, childIndex) {

var v1 = box2d.b2Mul_X_V2(xf, this.m_vertex1, box2d.b2EdgeShape.prototype.ComputeDistance.s_v1);

var v2 = box2d.b2Mul_X_V2(xf, this.m_vertex2, box2d.b2EdgeShape.prototype.ComputeDistance.s_v2);

var d = box2d.b2Sub_V2_V2(p, v1, box2d.b2EdgeShape.prototype.ComputeDistance.s_d);

var s = box2d.b2Sub_V2_V2(v2, v1, box2d.b2EdgeShape.prototype.ComputeDistance.s_s);

var ds = box2d.b2Dot_V2_V2(d, s);

if (ds > 0) {

var s2 = box2d.b2Dot_V2_V2(s, s);

if (ds > s2) {

box2d.b2Sub_V2_V2(p, v2, d);

} else {

d.SelfMulSub(ds / s2, s);

}

}

normal.Copy(d);

return normal.Normalize();

}

4 views0 comments

Recent Posts

See All

p2 naive broadphase

var Broadphase = require('../collision/Broadphase'); module.exports = NaiveBroadphase; /** * Naive broadphase implementation. Does N^2...

sopiro motor constranit

import { Matrix2, Vector2 } from "./math.js"; import { RigidBody } from "./rigidbody.js"; import { Settings } from "./settings.js";...

Comentários


記事: Blog2_Post
bottom of page