# How to get center of multiple meshes?

## Recommended Posts

I'd like to be able to transform a bunch of meshes at the same time.  To do that I would create an empty mesh and make that their parent.  When I rotate this group I'd want it to rotate around the center so I'll need to place this parent mesh within the center of all the children.  I've looked around for a function that can calculate the center from multiple points.  I saw Vector3.Center() but that only takes 2 vectors.  Is there a way I can calculate the center of multiple meshes?  Or could I use Vector3.Center() in a loop to get the exact center?

##### Share on other sites

I would build a combined bounding box (or bouding sphere) of all the meshes and use the center from the box/sphere.

##### Share on other sites

As I was checking out bounding boxes I came across MergeMeshes.  That seems to do what I want.  I checked the bounding box of that and I'm happy with it.  My pivot point seems off though, definitely not in the center.

##### Share on other sites

Its all good.  I think I got this.  Just hoping I can un-merge those meshes when I have to

##### Share on other sites

Would not do it that way, if you plan on un-merging or want the value of meshes with different materials.  I would collect all the minimulWorld & maximumWorlds from each meshes bounding box.

Get the maximum maximum, and minimum minimum in each dimension.  Then do a center calc the same as bounding box:

``this.center = this.maximum.add(this.minimum).scale(0.5);``

##### Share on other sites

Just to make life more difficult for you; what do you mean by center? If you just want to rotate a lot of meshes at the same time then placing a parent anywhere and rotating the parent will rotate all the meshes. However if you want the rotation to be about a specific point called the center this can be defined in more than one way.

Suppose you have two unit cubes with the same y and z coordinates but x coordinates a and b then the center is certainly at ( 0.5(a + b), y, z). You could call the centre of any two meshes at these coordinates their center. However if you have a unit cube at (a, y, z) and a cube of two units at (b, y, z) the the center of mass (assuming same density) would be at ((a + 8b)/9, y z) so it all depends what you mean by center.

##### Share on other sites

Crap!  You guys are right.  MergeMeshes was a stupid idea.  Didn't realise they all share the same materials.

@JohnK I wanted to have a central point to rotate all the combined meshes around.  I just want something intuitive for the user.  If they've built a house with walls, doors, windows, etc.. and want to rotate the whole thing, I'd want it to rotate around the center of that house which is what they would expect to happen

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.