Skip to content

Commit 86b0444

Browse files
committed
applyImpulse and applyForce are now using relative points instead of world points
1 parent 20b7eee commit 86b0444

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

src/objects/Body.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -671,22 +671,18 @@ Body.prototype.updateInertiaWorld = function(force){
671671
* Apply force to a world point. This could for example be a point on the Body surface. Applying force this way will add to Body.force and Body.torque.
672672
* @method applyForce
673673
* @param {Vec3} force The amount of force to add.
674-
* @param {Vec3} worldPoint A world point to apply the force on.
674+
* @param {Vec3} relativePoint A point relative to the center of mass to apply the force on.
675675
*/
676676
var Body_applyForce_r = new Vec3();
677677
var Body_applyForce_rotForce = new Vec3();
678-
Body.prototype.applyForce = function(force,worldPoint){
679-
if(this.type !== Body.DYNAMIC){
678+
Body.prototype.applyForce = function(force,relativePoint){
679+
if(this.type !== Body.DYNAMIC){ // Needed?
680680
return;
681681
}
682682

683-
// Compute point position relative to the body center
684-
var r = Body_applyForce_r;
685-
worldPoint.vsub(this.position,r);
686-
687683
// Compute produced rotational force
688684
var rotForce = Body_applyForce_rotForce;
689-
r.cross(force,rotForce);
685+
relativePoint.cross(force,rotForce);
690686

691687
// Add linear force
692688
this.force.vadd(force,this.force);
@@ -702,39 +698,38 @@ Body.prototype.applyForce = function(force,worldPoint){
702698
* @param {Vec3} localPoint A local point in the body to apply the force on.
703699
*/
704700
var Body_applyLocalForce_worldForce = new Vec3();
705-
var Body_applyLocalForce_worldPoint = new Vec3();
701+
var Body_applyLocalForce_relativePointWorld = new Vec3();
706702
Body.prototype.applyLocalForce = function(localForce, localPoint){
707703
if(this.type !== Body.DYNAMIC){
708704
return;
709705
}
710706

711707
var worldForce = Body_applyLocalForce_worldForce;
712-
var worldPoint = Body_applyLocalForce_worldPoint;
708+
var relativePointWorld = Body_applyLocalForce_relativePointWorld;
713709

714710
// Transform the force vector to world space
715711
this.vectorToWorldFrame(localForce, worldForce);
716-
this.pointToWorldFrame(localPoint, worldPoint);
712+
this.vectorToWorldFrame(localPoint, relativePointWorld);
717713

718-
this.applyForce(worldForce, worldPoint);
714+
this.applyForce(worldForce, relativePointWorld);
719715
};
720716

721717
/**
722718
* Apply impulse to a world point. 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.
723719
* @method applyImpulse
724720
* @param {Vec3} impulse The amount of impulse to add.
725-
* @param {Vec3} worldPoint A world point to apply the force on.
721+
* @param {Vec3} relativePoint A point relative to the center of mass to apply the force on.
726722
*/
727723
var Body_applyImpulse_r = new Vec3();
728724
var Body_applyImpulse_velo = new Vec3();
729725
var Body_applyImpulse_rotVelo = new Vec3();
730-
Body.prototype.applyImpulse = function(impulse, worldPoint){
726+
Body.prototype.applyImpulse = function(impulse, relativePoint){
731727
if(this.type !== Body.DYNAMIC){
732728
return;
733729
}
734730

735731
// Compute point position relative to the body center
736-
var r = Body_applyImpulse_r;
737-
worldPoint.vsub(this.position,r);
732+
var r = relativePoint;
738733

739734
// Compute produced central impulse velocity
740735
var velo = Body_applyImpulse_velo;
@@ -766,20 +761,20 @@ Body.prototype.applyImpulse = function(impulse, worldPoint){
766761
* @param {Vec3} localPoint A local point in the body to apply the force on.
767762
*/
768763
var Body_applyLocalImpulse_worldImpulse = new Vec3();
769-
var Body_applyLocalImpulse_worldPoint = new Vec3();
764+
var Body_applyLocalImpulse_relativePoint = new Vec3();
770765
Body.prototype.applyLocalImpulse = function(localImpulse, localPoint){
771766
if(this.type !== Body.DYNAMIC){
772767
return;
773768
}
774769

775770
var worldImpulse = Body_applyLocalImpulse_worldImpulse;
776-
var worldPoint = Body_applyLocalImpulse_worldPoint;
771+
var relativePointWorld = Body_applyLocalImpulse_relativePoint;
777772

778773
// Transform the force vector to world space
779774
this.vectorToWorldFrame(localImpulse, worldImpulse);
780-
this.pointToWorldFrame(localPoint, worldPoint);
775+
this.vectorToWorldFrame(localPoint, relativePointWorld);
781776

782-
this.applyImpulse(worldImpulse, worldPoint);
777+
this.applyImpulse(worldImpulse, relativePointWorld);
783778
};
784779

785780
var Body_updateMassProperties_halfExtents = new Vec3();

0 commit comments

Comments
 (0)