# Calculate segment distance to cover before turning via a pivot

I'm building a visualisation where I have a body that is moving along-a-path, which is comprised of multiple segments, each with an arbitrary angle.

The body is moving along the path and:

• When the body's centre reaches the end of each segment it stops.
• It then rotates around it's centre to align itself with the next segment .
• It starts moving again along the next segment.

I can get this working just fine when the rotation point is the body's centre.

Here's an animation of the body rotating by it's centre (blue dot):

However now I'd like to rotate the body from a pivot point.

How can I calculate the distance I should cover in each segment before I stop and start turning around my pivot, so when the rotation ends my body's centre lies exactly in the centre of the next segment?

In short, when the body is moving it's centre must always lie on the segment line it moves on.

Here's an animation of the body rotating by it's pivot point (red dots):

In the above example the body overshoots the position on each segment where it should stop and start rotating, thus when it starts moving again - it's centre doesn't lie on the path.

FWIW I've got some code for this working in a browser sandbox, available here

If your body's centre must always be on a segment while moving in straight line, your problem looks impossible to me when the rotation pivot is "in front" of the body centre. If you allow the centre to only lie on the (infinite) line through the (finite) segment, consider the situation below.

To preserve the front of the body at the front, you should rotate by the same angle $\theta$ between the two line segments. Let $O'$ be the rotation of $O$ around the pivot, the locus of $O'$ as $O$ moves along the first line, is a line parallel to that first line. Intersect that locus with the second line, and you'll obtain the location of $O'$ after the appropriate rotation, from which you can deduce the position of the pivot, and the position of the body centre $O$.

• @NicholasKyriakides I just wanted to make sure, so I prefaced my answer with that. But basically the rest of my answer is about where to stop on the infinite line $A$ so that the centre of your body ends up on the infinite line $B$ after rotating around your pivot. Depending on the specific position of your pivot wrt your centre, it's not always possible to keep your centre on the (finite) segments with only a rotation around the pivot. Jan 24 '18 at 1:54
• Ok - That's great, thanks! Jan 24 '18 at 2:13
• So basically this means that I should: a) Find the next segments theta angle - b) Place a point on the centre of the body - c) Rotate that point around my pivot by 'theta' angle - d) start moving - e) stop when that point intersects the infinite line of the next segment. Am I getting this correctly? Jan 24 '18 at 14:49
• @NicholasKyriakides Yes, that is one (among many) possible implementation of the strategy I presented. The "best" implementation would depend on what exactly you are trying to do with your visualisation, and what your body is supposed to model. Though if it is purely for visualisation purposes, you can "cheat" a little and pre-process your path in advance, if you find it simpler to handle pre-processed paths. Jan 24 '18 at 16:59

You should turn when pivot point $N$ reaches the angle bisector of $\angle ABC$, with a rotation of $2\angle BNO$. But the rotated body is turned by $180°-2\angle BON$ with respect to the direction of the path.

To make this work, then, you must choose your pivot so that $\angle BON=90°$.

• I edited my answer because I realized that your idea is feasible with a particular choice of the pivot. Jan 24 '18 at 7:45
• But wait - I can't arbitrarily choose my pivot positions. They are at a fixed position w.r.t the body's centre Jan 24 '18 at 14:57
• In that case you must follow the approach suggested by N.Bach. Jan 24 '18 at 15:39