Skip to content

Commit 872ea6c

Browse files
committed
added Body.prototype.applyImpulse, .applyImpulseLocal, .vectorToLocalFrame and .vectorToWorldFrame
1 parent 238d120 commit 872ea6c

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

src/objects/Body.js

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,11 +644,58 @@ Body.prototype.applyForce = function(force,worldPoint){
644644
this.angularForce += rotForce;
645645
};
646646

647+
/**
648+
* Apply impulse to a point relative to the body. This could for example be a point on the Body surface. An impulse is a force added to a body during a short period of time (impulse = force * time). Impulses will be added to Body.velocity and Body.angularVelocity.
649+
* @method applyImpulse
650+
* @param {Array} impulse The impulse vector to add, oriented in world space.
651+
* @param {Array} [relativePoint] A point relative to the body in world space. If not given, it is set to zero and all of the impulse will be excerted on the center of mass.
652+
*/
653+
var Body_applyImpulse_velo = vec2.create();
654+
Body.prototype.applyImpulse = function(impulseVector, relativePoint){
655+
if(this.type !== Body.DYNAMIC){
656+
return;
657+
}
658+
659+
// Compute produced central impulse velocity
660+
var velo = Body_applyImpulse_velo;
661+
vec2.scale(velo, impulseVector, this.invMass);
662+
663+
// Add linear impulse
664+
vec2.add(this.velocity, velo, this.velocity);
665+
666+
if(relativePoint){
667+
// Compute produced rotational impulse velocity
668+
var rotVelo = vec2.crossLength(relativePoint, impulseVector);
669+
rotVelo *= this.invInertia;
670+
671+
// Add rotational Impulse
672+
this.angularVelocity += rotVelo;
673+
}
674+
};
675+
676+
/**
677+
* Apply impulse to a point relative to the body. This could for example be a point on the Body surface. An impulse is a force added to a body during a short period of time (impulse = force * time). Impulses will be added to Body.velocity and Body.angularVelocity.
678+
* @method applyImpulseLocal
679+
* @param {Array} impulse The impulse vector to add, oriented in world space.
680+
* @param {Array} [relativePoint] A point relative to the body in world space. If not given, it is set to zero and all of the impulse will be excerted on the center of mass.
681+
*/
682+
var Body_applyImpulse_impulseWorld = vec2.create();
683+
var Body_applyImpulse_pointWorld = vec2.create();
684+
var Body_applyImpulse_pointLocal = vec2.create();
685+
Body.prototype.applyImpulseLocal = function(localImpulse, localPoint){
686+
localPoint = localPoint || Body_applyImpulse_pointLocal;
687+
var worldImpulse = Body_applyImpulse_impulseWorld;
688+
var worldPoint = Body_applyImpulse_pointWorld;
689+
this.vectorToWorldFrame(worldImpulse, localImpulse);
690+
this.vectorToWorldFrame(worldPoint, localPoint);
691+
this.applyImpulse(worldImpulse, worldPoint);
692+
};
693+
647694
/**
648695
* Transform a world point to local body frame.
649696
* @method toLocalFrame
650697
* @param {Array} out The vector to store the result in
651-
* @param {Array} worldPoint The input world vector
698+
* @param {Array} worldPoint The input world point
652699
*/
653700
Body.prototype.toLocalFrame = function(out, worldPoint){
654701
vec2.toLocalFrame(out, worldPoint, this.position, this.angle);
@@ -658,12 +705,32 @@ Body.prototype.toLocalFrame = function(out, worldPoint){
658705
* Transform a local point to world frame.
659706
* @method toWorldFrame
660707
* @param {Array} out The vector to store the result in
661-
* @param {Array} localPoint The input local vector
708+
* @param {Array} localPoint The input local point
662709
*/
663710
Body.prototype.toWorldFrame = function(out, localPoint){
664711
vec2.toGlobalFrame(out, localPoint, this.position, this.angle);
665712
};
666713

714+
/**
715+
* Transform a world point to local body frame.
716+
* @method vectorToLocalFrame
717+
* @param {Array} out The vector to store the result in
718+
* @param {Array} worldVector The input world vector
719+
*/
720+
Body.prototype.vectorToLocalFrame = function(out, worldVector){
721+
vec2.vectorToLocalFrame(out, worldVector, this.angle);
722+
};
723+
724+
/**
725+
* Transform a local point to world frame.
726+
* @method vectorToWorldFrame
727+
* @param {Array} out The vector to store the result in
728+
* @param {Array} localVector The input local vector
729+
*/
730+
Body.prototype.vectorToWorldFrame = function(out, localVector){
731+
vec2.vectorToGlobalFrame(out, localVector, this.angle);
732+
};
733+
667734
/**
668735
* Reads a polygon shape path, and assembles convex shapes from that and puts them at proper offset points.
669736
* @method fromPolygon

0 commit comments

Comments
 (0)