How to rotate a group of objects around a common center?
Posted
by
user1662292
on Game Development
See other posts from Game Development
or by user1662292
Published on 2013-11-03T15:33:15Z
Indexed on
2013/11/03
16:12 UTC
Read the original article
Hit count: 222
I've made a model in 3D Studio Max 9. It consists of a variety of cubes, clyinders etc.
In XNA I've imported the model okay and it shows correctly. However, when I apply rotation, each component in the model rotates around it's own centre. I want the model to rotate as a single unit.
I've linked the components in 3D Max and they rotate as I want in Max.
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
model = Content.Load<Model>("Models/Alien1");
}
protected override void Update(GameTime gameTime)
{
camera.Update(1f, new Vector3(), graphics.GraphicsDevice.Viewport.AspectRatio);
rotation += 0.1f;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);
Matrix worldMatrix = Matrix.Identity;
Matrix rotationYMatrix = Matrix.CreateRotationY(rotation);
Matrix translateMatrix = Matrix.CreateTranslation(location);
worldMatrix = rotationYMatrix * translateMatrix;
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = worldMatrix * transforms[mesh.ParentBone.Index];
effect.View = camera.viewMatrix;
effect.Projection = camera.projectionMatrix;
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;
}
mesh.Draw();
}
base.Draw(gameTime);
}
More Info: Rotating the object via it's properties works fine so I'm guessing there's something up with the code rather than with the object itself.
Translating the object also causes the objects to get moved independently of each other rather than as a single model and each piece becomes spread around the scene.
The model is in .X format.
© Game Development or respective owner