Say we have a square and a point inside. How to make the point bounce off when it hits the walls. No gravity.
EDIT: New here, should've been more specific. The point has to fly in some random direction, not simply from side to side. What Kuba has commented
x = 0.5;
y = 0.5;
vx = 1;
vy = Pi/2;
step = 0.01;
radius = 0.05;
Animate[
x = x + vx*step;
y = y + vy*step;
If[Abs[x - 1] <= radius || Abs[x] <= radius , vx = -vx];
If[Abs[y - 1] <= radius || Abs[y] <= radius, vy = -vy];
Graphics[{
Cyan, Rectangle[{0, 0}, {1, 1}],
Gray, Disk[{x, y}, radius],
Point[{0.0, 0.0}], Point[{1.0, 1.0}]
}],
{t, 0, Infinity}
]
seems cool, but it's a disk and if I were to draw a line inside the rectangle, it would simply fly through it. I'm currently checking out the other solutions on that page, but having a hard time since they're a bit too complex for my purpose.
Basically, what I need is a point bouncing off any line(s) I draw. I just used a square as an example. Hopefully this is more understandable.
