This is what I have, and I am going to share my findings with all of you.
public void calculate() { // Center of circle is at (250, 250). Radius is 40. //THIS ALGORITHM IS PROVEN TO BE BETTER THAN I FEARED... /* What it does: * Moves object around in a circle, if object is * inside of circle. * Does not move the object towards the center, * nor outwards. This is crucial. * Object always stays on the rim of the circle, * if the collision detection allows it to. * * Algorithm I used. (DOES WORK, NOT EXPECTING THIS THOUGH.): * N is normalized vector. * R = -2*(V dot N)*N + V */ double nx = x - 250; double ny = y - 250; double nd = Math.hypot(nx, ny); if (nd < 40){ vx += Accelero.X * 0.1; vy += Accelero.Y * 0.1; x -= vx; y -= vy; vx *= 0.9; vy *= 0.9; return; } vx += Accelero.X * 0.1; vy += Accelero.Y * 0.1; 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; }
I turned on collision detection inside my function, basically making this function not as efficient as possible. Ignore this, as this is not the main focus.
The radius of the circle is 40, the position (x, y) is (250 250).
Only when the object is either on the circle or further from the center of the circle, we must calculate the collision reaction, which is given by the algorithm R = -2 * (V dot N) * N + V, where the normal vector N is already normalized.
The algorithm is really correct, this is the logical condition for my collision detection - this is what makes the object stay on the rim of the circle and go back and forth.
I did not say the other algorithm provided by @trashgod is wrong. This is due to some strange problem that somehow makes the object move unusually. I would have guessed that the API, I am using an error that does not allow doubling, but I may be incorrect. I just could not find the source of the problem. Which I am also glad not to look into her further.
A boolean collision detection condition can change everything if it has been slightly modified. If it werenโt for @nm indicating that I somehow forgot the minus sign (in this case, the NOT sign), I probably never understood how trivial it would be.