• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
RoyP

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

11 posts in this topic

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.
0

Share this post


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

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
0

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.
1

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.
0

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.
0

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.
1

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.
0

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
0

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]
1

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

0

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0