I have a ray shot on the inside of an ellipse, it needs to reflect off of the ellipse and continue flying inside the ellipse. I've found that the easiest might be to find a tangent vector to the impact point and from there calculate the reflection angle.
Though I'm not sure how to implement this in code.
Here is what I have so far, I have the following function that draws the ellipse with its semi-major (a) and semi-minor (b) axes. t is incremented in an update function.
void drawEllipse(float t) {
float x = a * cos(t);
float y = b * sin(t);
PVector ellipseDotPosition = new PVector(x, y).add(ellipseCenter);
circle(ellipseDotPosition.x, ellipseDotPosition.y, 2);
}
I determine whether the ray has collided using this function:
boolean hasCollided(PVector pointToCheck) {
return pow((pointToCheck.x - ellipseCenter.x) / a, 2) +
pow((pointToCheck.y - ellipseCenter.y) / b, 2) > 1;
}
Here is how I've tried to calculate the reflection vector.
PVector getReflectionVector(PVector pointOfImpact) {
// Differentiate to get tangent to the collision point.
// PVector tangent = new PVector(-a / b * pointOfImpact.y, b / a * pointOfImpact.x);
// PVector tangent = new PVector(-(a * pointOfImpact.y / b), (b * pointOfImpact.x / a));
// circle(tangent.x, tangent.y, 10);
// Treat the tangent as the surface to be reflected off.
// Determine angle of reflection off the surface.
// Return reflection vector.
return new PVector();
}
Am I bad at math? Yes. Please help :)