How to simulate the true reflective movement of a particle bouncing around in an ellipse?
Edit V10!
This is simple example what we can now do in real time!
R = RegionUnion @@ Table[Disk[{Cos[i], Sin[i]}, .4], {i, 0, 2 Pi, Pi/6.}];
R2 = RegionBoundary@DiscretizeRegion@R;
go[] := (While[r > .105, x += v; r = RegionDistance[R2, x]; Pause[.01]]; bounce[];)
bounce[] := With[{normal = Normalize[x - RegionNearest[R2, x]]},
If[break, Abort[]];
v = .01 Normalize[v - 2 v.normal normal];
x = x + v;
r = RegionDistance[R2, x]; go[]
]
x = {1, 0.};
pos = {x};
break = False;
v = .01 Normalize@{2, 1.};
r = RegionDistance[R2, x];
RegionPlot[R2, Epilog -> Dynamic@Disk[x, .1], AspectRatio -> Automatic]
Button["break at edge", break = True;]
go[]
This is an example, not perfect but nice enough to start.
V9
Unfortunately I don't have time to explain now. But take a look at wikipedia ellips site, tangent line part especially.
DynamicModule[{u = 0, t0, imp, v1, x0 = {0, .49}, v0 = {.5, -1.0}, t, a = 1, b = .5,
c, f1, f2},
DynamicWrapper[
Graphics[{ Thick, Scale[Circle[], {a, b}], AbsolutePointSize@7, Dynamic@Point[x0],
Dashed, Thin, Dynamic@Line[{{x0, imp}, {imp, imp + Normalize@v1},
{imp - normal, imp + normal}}]
}, PlotRange -> 1.1, ImageSize -> 500, Frame -> True],
Refresh[
If[(#/a)^2 + (#2/b)^2 & @@ x0 < 1,
x0 += v0;,
x0 = imp + v1; v0 = v1; rec]
, TrackedSymbols :> {}, UpdateInterval -> .001]]
,
Initialization :> (
c = Sqrt[a^2 - b^2]; v0 = Normalize[v0]/100; f1 = {-c, 0}; f2 = {c, 0};
rec := ({t0, imp} = {t, x0 + t v0
} /. Quiet@NSolve[(#/a)^2 + (#2/b)^2 & @@ (x0 + t v0) == 1. &&
t > 0, t, Reals][[1]];
normal = Normalize[Normalize[imp - f1] + Normalize[imp - f2]];
v1 = Normalize[v0 - 2 normal (v0.normal)]/100;(*bounce*));
rec)]
My goal was quite ambitious. I wanted to create a way to let any rigid body bounce elastically against any other surface in MMA V9. To do this I use "masks" for the object and the environment. These masks are black and white images. White indicates that this is where the object/surface is, black is empty space. I can calculate the overlap between the object and the surface using Mathematica's image processing functions. Using the overlap I can calculate the normal of the surface. After that it's simple physics to change the velocity of the object accordingly. The code looks like this:
obj[mask_] := Graphics[{
White, mask
},
PlotRange -> {{0, 500}, {0, 500}},
ImageSize -> {500, 500},
Background -> Black
]
forceVector[obj_, env_, center_] := N@Normalize[Plus @@ (center - # & /@ PixelValuePositions[ImageMultiply[obj, env], 1])]
step[{pt_, v_}] := Module[{f, nv},
f = forceVector[obj[Disk[pt, 20]], ColorNegate@obj[Disk[{250, 250}, {100, 200}]], pt] /. (0. -> {0, 0});
nv = If[v.f < 0, v - 2 v.f f, v];
{pt + nv, nv}
]
pts = NestList[step, {{250, 250}, {1, 2}}, 1000];
frames = Graphics[{
Black, Rectangle[{0, 0}, {500, 500}],
White, Disk[{250, 250}, {100, 200}],
Orange, Disk[#, 20]
},
PlotRange -> {{0, 500}, {0, 500}},
ImageSize -> {500, 500}
] & /@ pts[[All, 1]];
ListAnimate[frames]
Here's a gif with a reduced number of frames:
One can play with the velocity of the disk as well as the number of frames to get a longer path without as many calculations. This method is not very fast.
If you don't have the time/computing power to pre-compute the position list, you can still view the simulation using the code below. It will probably be very slow on many computers though (which is why I chose to pre-compute positions):
DynamicModule[{pt = {250, 250}, v = {6, 2}, f},
Dynamic[
f = forceVector[obj[Disk[pt, 20]],
ColorNegate@obj[Disk[{250, 250}, {100, 200}]],
pt] /. (0. -> {0, 0});
If[v.f < 0, v = v - 2 v.f f];
pt = pt + v;
Graphics[{
Black, Rectangle[{0, 0}, {500, 500}],
White, Disk[{250, 250}, {100, 200}],
Orange, Disk[pt, 20]
},
PlotRange -> {{0, 500}, {0, 500}},
ImageSize -> {500, 500}
]
]
]