Skip to content

Commit 663f70f

Browse files
committed
ConvexPolyhedron.pointIsInside optimize
1 parent 62b1116 commit 663f70f

4 files changed

Lines changed: 28 additions & 10 deletions

File tree

build/cannon.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2952,22 +2952,31 @@ CANNON.ConvexPolyhedron.prototype.transformAllPoints = function(offset,quat){
29522952
// The point lies outside of the convex hull of the other points
29532953
// if and only if the direction of all the vectors from it to those
29542954
// other points are on less than one half of a sphere around it.
2955+
var ConvexPolyhedron_pointIsInside = new CANNON.Vec3();
2956+
var ConvexPolyhedron_vToP = new CANNON.Vec3();
2957+
var ConvexPolyhedron_vToPointInside = new CANNON.Vec3();
29552958
CANNON.ConvexPolyhedron.prototype.pointIsInside = function(p){
29562959
var n = this.vertices.length,
29572960
verts = this.vertices,
29582961
faces = this.faces,
29592962
normals = this.faceNormals;
29602963
var positiveResult = null;
29612964
var N = this.faces.length;
2962-
var pointInside = this.getAveragePointLocal();
2965+
var pointInside = ConvexPolyhedron_pointIsInside;
2966+
this.getAveragePointLocal(pointInside);
29632967
for(var i=0; i<N; i++){
29642968
var numVertices = this.faces[i].length;
29652969
var n = normals[i];
29662970
var v = verts[faces[i][0]]; // We only need one point in the face
29672971

29682972
// This dot product determines which side of the edge the point is
2969-
var r1 = n.dot(p.vsub(v));
2970-
var r2 = n.dot(pointInside.vsub(v));
2973+
var vToP = ConvexPolyhedron_vToP;
2974+
p.vsub(v,vToP);
2975+
var r1 = n.dot(vToP);
2976+
2977+
var vToPointInside = ConvexPolyhedron_vToPointInside;
2978+
pointInside.vsub(v,vToPointInside);
2979+
var r2 = n.dot(vToPointInside);
29712980

29722981
if((r1<0 && r2>0) || (r1>0 && r2<0)){
29732982
return false; // Encountered some other sign. Exit.
@@ -4714,7 +4723,7 @@ CANNON.ContactGenerator = function(){
47144723
sj.faceNormals[i].copy(normal);
47154724
normal.normalize();
47164725
qj.vmult(normal,normal);
4717-
4726+
47184727
// Check how much the particle penetrates the polygon plane.
47194728
var penetration = -normal.dot(xi.vsub(verts[0]));
47204729
if(minPenetration===null || Math.abs(penetration)<Math.abs(minPenetration)){

build/cannon.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/objects/ConvexPolyhedron.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,22 +697,31 @@ CANNON.ConvexPolyhedron.prototype.transformAllPoints = function(offset,quat){
697697
// The point lies outside of the convex hull of the other points
698698
// if and only if the direction of all the vectors from it to those
699699
// other points are on less than one half of a sphere around it.
700+
var ConvexPolyhedron_pointIsInside = new CANNON.Vec3();
701+
var ConvexPolyhedron_vToP = new CANNON.Vec3();
702+
var ConvexPolyhedron_vToPointInside = new CANNON.Vec3();
700703
CANNON.ConvexPolyhedron.prototype.pointIsInside = function(p){
701704
var n = this.vertices.length,
702705
verts = this.vertices,
703706
faces = this.faces,
704707
normals = this.faceNormals;
705708
var positiveResult = null;
706709
var N = this.faces.length;
707-
var pointInside = this.getAveragePointLocal();
710+
var pointInside = ConvexPolyhedron_pointIsInside;
711+
this.getAveragePointLocal(pointInside);
708712
for(var i=0; i<N; i++){
709713
var numVertices = this.faces[i].length;
710714
var n = normals[i];
711715
var v = verts[faces[i][0]]; // We only need one point in the face
712716

713717
// This dot product determines which side of the edge the point is
714-
var r1 = n.dot(p.vsub(v));
715-
var r2 = n.dot(pointInside.vsub(v));
718+
var vToP = ConvexPolyhedron_vToP;
719+
p.vsub(v,vToP);
720+
var r1 = n.dot(vToP);
721+
722+
var vToPointInside = ConvexPolyhedron_vToPointInside;
723+
pointInside.vsub(v,vToPointInside);
724+
var r2 = n.dot(vToPointInside);
716725

717726
if((r1<0 && r2>0) || (r1>0 && r2<0)){
718727
return false; // Encountered some other sign. Exit.

src/world/ContactGenerator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ CANNON.ContactGenerator = function(){
618618
sj.faceNormals[i].copy(normal);
619619
normal.normalize();
620620
qj.vmult(normal,normal);
621-
621+
622622
// Check how much the particle penetrates the polygon plane.
623623
var penetration = -normal.dot(xi.vsub(verts[0]));
624624
if(minPenetration===null || Math.abs(penetration)<Math.abs(minPenetration)){

0 commit comments

Comments
 (0)