Wollte, dass ein Objekt innerhalb eines Kreises abprallt, und bewegte das Objekt am Rand des Kreises entlang

Hier ist der fragliche Code:

public void calculate() {
        // Center of circle is at (250, 250).
        //THIS ALGORITHM IS NOW PROVEN TO BE WORSE THAN I FEARED...

        /*      What it does:
         *          Moves object around in a circle.
         *          Does not move the object towards the center.
         *          Object always stays on the rim of the circle.
         * 
         *      Algorithm I used. (DOES NOT WORK):
         *          N is normalized vector. 
         *          R = -2*(V dot N)*N + V
         */

        vx += Accelero.X * 0.1;
        vy += Accelero.Y * 0.1;
        double nx = x - 250;
        double ny = y - 250;
        double nd = Math.hypot(nx, ny);
        if (nd == 0)
            nd = 1;
        nx /= nd;
        ny /= nd;
        double dotProduct = vx * nx + vy * ny;
        vx += (float) (-2 * dotProduct * nx);
        vy += (float) (-2 * dotProduct * ny);
        x -= vx * 2;
        y -= vy * 2;
        vx *= 0.99;
        vy *= 0.99;
    }

Und genau das passiert.

Die schwarze Linie, die Sie sehen, ist die Position, an der sich das lila Objekt (Kästchen) bewegt. Es ist einfach so, dass es genau auf der Kreislinie liegt, mit der ich gezeichnet habeCanvas.drawCircle().

Ich verstehe nicht, warum das Nachdenken nicht funktioniert hat. Wenn ein Objekt gegen eine kreisförmige Wand stoßen soll, sollte es dann nicht die Geschwindigkeitsrichtung des Objekts widerspiegeln, wie es der Algorithmus sein sollte? Oder habe ich den falschen Algorithmus verwendet?

Jede Hilfe wird geschätzt. Danke im Voraus.

Antworten auf die Frage(3)

Ihre Antwort auf die Frage