• Advertisement
Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

[img]http://i46.tinypic.com/334punn.png[/img][img]http://i48.tinypic.com/6y1r3p.png[/img]

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.

Please help me understand the process and the math.

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:
[img]http://i48.tinypic.com/fkxz48.png[/img][img]http://i45.tinypic.com/fnxsar.png[/img]

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?



[quote][b]NoAdmiral:[/b]
If you think about it, your sprites have a point in each corner, which you are rotating around another point.[i] [b]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.[/b][/i] 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.[/quote]
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
@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

Share this post


Link to post
Share on other sites
[quote name='RoyP' timestamp='1355698193' post='5011388']
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:

[img]http://i46.tinypic.com/334punn.png[/img][img]http://i48.tinypic.com/6y1r3p.png[/img]

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.

Please help me understand the process and the math.
[/quote]

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 matrix
Matrix Object::GetLocalToWorldMatrix(void)
{
// mLocalToParentMatrix is an object of type "Matrix"
if(GetParent())
{
return matrixMultiply(GetParent()->GetLocalToWorldMatrix(), mLocalToParentMatrix);
}
return mLocalToParentMatrix;
}[/source]

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement