mirror of
https://github.com/RandomityGuy/MBHaxe.git
synced 2025-10-30 08:11:25 +00:00
sap webport test
This commit is contained in:
parent
953185cb61
commit
10c1eb5650
1 changed files with 131 additions and 104 deletions
|
|
@ -4,148 +4,175 @@ package collision;
|
||||||
@:publicFields
|
@:publicFields
|
||||||
class SAPProxy {
|
class SAPProxy {
|
||||||
var object:CollisionEntity;
|
var object:CollisionEntity;
|
||||||
var flags:Int;
|
var index:Int;
|
||||||
var intersections:Array<CollisionEntity>;
|
var value:Float;
|
||||||
var positions:Array<Int>;
|
var left:Bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
@:structInit
|
||||||
|
@:publicFields
|
||||||
|
class SAPProxyHolder {
|
||||||
|
var isec:Int;
|
||||||
|
var xMinProxy:SAPProxy;
|
||||||
|
var yMinProxy:SAPProxy;
|
||||||
|
var zMinProxy:SAPProxy;
|
||||||
|
var xMaxProxy:SAPProxy;
|
||||||
|
var yMaxProxy:SAPProxy;
|
||||||
|
var zMaxProxy:SAPProxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
class SAP {
|
class SAP {
|
||||||
var dimXEdges:Array<Float> = [];
|
var dimX:Array<SAPProxy> = [];
|
||||||
var dimXEdgeLefts:Array<Bool> = [];
|
var dimY:Array<SAPProxy> = [];
|
||||||
var dimXEdgeOwner:Array<Int> = [];
|
var dimZ:Array<SAPProxy> = [];
|
||||||
var dimYEdges:Array<Float> = [];
|
|
||||||
var dimYEdgeLefts:Array<Bool> = [];
|
|
||||||
var dimYEdgeOwner:Array<Int> = [];
|
|
||||||
var dimZEdges:Array<Float> = [];
|
|
||||||
var dimZEdgeLefts:Array<Bool> = [];
|
|
||||||
var dimZEdgeOwner:Array<Int> = [];
|
|
||||||
|
|
||||||
var objects:Array<SAPProxy> = [];
|
var objects:Array<CollisionEntity> = [];
|
||||||
var objToProxy:Map<CollisionEntity, SAPProxy> = [];
|
var flags:Array<Int> = [];
|
||||||
|
var intersections:Array<Array<CollisionEntity>> = [];
|
||||||
|
var objToProxy:Map<CollisionEntity, SAPProxyHolder> = [];
|
||||||
var needsSort = true;
|
var needsSort = true;
|
||||||
|
var anyFlagSet = false;
|
||||||
|
|
||||||
public function new() {}
|
public function new() {}
|
||||||
|
|
||||||
public function addEntity(obj:CollisionEntity) {
|
public function addEntity(obj:CollisionEntity) {
|
||||||
needsSort = true;
|
|
||||||
var edgeLen = dimXEdges.length;
|
|
||||||
var proxy:SAPProxy = {
|
|
||||||
object: obj,
|
|
||||||
intersections: [],
|
|
||||||
flags: 0,
|
|
||||||
positions: [edgeLen, edgeLen + 1, edgeLen, edgeLen + 1, edgeLen, edgeLen + 1]
|
|
||||||
};
|
|
||||||
var idx = objects.length;
|
var idx = objects.length;
|
||||||
objects.push(proxy);
|
var xMinProxy:SAPProxy = {
|
||||||
dimXEdges.push(obj.boundingBox.xMin);
|
object: obj,
|
||||||
dimXEdges.push(obj.boundingBox.xMax);
|
left: false,
|
||||||
dimYEdges.push(obj.boundingBox.yMin);
|
value: obj.boundingBox.xMin,
|
||||||
dimYEdges.push(obj.boundingBox.yMax);
|
index: idx
|
||||||
dimZEdges.push(obj.boundingBox.zMin);
|
};
|
||||||
dimZEdges.push(obj.boundingBox.zMax);
|
dimX.push(xMinProxy);
|
||||||
dimXEdgeLefts.push(true);
|
var xMaxProxy:SAPProxy = {
|
||||||
dimXEdgeLefts.push(false);
|
object: obj,
|
||||||
dimYEdgeLefts.push(true);
|
left: true,
|
||||||
dimYEdgeLefts.push(false);
|
value: obj.boundingBox.xMax,
|
||||||
dimZEdgeLefts.push(true);
|
index: idx
|
||||||
dimZEdgeLefts.push(false);
|
};
|
||||||
dimXEdgeOwner.push(idx);
|
dimX.push(xMaxProxy);
|
||||||
dimXEdgeOwner.push(idx);
|
|
||||||
dimYEdgeOwner.push(idx);
|
|
||||||
dimYEdgeOwner.push(idx);
|
|
||||||
dimZEdgeOwner.push(idx);
|
|
||||||
dimZEdgeOwner.push(idx);
|
|
||||||
|
|
||||||
objToProxy.set(obj, proxy);
|
var yMinProxy:SAPProxy = {
|
||||||
|
object: obj,
|
||||||
|
left: false,
|
||||||
|
value: obj.boundingBox.yMin,
|
||||||
|
index: idx
|
||||||
|
};
|
||||||
|
dimY.push(yMinProxy);
|
||||||
|
var yMaxProxy:SAPProxy = {
|
||||||
|
object: obj,
|
||||||
|
left: true,
|
||||||
|
value: obj.boundingBox.yMax,
|
||||||
|
index: idx
|
||||||
|
};
|
||||||
|
dimY.push(yMaxProxy);
|
||||||
|
|
||||||
|
var zMinProxy:SAPProxy = {
|
||||||
|
object: obj,
|
||||||
|
left: false,
|
||||||
|
value: obj.boundingBox.zMin,
|
||||||
|
index: idx
|
||||||
|
};
|
||||||
|
dimZ.push(zMinProxy);
|
||||||
|
var zMaxProxy:SAPProxy = {
|
||||||
|
object: obj,
|
||||||
|
left: true,
|
||||||
|
value: obj.boundingBox.zMax,
|
||||||
|
index: idx
|
||||||
|
};
|
||||||
|
dimZ.push(zMaxProxy);
|
||||||
|
|
||||||
|
objects.push(obj);
|
||||||
|
intersections.push([]);
|
||||||
|
objToProxy.set(obj, {
|
||||||
|
xMinProxy: xMinProxy,
|
||||||
|
xMaxProxy: xMaxProxy,
|
||||||
|
yMinProxy: yMinProxy,
|
||||||
|
yMaxProxy: yMaxProxy,
|
||||||
|
zMinProxy: zMinProxy,
|
||||||
|
zMaxProxy: zMaxProxy,
|
||||||
|
isec: idx
|
||||||
|
});
|
||||||
|
|
||||||
|
var oldFlags = flags;
|
||||||
|
flags = [];
|
||||||
|
var n = objects.length - 1;
|
||||||
|
for (i in 0...Std.int((idx * idx + idx) / 2))
|
||||||
|
flags.push(0);
|
||||||
|
if (anyFlagSet) {
|
||||||
|
for (o1 in 0...(objects.length - 1)) {
|
||||||
|
for (o2 in (o1 + 1)...(objects.length - 2)) {
|
||||||
|
// https://stackoverflow.com/questions/27086195/linear-index-upper-triangular-matrix
|
||||||
|
var oldN = n - 1;
|
||||||
|
var oldIndex = Std.int((oldN * (oldN - 1) / 2) - (oldN - o2) * ((oldN - o2) - 1) / 2 + o1 - o2 - 1);
|
||||||
|
var newIndex = Std.int((n * (n - 1) / 2) - (n - o2) * ((n - o2) - 1) / 2 + o1 - o2 - 1);
|
||||||
|
|
||||||
|
this.flags[newIndex] = oldFlags[oldIndex];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
needsSort = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(obj:CollisionEntity) {
|
public function update(obj:CollisionEntity) {
|
||||||
if (!objToProxy.exists(obj))
|
if (!objToProxy.exists(obj))
|
||||||
addEntity(obj);
|
addEntity(obj);
|
||||||
|
|
||||||
|
var proxyHolder = objToProxy.get(obj);
|
||||||
|
proxyHolder.xMinProxy.value = obj.boundingBox.xMin;
|
||||||
|
proxyHolder.xMaxProxy.value = obj.boundingBox.xMax;
|
||||||
|
proxyHolder.yMinProxy.value = obj.boundingBox.yMin;
|
||||||
|
proxyHolder.yMaxProxy.value = obj.boundingBox.yMax;
|
||||||
|
proxyHolder.zMinProxy.value = obj.boundingBox.zMin;
|
||||||
|
proxyHolder.zMaxProxy.value = obj.boundingBox.zMax;
|
||||||
|
|
||||||
needsSort = true;
|
needsSort = true;
|
||||||
var proxy = objToProxy.get(obj);
|
|
||||||
proxy.object = obj;
|
|
||||||
proxy.intersections = [];
|
|
||||||
proxy.flags = 0;
|
|
||||||
dimXEdges[proxy.positions[0]] = obj.boundingBox.xMin;
|
|
||||||
dimXEdges[proxy.positions[1]] = obj.boundingBox.xMax;
|
|
||||||
dimYEdges[proxy.positions[2]] = obj.boundingBox.yMin;
|
|
||||||
dimYEdges[proxy.positions[3]] = obj.boundingBox.yMax;
|
|
||||||
dimZEdges[proxy.positions[4]] = obj.boundingBox.zMin;
|
|
||||||
dimZEdges[proxy.positions[5]] = obj.boundingBox.zMax;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function sort(dim:Int) {
|
public function sort(dim:Int) {
|
||||||
var edges;
|
var edges;
|
||||||
var edgeLefts;
|
|
||||||
var edgeOwner;
|
|
||||||
if (dim == 0) {
|
if (dim == 0) {
|
||||||
edges = dimXEdges;
|
edges = dimX;
|
||||||
edgeLefts = this.dimXEdgeLefts;
|
|
||||||
edgeOwner = this.dimXEdgeOwner;
|
|
||||||
} else if (dim == 1) {
|
} else if (dim == 1) {
|
||||||
edges = dimYEdges;
|
edges = dimY;
|
||||||
edgeLefts = this.dimYEdgeLefts;
|
|
||||||
edgeOwner = this.dimYEdgeOwner;
|
|
||||||
} else {
|
} else {
|
||||||
edges = dimZEdges;
|
edges = dimZ;
|
||||||
edgeLefts = this.dimZEdgeLefts;
|
|
||||||
edgeOwner = this.dimZEdgeOwner;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i in 0...edges.length) {
|
for (i in 0...edges.length) {
|
||||||
var j = i - 1;
|
var j = i - 1;
|
||||||
while (j >= 0) {
|
while (j >= 0) {
|
||||||
if (edges[j] < edges[j + 1])
|
if (edges[j].value < edges[j + 1].value)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Swap
|
// Swap
|
||||||
|
|
||||||
var edge1Owner = objects[edgeOwner[j]];
|
|
||||||
var edge2Owner = objects[edgeOwner[j + 1]];
|
|
||||||
var edge1Left = edgeLefts[j];
|
|
||||||
var edge2Left = edgeLefts[j + 1];
|
|
||||||
|
|
||||||
edge1Owner.positions[2 * dim + (edge1Left ? 1 : 0)] = j + 1;
|
|
||||||
edge2Owner.positions[2 * dim + (edge2Left ? 1 : 0)] = j;
|
|
||||||
|
|
||||||
var tmp = edges[j];
|
var tmp = edges[j];
|
||||||
edges[j] = edges[j + 1];
|
edges[j] = edges[j + 1];
|
||||||
edges[j + 1] = tmp;
|
edges[j + 1] = tmp;
|
||||||
|
|
||||||
var tmp2 = edgeLefts[j];
|
|
||||||
edgeLefts[j] = edgeLefts[j + 1];
|
|
||||||
edgeLefts[j + 1] = tmp2;
|
|
||||||
|
|
||||||
var tmp3 = edgeOwner[j];
|
|
||||||
edgeOwner[j] = edgeOwner[j + 1];
|
|
||||||
edgeOwner[j + 1] = tmp3;
|
|
||||||
|
|
||||||
// Sweep
|
// Sweep
|
||||||
var edge1 = j;
|
var edge1 = edges[j];
|
||||||
var edge2 = j + 1;
|
var edge2 = edges[j + 1];
|
||||||
if (edgeLefts[edge1] && !edgeLefts[edge2]) {
|
|
||||||
var obj1 = edgeOwner[edge1];
|
|
||||||
var obj2 = edgeOwner[edge2];
|
|
||||||
objects[obj1].flags |= (1 << dim);
|
|
||||||
objects[obj2].flags |= (1 << dim);
|
|
||||||
|
|
||||||
if (objects[obj1].flags == 7 && objects[obj2].flags == 7) {
|
var n = objects.length;
|
||||||
objects[obj1].intersections.push(objects[obj2].object);
|
var i1 = edge1.index;
|
||||||
objects[obj2].intersections.push(objects[obj1].object);
|
var i2 = edge2.index;
|
||||||
}
|
var flagIndex = Std.int((n * (n - 1) / 2) - (n - i2) * ((n - i2) - 1) / 2 + i1 - i2 - 1);
|
||||||
} else if (!edgeLefts[edge1] && edgeLefts[edge2]) {
|
|
||||||
var obj1 = edgeOwner[edge2];
|
|
||||||
var obj2 = edgeOwner[edge1];
|
|
||||||
if (objects[obj1].flags == 7) {
|
|
||||||
objects[obj1].intersections.remove(objects[obj2].object);
|
|
||||||
}
|
|
||||||
if (objects[obj2].flags == 7) {
|
|
||||||
objects[obj2].intersections.remove(objects[obj1].object);
|
|
||||||
}
|
|
||||||
|
|
||||||
objects[obj1].flags &= ~(1 << dim);
|
if (edge1.left && !edge2.left) {
|
||||||
objects[obj2].flags &= ~(1 << dim);
|
flags[flagIndex] |= (1 << dim);
|
||||||
|
|
||||||
|
if (flags[flagIndex] == 7) {
|
||||||
|
intersections[edge1.index].push(edge2.object);
|
||||||
|
intersections[edge2.index].push(edge1.object);
|
||||||
|
}
|
||||||
|
} else if (!edge1.left && edge2.left) {
|
||||||
|
if (flags[flagIndex] == 7) {
|
||||||
|
intersections[edge1.index].remove(edge2.object);
|
||||||
|
intersections[edge2.index].remove(edge1.object);
|
||||||
|
}
|
||||||
|
flags[flagIndex] &= ~(1 << dim);
|
||||||
}
|
}
|
||||||
|
|
||||||
j--;
|
j--;
|
||||||
|
|
@ -163,6 +190,6 @@ class SAP {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getIntersections(obj:CollisionEntity):Array<CollisionEntity> {
|
public function getIntersections(obj:CollisionEntity):Array<CollisionEntity> {
|
||||||
return objToProxy[obj].intersections;
|
return intersections[objToProxy[obj].isec];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue