I am developing the software to control a common fly's flight in Opensim simulator, although I am facing some troubles determining when the fly must maneuver to avoid hitting a wall.


  • How do I get the point of collision between the fly and the wall?
  • How do I know if I have to turn clockwise or counterclockwise?

I have access to the following data:

  • Position of the center of wall in world coordinates.
  • Two additional points representing the nearest end of the wall and the furthest, in local coordinates of the wall.
  • Position of the fly in world coordinates.
  • Vector representing the direction (force) of the fly in local coordinates.
  • The difference, in radians, of the position of the local axis of the fly with regard to the global axis.

Example: a fly moving from left to right, and a wall of 14m wide and 8m tall. enter image description here

There are several axis in the world: one axis per object, situated at its center and initially with the same orientation as the global axis; and one unique global axis, at one end of the world.
The global axis is used to place objects in the world.

My idea so far is to calculate the intersection of the fly's predicted path with the wall, and then, with the point of collision, calculate the distance and maneuver to avoid that collision. The problem is that I have not been able to calculate that point with the information neither the direction of the rotation (clockwise or counterclockwise).

Valid assumption

  • The wall is an infinite plane.
  • Fly's rotation only occur around z-axis.
  • I am a bit geometry newbie.
  • $\begingroup$ If you know the position of the fly and his velocity, can't you use that to define a line of infinite length and then find where that line hits the wall. It doesn't matter if you're using world or wall coordinates, as long as use the same for both. As for direction, the sign of the determinant will tell you if one vector is to the right or left of another, though I am sure there are better ways. $\endgroup$ – AnonSubmitter85 Jan 24 '14 at 19:54
  • $\begingroup$ @AnonSubmitter85 that's the problem, how do I get a line with a vector and an angle? How do I interset that line with a plane defined by 3 points? I don't really know what a determinant is (I've updated the assumptions ;) ) $\endgroup$ – eversor Jan 25 '14 at 2:43

You know where the fly is, $\mathbf{r}_{fly}$, and its velocity $\mathbf{v}_{fly}$. For $\alpha \in \mathbb{R}$, we have the following line that goes the through fly oriented with its velocity:

$$ \mathbf{r} = \mathbf{r}_{fly} + \alpha \cdot \mathbf{v}_{fly}. $$

Convince yourself that as $\alpha$ varies from $-\infty$ to $+\infty$, all points on this line are covered, which means that there must exist some value of $\alpha$ that tells us where the line hits the wall -- assuming that the line hits the wall at all.

For a point to be part of the wall, the vector from the wall's center (actually it's any other point on the wall, but it helps to choose a fixed reference point), $\mathbf{r}_{wall}$, to said point must be orthogonal to the wall's normal vector, $\mathbf{n}$:

$$ \mathbf{n}^{\mathrm{T}}(\mathbf{r} - \mathbf{r}_{wall}) = 0. $$

We can combine these two relations to get

$$ \mathbf{n}^{\mathrm{T}} \left( \mathbf{r}_{fly} + \alpha \cdot \mathbf{v}_{fly} - \mathbf{r}_{wall} \right) = 0. $$

Everything in the above equation except for $\alpha$ is known, which is easily solved for:

$$ \alpha = {-{\mathbf{n}^{\mathrm{T}} (\mathbf{r}_{fly} - \mathbf{r}_{wall}) } \over {\mathbf{n}^{\mathrm{T}} \mathbf{v}_{fly}} } $$

If the denominator is $0$, then we have an undefined equation. However, note that the denominator is only zero if the velocity is orthogonal to the wall's normal vector, that is, the fly is moving parallel to the wall. In which case, the line never hits the wall and there is no solution to the equation. Otherwise, plugging the above value of $\alpha$ back into the equation for the position of the fly will tell you where he hits the wall. Another case of note is when $\alpha < 0$. This would mean that fly is moving away from the wall.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.