# Hack n' Slash Formation Problem

This topic is 4222 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have to implement formations into my group's project and I have run into a problem. It is a 3D hack and slash where we only change X and Z position along a terrain clamped Y-plane. The only rotation we do is along the Y axis to turn, we don't touch X or Z on rotation. The Problem I have is with any formation I use, for example the Wedge formation, I can set the soldiers behind their captain in a 1-2-3 formation like this: ---| ---C --S-S -S-S-S (The - Are there to maintain structure) Where the C is the captain, the S are soldiers, and the | is the forward vector. However, if the C turns any direction after that, I want the soldiers to maintain the formation, but I can't get the rotation of their positions right. For example: ---C ---| --S-S -S-S-S Is what currently happens, and I want: -S-S-S --S-S ---C ---| So far, I think I need to get the angle of rotation from example one to example two and rotate all the soldier's positions accordingly, but I am having trouble doing so. If this is wrong, please tell me so and give me a bit of a push in the right direction, please. Thank you.

##### Share on other sites
You need to express the position on each soldier in regard to the leader...

Example!

Lets say the position of the leader is Lx, Ly. Its facing angle on the ground plane is Ltheta.

You want one of the soldier to be 10 units behind the leader, and 5 units to its left, giving us a vector of (-10, -5) from the leader (depending on your coordinate system...). Just rotate that vector using Ltheta to get the new one. On mathworld, you can learn about Rotation matrices.

Using the 2d rotation matrix, you know that your new rotated position will be:

x' = (-10)*cos(Ltheta) + (-5)*sin(Ltheta)
y' = -(-10)*sin(Ltheta) + (-5)*cos(Ltheta)

(again change this depending on your coordinate system...)

Just add the leader position to get the position you want this unit to take at any given time:

X = (-10)*cos(Ltheta) + (-5)*sin(Ltheta) + Lx
Y = -(-10)*sin(Ltheta) + (-5)*cos(Ltheta) + Ly

Fun with maths.

##### Share on other sites
To get more realistic looking formation movement, you can look into steering behaviors. Each soldier is assigned a position relative to the leader, and each frame they calculate that position and move in a way to attempt to reach it.

##### Share on other sites
Ah, thanks Steadtler for that help. I was doing something stupid with my math and now it is fixed.

Vorpy: Yea, I did something along those lines with the help of an AI book (Programming Game AI by Example).

##### Share on other sites
Quote:
 Original post by GhostOfStarmenVorpy: Yea, I did something along those lines with the help of an AI book (Programming Game AI by Example).

Somebody cue fup!

Another thing you may want to address is that having everybody go to assigned positions like that will occasionally cause a cluster... er... confusion. In your 180-degree example, there will be a crossing of all the soldiers as they try to get to their spots. Instead, in the case of a radical change of position, you can have units go to relatively near, "open" spots. That way, in your example, the units would go straight up rather than crossing in the middle. In essence, they would take up mirrored locations from what they had prior to the switch.

What you would do is create open spots and then run an algorithm where people claim those spots (making them no longer open). You can actually get clever and run them through a prior algorithm that gives everyone a chance to "vote" on their prefered slot(s) based on distance, etc. then assign everyone a slot so that it makes the best sense. That gets tricky, though.

For example, for the least collision problems, the 2nd row of 2 dudes would actually take up spots on the BACK row of the new, flipped formation. If they had taken the 2nd row again, the back row would have had to pass them - and actually have longer distances to go. It would have taken longer to receate the formation as well. If may have taken the 2nd row 2 seconds to get to the other 2nd row... but 4 seconds for the back to get to the back. In the example I proffered earlier, it may have only taken 3 seconds.

Anyway, there's a lot of approaches you can take.

##### Share on other sites
Ah... Yea, I started to notice the clustering in much larger formations. Well, I will try to make my units smarter by having them re-assign their positions when a direction switch, such as the 180 example, happens. Once again, thanks for the help, so far I am looking fine, I will be sure to come back if I a stumped on something else.

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633695
• Total Posts
3013374
×