Swapping positions of three objects while moving

Started by
1 comment, last by dzeligman 15 years, 9 months ago
I have three different objects with set positions. They move together as one unit in a column : A B C Currently when I press R they "swap" as follows: B moves vertically to the old position of A, C moves vertically to the old position of B and A moves around a Curve to the old position of C. (This is in 2d, with vectors I should add in) Currently to perform this I have to keep track of which of the three objects is in the A position as well as having a separate Vector2 to track their oldPosition. temp0,temp1,temp2 are the old position Vectors. I store the three objects in a list. The movement vertically is done along a linear line, the curve is quadratic. I have to pass start Position, End position and a speed variable to the Quad and Linear methods. The second position in the Quad method is basically its midpoint along the curve. t1 and t2 are speeds that are updated and increased during my Update call. This method is called within my update loop. Some sample Code:
 if (componentShips[0].isFirst)
            {
                componentShips[0].worldPosition = CalcHelper.Quad(componentShips[0].worldPosition, 
(new Vector2(componentShips[0].worldPosition.X + componentShips[0].Bounds().Width / 3, componentShips[0].worldPosition.Y)), temp2, t);
                componentShips[1].worldPosition = CalcHelper.Linear(componentShips[1].worldPosition, temp0, t2);
                componentShips[2].worldPosition = CalcHelper.Linear(componentShips[2].worldPosition, temp1, t2);


                if (componentShips[0].worldPosition.Y - temp2.Y == 0)
                {    //the linear position X does not change, but Quad is off by hundreths, hard code change 
                    componentShips[0].worldPosition.X = componentShips[2].worldPosition.X;

                    componentShips[1].isFirst = true;
                    componentShips[0].isFirst = false;
                    componentShips[2].isFirst = false;

                    setAll = true;
                }
            }


It appears that the + operator is not rendered correctly within this post, so where you see a few spaces between things a + would be there. Basically I kind of figured out how to do this off the top of my head and was wondering if anyone could think of small or large improvements to its design. Currently keeping track of the three old positions and having different if blocks based off which ship is first is rather annoying.
Advertisement
Instead of having the objects remember which is first, have the array do it: i.e., actually re-order the objects in the array. That way, you cut out redundant data and avoid having to repeat the code for each case. (You could work around that, but then you still have overly-complicated code because of the arithmetic.)

Also, the proper place to fix the inaccuracy of the Quad calculation is within that calculation itself... although you can help out by giving it an accurate time value.

The calculation helpers should probably know more, or at least have a standard interface like: start and end positions, and a ratio of elapsed to total time. (Why are there separate 't' and 't2' values currently?)

Finally, let the objects themselves update their own positions, instead of reading those values, calculating them and putting them back.

ComponentShip::moveDuringComponentSwap(bool leader, ComponentShip swapWith, Direction distance, double progress) {  Position target = swapWith.worldPosition + distance;  Position p = positionBeforeComponentSwap();  if (leader) {    worldPosition = CalcHelper.Quad(p, target, progress);  } else {    worldPosition = CalcHelper.Linear(p, target, progress);  }}Direction distance = distanceMovedDuringComponentSwap();double progress = elapsed_time / total_time;if (progress >= .999) { progress = 1; } // make sure things line up at the endint ship_count = 3;for (int i = 0; i < ship_count; ++i) {  componentShips.moveDuringComponentSwap(i == 0, componentShips[(i + ship_count - 1) % ship_count], distance, progress);}if (progress == 1) {  ComponentShip temp = componentShips[0];  for (int i = 1i; i < ship_count; ++i) {    componentShips = componentShips<span style="font-weight:bold;">;<br>  }<br>  componentShips[ship_count - <span class="cpp-number">1</span>] = temp;<br>}<br><br></pre></div><!–ENDSCRIPT–> 
Yeah, that makes so much more sense. Thanks

t and t2 are the two speeds that they move. The move quadratically is a farther total distance, thus it takes longer and the speed has to be adjusted to match how fast the other objects moved linearly so each ship moves into the new spot at the same time.

This topic is closed to new replies.

Advertisement