Hello Guest

Author Topic: Center of group meshes.  (Read 5247 times)

RhapsodyGames

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 40
    • View Profile
Center of group meshes.
« on: November 17, 2013, 03:10:21 am »
Hello!
Here's the deal: I want to find center of group of some meshes (for example: tk2dSprite + tkd2TextMesh), what i do:

MeshRenderer[] mr = objWithMeshes.GetComponentsInChildren<MeshRenderer>(true);
Bounds bound = new Bounds(Vector3.zero, Vector3.zero);

foreach(MeshRenderer m in mr)
{
   bound.Encapsulate(m.bounds);
}

return bound.center;

On the exit I have totally wrong coordinates (maybe it only works for usual 3d models or some, don't know). I've try to put this coord to local and world positions, but it's still totally wrong. And coords not changes if I add some symbols to textMesh. What I did wrong? How can I find center of group tk2dsprites/textMeshes?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Center of group meshes.
« Reply #1 on: November 17, 2013, 06:07:39 pm »
Ah but this won't get you the centre. You're starting off with a bounding box covering zero with a dimension of zero. Any encapsulated bounds will include this point too... couldn't you start off with the first mr's bounds?

RhapsodyGames

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Center of group meshes.
« Reply #2 on: November 18, 2013, 04:59:44 am »
Now I've tried this, is it closer to right?  :'(

MeshRenderer[] mr = objWithMeshes.GetComponentsInChildren<MeshRenderer>(true);
Bounds bound = mr[0].bounds;
foreach(MeshRenderer m in mr)
{
   if(m != mr[0])
      bound.Encapsulate(m.bounds);
}

It makes ok position at first, but if I try to make sprite bigger it calculates wrong position. And if I put this code to Update it moves my scprites to the infinity or some.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Center of group meshes.
« Reply #3 on: November 18, 2013, 10:56:29 am »
I'm not sure what you're doing with the bounds, but if you do this, and keep your sprite in the list of bounds you're checking for, every time you move it it will change the bounds, and this will keep feeding back and it will go off into infinity...

RhapsodyGames

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Center of group meshes.
« Reply #4 on: November 18, 2013, 01:00:54 pm »
Yes, I got it :) But can You tell me how to center them right? Is it possible, right?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Center of group meshes.
« Reply #5 on: November 18, 2013, 09:28:18 pm »
Surely you just don't consider the sprite you're moving when you're centring a group of sprites? I don't think I know what you're trying to do here.

RhapsodyGames

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Center of group meshes.
« Reply #6 on: November 19, 2013, 08:16:43 am »
Sorry, maybe I explained wrong. I have some target point and group of meshes, I need to calculate overall center of thise meshes and put this center to "some target point". Like "Anchor" option on TextMesh, but for group of different meshes.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Center of group meshes.
« Reply #7 on: November 19, 2013, 12:19:13 pm »
So you're trying to centre the meshes on this game object? If so, the way to do this is pretty straightforward.
Like so:

Anchor
   TheGameObjects


1. Find the center of the group of objects you're trying to centre.
2. Unparent TheGameObjects from Anchor. Make sure they aren't linked to anchor.
3. Set the anchor .position to the centre position you calculated in 1.
4. Attach TheGameObjects to Anchor.

You should do this every time you need to update this, so it doesn't feed back.
You can also calculate this using the matrices.

RhapsodyGames

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Center of group meshes.
« Reply #8 on: November 20, 2013, 06:24:45 am »
yipikaye! :)
It works! Thank a lot! I'm so happy :)