top of page
Search
cedarcantab

Studying Box2D-Lite in Javascript, Part 7.3: Raycast against Circle

Updated: Mar 1




/**

* Ray-cast input data. The ray extends from p1 to p1 +

maxFraction (p2 - p1).

* @export

* @constructor

*/

box2d.b2RayCastInput = function() {

this.p1 = new box2d.b2Vec2();

this.p2 = new box2d.b2Vec2();

this.maxFraction = 1;

}



/**

Ray-cast output data. The ray hits at p1 + fraction (p2 -

* p1), where p1 and p2 come from box2d.b2RayCastInput.

* @export

* @constructor

*/

box2d.b2RayCastOutput = function() {

this.normal = new box2d.b2Vec2();

this.fraction = 0;

};


RayCast = function(output, input, transform, childIndex) {

var position = box2d.b2Mul_X_V2(transform, this.m_p, box2d.b2CircleShape.prototype.RayCast.s_position);

var s = box2d.b2Sub_V2_V2(input.p1, position, box2d.b2CircleShape.prototype.RayCast.s_s);

var b = box2d.b2Dot_V2_V2(s, s) - box2d.b2Sq(this.m_radius);

// Solve quadratic equation.

var r = box2d.b2Sub_V2_V2(input.p2, input.p1, box2d.b2CircleShape.prototype.RayCast.s_r);

var c = box2d.b2Dot_V2_V2(s, r);

var rr = box2d.b2Dot_V2_V2(r, r);

var sigma = c c - rr b;

// Check for negative discriminant and short segment.

if (sigma < 0 || rr < box2d.b2_epsilon) {

return false;

}

// Find the point of intersection of the line with the circle.

var a = (-(c + box2d.b2Sqrt(sigma)));

// Is the intersection point on the segment?

if (0 <= a && a <= input.maxFraction * rr) {

a /= rr;

output.fraction = a;

box2d.b2AddMul_V2_S_V2(s, a, r, output.normal).SelfNormalize();

return true;

}

return false;

}

8 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";...

コメント


記事: Blog2_Post
bottom of page