JavaScript Separate Axis Theorem

I am trying to wrap my head using the dividing axis theorem in JavaScript to detect two squares colliding (one turned, one not). As far as I try, I can’t figure out how this will look in JavaScript, and I won’t be able to find any JavaScript examples. Please help, an explanation using primes or JavaScript code will be most helpful.


Update: After learning a lot of geometries and mathematical theories, I decided to release a simplified implementation of SAT in the GitHub repo. You can find a working copy of SAT in JavaScript here: https://github.com/ashblue/canvas-sat

+3
source share
1 answer

( ), , angle.

, .. . Matrix , ( , ).

:

var transform = new Matrix();
transform.appendRotation(alpha);
points = transform.transformPoints(points);

points - Point .

, , . , , ( ), :

  • ( 0 1):
    • polgyons "", "" "" .
    • (1 "" 1 "" ), , ( ).
  • , polgyons: /.

, " " - , , .

, / . , . , ( , ).

, :

// this code assumes p0 and p1 are instances of some Vector3D class

var p0 = edge[0]; // first point of edge
var p1 = edge[1]; // second point of edge
var v = p1.subtract(p0);
var normal = new Vector3D(0, 0, 1).crossProduct(v);
normal.normalize();

z- . , .

: SAT.

, , ( ) :

// point is the point to classify as "in front" or "behind" the edge
var point = point.subtract(p0);
var distance = point.dotProduct(normal);
var inFront = distance >= 0;

inFront true, , false .

, , , , 1 1 , , ( ).

, , . js Matrix Vector3D . () Point Edge.

, .

+3

All Articles