Centre point of mesh

Blitz3D Forums/Blitz3D Programming/Centre point of mesh

Jeroen(Posted 2003) [#1]
Hi,

How can I set the pivot point of a mesh to exactly the center of the mesh?

Jeroen


Jeroen(Posted 2003) [#2]
PositionEntity m\block4_pivot, MeshWidth#(m\block4_entity)/2, MeshHeight#(m\block4_entity), MeshDepth#(m\block4_entity)/2


Doesn't work :(
I want to make the entity a child of the pivot.

Plz help :)


sswift(Posted 2003) [#3]
If you want to change the center of a mesh you need to first choose the point which you want to be the center relative to the current center of the mesh, and then move all the vertcies in the mesh.

this is hard to explain though.

Okay...

Inside your mesh it has it's own "space". Each vertex is at a position in mesh space. These positions are the same no matter how you orient the mesh in the world.

So, you need to define the center point in this mesh space. Once you have done that you can figure out how much to offset all the vertices.

But finding this point in mesh space can be hard for you.

Here's an easy way to do it.

Leave your mesh at 0,0,0 in the world, with no rotation when you load it.

FInd the point in world cooridnates where you want the center point to be in the mesh.

This point will be Px# Py# Pz#.

Then do this, where Mesh is the pointer to your mesh.

TFormPoint Px#, Py#, Pz#, 0, Mesh
Px# = TFormedX()
Py# = TFormedY()
Pz# = TFormedZ()

You now have transformed the point from world space to mesh space.

Now the center of your mesh is always at 0,0,0 in mesh space. So, Pxyz is going to be a point around this somewhere.

Let's pretend Px=1, and Py and Pz = 0.

That means that the new center point you want is one unit (in mesh space) to the right of the current center location.

So what you need to do is move all the vertices one unit to the LEFT to move 0,0,0 over to 1,0,0.

So all you do now is loop through every vertex in the mesh, and move it from it's current position, to it's current position MINUS Pxyz.

In other words:

Loop through each vertex
For this vertex
Set it's position to VertexX#()-Px#, VertexY#()-Py#, VertexZ#()-Pz#

And voila, you now have a new mesh center.


sswift(Posted 2003) [#4]
Oh and the center point is the average location of all vertices. So add all the Vx#'s and divide by rhe number of vertices. Same for Vy and Vz. The result is your center position in MESH space. That allows you to skip the whole global space transformation I told you about and skip right to subtracting Pxyz from the vertex positions.

And if you want to use a pivot instead you can make a pivot and then go EntityParent Entity, Pivot or maybe the other way around I forget.


Sweenie(Posted 2003) [#5]
Hmm, don't you get the center point by adding the smallest and largest value of each axis and divide by 2?
MidPointX = (SmallestX + LargestX) / 2
MidPointY = (SmallestY + LargestY) / 2
MidPointZ = (SmallestZ + LargestZ) / 2

If you sum all Vx#'s you get the mean value.

Consider a 2D triangle.

Topvertex Y = 0
BottomRightvertex Y = 100
BottomLeftvertex Y = 100

The middle should be at Y = 50 , right?

But (0 + 100 + 100) / 3 = 66.6666...
Instead use this...
(0 + 100)/2 = 50


sswift(Posted 2003) [#6]
No the middle should not be at Y = 50.

It's only an approxiomation, but the center which you almost always want in a 3D object is the center of mass, and the locations of the vertices gives you an approximation of that.

The reason you want the center of mass is because that is the point an object in the real world is most likely to rotate around.

In your triangle example, the two vertices at 100 Y means that that is where the wide end of the triangle is, and thus the most mass, so the center of mass is closer to them than the other point.


Sweenie(Posted 2003) [#7]
Well, I think we both can agree that it depends on what we are gonna use the centerpoint for.
For realistic rotations and physics you'll want the center of mass, but for bounding spheres and such, you don't want the center of mass, but the center of the mesh.


sswift(Posted 2003) [#8]
Yes, for bounding regions you want the center of the bounding box which would be calculated with the method you described.