Moving child objects as parent object rotates
I have a problem with attempting to move simple models in tandem. I have
20 smaller models attached to a larger model. It's essentially a flying
saucer with multiple external cannons. I've seen other questions, like
this one, that look almost like what I want. However, that just creates
the draw translation. I actually need to move the child models in 3d
space, because they can be independently destroyed and thus require
collision detection. Here's how I'm rotating the parent (in the Update()
function):
angle += 0.15f;
RotationMatrix = Matrix.CreateRotationX(MathHelper.PiOver2) *
Matrix.CreateRotationZ(MathHelper.ToRadians(angle) * MathHelper.PiOver2);
I've tried a lot of solutions, but the positioning is always off, so they
don't really look attached. This is the closest I've gotten:
cannons[x].RotationMatrix = Matrix.CreateRotationX(MathHelper.PiOver2) *
Matrix.CreateRotationZ(MathHelper.ToRadians(angle + cannons[x].angle) *
MathHelper.PiOver2);
cannons[x].position.X = (float)(Math.Cos(MathHelper.ToRadians(angle +
cannons[x].originAngle) *
MathHelper.PiOver2) * 475) + position.X;
cannons[x].position.Y = (float)(Math.Sin(MathHelper.ToRadians(angle +
cannons[x].originAngle) *
MathHelper.PiOver2) * 475) + position.Y;
What did I do wrong in that code? Alternatively, if my approach is
completely off, what is the proper way of doing this?
No comments:
Post a Comment