Ich versuche, die SAT (Trennung von Achsensatz) in ein kleines Programm zu implementieren. Ich habe eine for-Schleife, die dreimal durchlaufen werden soll, aber sie tut dies nur einmal, bevor sie ausbricht und mit dem nächsten Teil des Codes fortfährt. Es gibt keinen offensichtlichen Grund, dass es das tun sollte ... Hier ist die for-Schleife. Ich habe die Funktionen, die aus der folgenden Schleife aufgerufen werden, auch aufgenommen.Warum ist diese p5js for-Schleife nicht iterieren den ganzen Weg
this.collides = function(shape) {
axes1 = this.getAxes();
axes2 = shape.getAxes();
for (i = 0; i < 3; i++) {
console.log(i + 1)
axis = axes1[i];
pr1 = this.projectShape(axis)
pr2 = shape.projectShape(axis)
if (!this.overlaps(pr1, pr2)) {
console.log("no collision")
return false;
}
}
}
}
Wie Sie den Code sehen kann, sollte 1,2,3 an der Konsole anmelden aber es meldet sich nur 1.
"getAxes()" Funktion:
this.getAxes = function() {
axes = []
for (i = 0; i < this.vertices.length; i++) {
p1 = this.vertices[i]
if (i === this.vertices.length - 1) {
p2 = this.vertices[0]
} else {
p2 = this.vertices[i + 1]
}
edge = createVector(p1.x - p2.x, p1.y - p2.y)
normal = createVector(-edge.y, edge.x)
// the perp method is just (x, y) => (-y, x) or (y, -x)
axes[i] = normal
}
return axes;
}
„projectShape ()“Funktion:
this.projectShape = function(axisToProj) {
minV = axisToProj.dot(this.vertices[0]);
maxV = minV;
for (i = 1; i < this.vertices.length; i++) {
// NOTE: the axis must be normalized to get accurate projections
p = axisToProj.dot(this.vertices[i]);
if (p < minV) {
minV = p;
} else if (p > maxV) {
maxV = p;
}
}
minMax = createVector(minV, maxV);
return minMax;
}
"Überlappungen()" Funktion:
012.351.this.overlaps = function(a, b) {
if (a.y > b.x && b.y > a.x) {
return true
}
return false
}
@KevinWorkman Ja, ich habe es geschafft, es zu beheben. Ich erinnere mich vage daran, dass das Problem etwas mit Variablen in der for-Schleife zu tun hatte, die miteinander in Konflikt standen. Ich glaube, dass ich statt "i" in anderen for-Schleifen zum Beispiel "j" verwende. Ich habe auch den gesamten Teil des Programms umgeschrieben und [hier] (https://github.com/MaxBryarsMansell/Velavity/blob/collision/Velavity/collide.js) ist es, wenn Sie interessiert sind. – Max
Okay, cool. Der Grund, warum ich gefragt habe, ist, dass diese Frage immer noch als Antwort auf die Frage markiert ist, also wollte ich nur weitermachen. Ich bin froh, dass du es herausgefunden hast. –