• Create Account

## How do I rotate a group of child objects in relation to a parent object?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1RoyP  Members

202
Like
0Likes
Like

Posted 16 December 2012 - 04:49 PM

I'm working on a space shooter with a local team. We're building the game with C# and XNA 4.0. I would rate myself as an advanced beginning programmer.

I'm trying to rotate a fleet of smaller fighter ships in relation to a larger cruiser ship, like this:

I'm setting the position of the parent ship as an offset to the origin point of the map object. And then I'm setting the position of each child ship as an offset from the origin point of the parent ship.

How do I rotate the positions of each child ship in relation to the parent ship's rotation? I want the child ships to maintain their formation with the parent ship no matter what angle I rotate it to.

I'm not worried about smooth rotation or anything right now. Just trying to figure out how to transform the x and y positions to the new rotated x and y positions. I know how to set the rotation of each object.

Northwest Arkansas Game Developer Group
Live in Fayetteville, Springdale, Bentonville, or Rogers?

Interested in making video games and having fun?
Check us out on Meetup.

### #2Álvaro  Members

20254
Like
0Likes
Like

Posted 16 December 2012 - 05:10 PM

So you have an offset from the "mother ship", the position of the mother ship and its rotation? Apply the rotation to the offset, add that to the position of the mother ship and you'll get where the "satellite ship" is. Is there a part of that you don't know how to do?

### #3RoyP  Members

202
Like
0Likes
Like

Posted 16 December 2012 - 09:44 PM

Alvaro,

I know to rotate the sprite, but I'm not sure how to apply the rotation to the offset locations of each satellite ship using math.

In XNA, my location for the mother ship isn't changing, just the rotation value. However, the location for the satellite ships have to change to keep the same fleet orientation.

How do I find that new x,y location for each satellite ship? That's what I'm trying to grasp.

Roy

Edited by RoyP, 16 December 2012 - 09:46 PM.

Northwest Arkansas Game Developer Group
Live in Fayetteville, Springdale, Bentonville, or Rogers?

Interested in making video games and having fun?
Check us out on Meetup.

### #4Álvaro  Members

20254
Like
1Likes
Like

Posted 16 December 2012 - 10:25 PM

I was talking about the offset locations, not how to rotate the sprite.

The formula for rotating in 2D is:
rotated_x = x * cos(alpha) - y * sin(alpha);
rotated_y = x * sin(alpha) + y * cos(alpha);

You can either represent the rotation by alpha (which is probably what you are doing), or you can represent it by the unit-length vector (cos(alpha), sin(alpha)) (which is what I would recommend).

Applying that formula to your offsets should work.

1065
Like
0Likes
Like

Posted 16 December 2012 - 10:30 PM

If you think about it, your sprites have a point in each corner, which you are rotating around another point. The math to get those corner points to rotate around the center-point (or the rotation point) is the same that you can use to rotate any number of arbitrary points about an axis. I've never used XNA, but if you can view the code for rotating the sprite that should be a pretty good clue of what to do.

Inspiration from my tea:

"Never wish life were easier. Wish that you were better" -Jim Rohn

soundcloud.com/herwrathmustbedragons

### #6RoyP  Members

202
Like
0Likes
Like

Posted 17 December 2012 - 09:07 AM

I put the formulas that Alvaro gave me into a static method. Here's the method:

[source lang="csharp"] private static Vector2 RotateVector2ByAngle(Vector2 originalVector, float rotationAngleInRadians){ float rotatedX = (originalVector.X * (float)Math.Cos(rotationAngleInRadians)) - (originalVector.Y * (float)Math.Sin(rotationAngleInRadians)); float rotatedY = (originalVector.X * (float)Math.Sin(rotationAngleInRadians)) + (originalVector.Y * (float)Math.Cos(rotationAngleInRadians)); return new Vector2(rotatedX, rotatedY);}[/source]

Position of Sprites When Not Rotated
I set the top left corner of the mother ship at (640, 360) with a rotation of 0.
The top left corner of satellite ship 1 is offset from the mother ship by (-32, -40) with a rotation of 0.
The top left corner of satellite ship 2 is offset from the mother ship by (64, -40) with a rotation of 0.
I am rotating the each ship around it's center point.
The mother ship sprite is 64x64 pixels.
The satellite ships are 32x32 pixels.

Here's what happens when I pass the offsets into the static method and set the sprites' draw positions to the new Vector2:

It looks to me like the ships slide to the right and up a bit more than they should. The only thing I can think that might be causing this is drawing the ship based on it's top left corner but rotating it around it's center point. Any ideas on what I've done wrong?

If you think about it, your sprites have a point in each corner, which you are rotating around another point. The math to get those corner points to rotate around the center-point (or the rotation point) is the same that you can use to rotate any number of arbitrary points about an axis. I've never used XNA, but if you can view the code for rotating the sprite that should be a pretty good clue of what to do.

That's the problem. I don't know the math. I'm sure I can learn it, but it's probably been 15 years since I did that level of math and I just can't remember how to do it.
Northwest Arkansas Game Developer Group
Live in Fayetteville, Springdale, Bentonville, or Rogers?

Interested in making video games and having fun?
Check us out on Meetup.

### #7Álvaro  Members

20254
Like
1Likes
Like

Posted 17 December 2012 - 09:20 AM

I would give one of the red triangles an offset of (0,0) and see where it ends up. You want that to be the center of the blue triangle.

### #8Morphex  Members

298
Like
0Likes
Like

Posted 17 December 2012 - 09:25 AM

Using XNA, I have done somethinig similar, I used a simple scene graph for that though, I think I have it lurking around if you want it, its a generic scene graph with just a parent object, childobjects, Transform Matrix, Position, and Rotation.

Its quite easy if you use matrices, just multiply the parent matrix with the child matrix and you are done. They will both have their own Coordinate system so you would need to convert them to screen space if you want to get the real coordinates.

Check out my new blog: Morphexe

### #9RoyP  Members

202
Like
0Likes
Like

Posted 17 December 2012 - 10:41 AM

@Alvaro:
Thanks for the suggestion, Alvaro. I was able to figure it out from your suggestion. Thanks for the help.

I was setting the top left corner of the satellite ship to the new point rather than setting it's center to the new point.

@Morphex:
I appreciate the offer, but I'm not at that level yet. I don't know how to transform matrices ... but it's on my list to learn. :-)

Thanks for the help, everyone. You guys rock!

Roy

Edited by RoyP, 17 December 2012 - 10:42 AM.

Northwest Arkansas Game Developer Group
Live in Fayetteville, Springdale, Bentonville, or Rogers?

Interested in making video games and having fun?
Check us out on Meetup.

### #100r0d  Members

1573
Like
1Likes
Like

Posted 19 December 2012 - 06:43 PM

I'm working on a space shooter with a local team. We're building the game with C# and XNA 4.0. I would rate myself as an advanced beginning programmer.

I'm trying to rotate a fleet of smaller fighter ships in relation to a larger cruiser ship, like this:

I'm setting the position of the parent ship as an offset to the origin point of the map object. And then I'm setting the position of each child ship as an offset from the origin point of the parent ship.

How do I rotate the positions of each child ship in relation to the parent ship's rotation? I want the child ships to maintain their formation with the parent ship no matter what angle I rotate it to.

I'm not worried about smooth rotation or anything right now. Just trying to figure out how to transform the x and y positions to the new rotated x and y positions. I know how to set the rotation of each object.

Every object should have a position/rotation matrix. This is the local-to-parent matrix, often just called the "world" matrix because objects tend to exist in world space (hence local-to-parent = local-to-world), but it's just a matrix to convert from local coordinates to those of its parent object. Clear so far?

So when you go to render an object you take its world matrix and use that. But, if an object has a parent, then its world matrix is the concatenation of its world matrix and that of it's parent. That will give you what you want, since when the parent moves or rotates, the children will move and rotate along with it.

Your object class should have a method that returns this matrix, something like:

[source lang="cpp"]// Return local-to-world matrixMatrix Object::GetLocalToWorldMatrix(void){ // mLocalToParentMatrix is an object of type "Matrix" if(GetParent()) { return matrixMultiply(GetParent()->GetLocalToWorldMatrix(), mLocalToParentMatrix); } return mLocalToParentMatrix;}[/source]

### #11RoyP  Members

202
Like
0Likes
Like

Posted 21 December 2012 - 03:58 AM

Thanks, mate.  I don't know how to work with matrices yet, but this experience has taught me I need to learn linear algebra.  So that's what I'm focusing on right now.  I appreciate the help.

Roy

Northwest Arkansas Game Developer Group
Live in Fayetteville, Springdale, Bentonville, or Rogers?

Interested in making video games and having fun?
Check us out on Meetup.

### #120r0d  Members

1573
Like
0Likes
Like

Posted 21 December 2012 - 01:41 PM

Thanks, mate.  I don't know how to work with matrices yet, but this experience has taught me I need to learn linear algebra.  So that's what I'm focusing on right now.  I appreciate the help.

Roy

Basic matrix math is not so bad once you jump into it.  There's some good websites that will teach you the basics of setting up a rotation matrix, transforming positions and direction vectors, and stuff like that.  Any time you start moving objects around the world, having parent-child relationships, etc, you're into the realm of matrices... so that's the point where you should put everything else down, move matrices up to the top of your todo list, and learn them.

Once you have a basic matrix class and change your code to use them (whether you're working in 2D or 3D), it'll make your life much easier.  It's like trying to dig a hole with your hands instead of using a shovel.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.