0
\$\begingroup\$

The user clicks a unit to select it for a move. Its going to move to wherever they click next.

It's centre c is point cf They then click a point it is to move to. This is to be where the unit centre moves to ct.

It is simple to work out the bearing between f and t and I know how to work out the delta. What I would like to simulate is a unit wheeling. Think in terms of a napoleonic battalion of a thousand men in 2 or 3 lines. They don't just spin to face the new place they will move to. Turning is handled by wheeling. The unit rotates around the nearest corner until it faces in the direction of ct giving a rotated point rcf and then moves in a straight line finishing with a facing based on the bearing between rcf and ct.

I need to somehow calculate that wheel to get rcf.

My question is: how do I do that?

My first thought was calculate the delta offset from the unit facing and then rotate cf in relation to the nearest corner.

That cannot be right though.

If the unit starts facing directly north and you click west then that delta would be 90 degrees. If you rotate the unit by the left corner by 90 degrees the facing would be out though. The left corner would end up where I want the centre to be.

This is rather like the user is dragging a rectangle projected from the front of the unit around. It rotates around either corner of the unit ending up with it's centre wherever the user clicks.

I'm not using any gaming engine so I'll be writing any c# to do the trig.

Edit.

I don't think my drawing skills are really up to this but I'll see what I can come up with.

The unit must rotate around one corner such that it would move directly forward and end up with it's centre where the user clicks.

One way to work this out would be to calculate the rotated point degree by degree of rotation. Then project out at 90 degrees and see if that hits the required point.

If you imagine opening a door.

There's a stick attached to the centre.

A spot on the floor represents where the unit moves to.

As you open the door the stick moves and will eventually come to that point.

Here's an attempt at a drawing.

unit wheels from blue to red

Bear in mind units can ( could ) only move straight forward but they can also wheel.

The unit starts off in blue position.

The user clicks at a point where the red arrow head is.

This is not on that blue line which projects at 90 degrees to it's centre which is it's original facing (or bearing).

That means it must wheel.

The delta is to the left so it's going to wheel (hinge) around the left corner.

Those guys on the left of the unit stay where they are. Those on the far end do the most movement.

When a line projected from it's centre crosses that point it needs to go to then it's got the right rotation.

This is represented by the red line.

So the unit starts it's move by wheeling from blue to red state.

That point it wants to get to is now straight ahead of it's centre.

So it can then move straight forward to it.

The dotted line is to exaggerate the fact I'm looking to somehow calculate where that central point has to be after it's wheeled and is ready to move directly forward.

Any ideas?

\$\endgroup\$
3
  • \$\begingroup\$ This is definitely a question that is well thought out in your head, but needs some illustrations and some more clarification here \$\endgroup\$ – Natalo77 Apr 15 '20 at 13:04
  • \$\begingroup\$ @Natalo77 I added a picture and more explanation. I hope this is at least clarifies what I'm trying to do. \$\endgroup\$ – Andy Apr 15 '20 at 15:02
  • \$\begingroup\$ In terms of clarification - you have a lot of verbose writing - you should try and condense your writing down and make sure the end of your question text is a question - or include a Tl;Dr: (It's hard to ascertain the question you're actually asking here) \$\endgroup\$ – Natalo77 Apr 15 '20 at 16:48
1
\$\begingroup\$

You've almost got it in the diagram you added. Here, let's take the dotted arrow you drew, and extend it into the full circle traced out by the center of the leading edge as it swings around the pivot:

Wheeling diagram

You can see the line from our destination, through the center of our formation after it's completed the wheeling maneuver, is one of two such lines that exactly kiss this circle.

Put another way, our pivot, our destination point, and the center of our leading edge at the end of the wheel form a right-angled triangle. So now we can find all the angles we need with a little trigonometry.

Easiest is the angle \$\alpha\$, which is the bearing to the destination:

Vector2 pivotToDestination = destination - pivot;
float bearingRadians = Mathf.Atan2(pivotToDestination.y, pivotToDestination.x);

Next is the angle \$\beta\$, the difference in angle between this bearing, and the rotation we want to perform:

float halfWidth = formation.width / 2f;
float hypotenuse = Vector2.Length(pivotToDestination);
float differenceRadians = Mathf.Acos(halfWidth / hypotenuse);

Leaving us with the angle we need to wheel, \$\theta\$:

float wheelingRadians = bearingRadians - differenceRadians;

There are likely some additional sign considerations to attend to if we're rotating clockwise instead of counter-clockwise, or wheeling more than 180 degrees, but since I'm at the end of my lunch hour, those will be left as an exercise for the reader for the time being. ;)

Hopefully that gives you the inroad you need to tackle this problem.

\$\endgroup\$
1
  • \$\begingroup\$ Aha! That looks really useful. I'll take a look at writing some code tomorrow. My trig-fu is weak. \$\endgroup\$ – Andy Apr 15 '20 at 17:20

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.