Pivot Point centering

Blitz3D Forums/Blitz3D Programming/Pivot Point centering

Naughty Alien(Posted 2008) [#1]
..im wondering how to achieve such thing?? Lets assume that you have received geometry(separated models/entities) from your artists, but pivot points are not in center..how to do centering within B3D trough code without affecting position of object??


boomboom(Posted 2008) [#2]
There are a couple of functions in the Code Archive that do exactly this :)


Naughty Alien(Posted 2008) [#3]
..huh..I must be blind then..can you post some linky, please..


Naughty Alien(Posted 2008) [#4]
..I have found few functions and none of them doing what I was asking...all of them centering mesh around 0,0,0 or around entity pivot point coordinates..for example..take a look at this pics..

Let say I got this from artist..pivot is outside of sphere..


so what i want is, after applying centering function, my entity should be like this


so, pivot is in center of entity, while entity geometry position are not affected at all..


Ross C(Posted 2008) [#5]
You take the width,height and depth of the entity. You centre the entity around the 0,0,0 axis. You do this by using the fitmesh command:

FitMesh mesh,-(0.5*meshwidth(mesh)),-(0.5*meshheight(mesh)),-(0.5*meshdepth(mesh)),meshwidth(mesh),meshheight(mesh),meshdepth(mesh)

Your mesh should now be centred around the 0,0,0 axis. Now, simple Positionentity, or if you want to move the vertices instead, moveentity to your pivot point.


Naughty Alien(Posted 2008) [#6]
I am not sure that I understand properly...what you saying is to center mesh around 0,0,0, what is fine..then move sphere or vertices to a proper position of my pivot..but far as i can see from your explanation, my pivot was not moved at all..i want to move pivot shown on pic, on to proper position within sphere(or any other custom object), what is in setup you propose, unknown location..so how to move my vertices/entity there if I dont know location at all? Pivot on pics i show is not center of scene..its sphere pivot purposely moved out..how to push it back?? Can you be more specific Ross..here is what I dont understand..
1) use fitmesh so my sphere is centered around 0,0,0......fine
2) Positionentity, or move the vertices , moveentity to your pivot point

point (2) is something I dont understand..how you know what are X,Y,Z of that pivot of mine, if it was never existed on place i wanted it to be, in order to make a moveentity?


boomboom(Posted 2008) [#7]
http://www.blitzbasic.com/codearcs/codearcs.php?code=732


Naughty Alien(Posted 2008) [#8]
..I have tried that boomboom...its just centering mesh at 0,0,0..here is B3D file im playing with so I may be wrong..give it a try guys..
http://www.filefactory.com/file/b8a7f5/n/Test_b3d

name of the sphere in hierarchy is Sphere01


gameproducer(Posted 2008) [#9]
ask your artist to do this perhaps? ;)


Naughty Alien(Posted 2008) [#10]
..well..its a bit different when you working with folks online and in Asia :) trust me..I have tried it but its simply not happening..or if does some objects are in place(pivot position wise) and some not..its hard to check always everything and i want simply to do that by default inside code..


Al Meyer(Posted 2008) [#11]
I have this problem recently. I load the objects in 3DMAx adjust the ppivit and save. That was the cure for my case


OJay(Posted 2008) [#12]
well...what about:

- loop through all of the vertices, averaging their position (so you get the center of the visible geometry)
- than call "positionmesh sphere, -averageX,-averageY,-averageZ" to center the visible geometry on 0,0,0 (but w/o repositioning the entities pivot)
- and then call "positionentity sphere, averageX,averageY,averageZ" to move your entity to the averaged position.

that should move the pivot to the center of your visible geometry... :)


stayne(Posted 2008) [#13]
Ultimate Unwrap 3D is perfect for that. It will take you less than 15 seconds to load the model, select all, center and save.


Pongo(Posted 2008) [#14]
you need to use the positionmesh command to move the mesh to it's pivot. I think that Ross C's suggestion should work.

Look at this chunk of code:



notice how the cube does not rotate around it's center. The positionMESH command moves the mesh but not the pivot. To fix a mis-aligned pivot, you should be able to:
1. position mesh at 0,0,0 (This will put the pivot at 0,0,0)
2. use fitmesh to place the mesh around the 0,0,0, coordinates
3. now use positionmesh to put the object back where it was, except now the pivot should be correct.


Naughty Alien(Posted 2008) [#15]
..what confusing me is step (3)... how should I know coordinates where mesh was, so i can move it back to origin with realigned pivot, if returned coordinates are based on pivot, while mesh structure is away from pivot itself, as I do have shown on pic for my input geometry?..getting coordinates for step (3) confusing me..


John Blackledge(Posted 2008) [#16]
http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=1785 ; CentreAnimMesh


Ross C(Posted 2008) [#17]
The only other thing i think you mean, which i'm now getting, is actually getting the centre location of the mesh in the first place, which did trip me up when doing the cannon game, because you can't use the entityX commands etc.

What i did was find the centre point of the mesh as it was, so:

I looped through each vertex in the mesh, and had six different functions, but three should do you:

find_middle_x()
find_middle_y()
find_middle_z()

function find_middle_x(mesh)

   c = countvertices(mesh,surface) ; you'll need to select the correct surface
   least_x# = 999999
   most_x# = -999999
   for loop = 1 to c
      
      if vertexX(mesh,surface) < least_x then
         least_x = vertexX(mesh,surface)
      end if
      if vertexX(mesh,surface) > most_X then
         most_x = vertexX(mesh,surface)
      end if
   next

   average = least_x + ((most_x - least_x)/2)

   return average

end function


Something like that. Note, the above might not work, as i can't test it, but it give you the basic idea. That way you have your centre point. From that, use the centring code i gave in the above posts, then positionentity your mesh at the co-ords you received from your middle functions. That should sort your out.

Sorry it took so long to catch on!


Kryzon(Posted 2008) [#18]
Open 3DS Max -> Hierarchy -> Affect Pivot Only -> Center to Object.

Export.

EDIT: It takes 3.4 seconds to do it in 3DS Max. Why do you want to code it and take much more time? are you unable to open the files (perhaps they are already in .b3d format)?


Ross C(Posted 2008) [#19]
In blitz, finding and moving the centre can be done in real time. It's practically instant.


Naughty Alien(Posted 2008) [#20]
@Kryzon
3dsmax was never issue, and if i receive MAX file i will do it with smile on da face..but artist as we all, protecting his work and sending only end user format (B3D), not source(max)...I have try B3D importer by fredborg(I think) but for large levels (250K polys) simply doesnt work (read i cant import it)...soo..real time is only way..


Ross C(Posted 2008) [#21]
Have you tried my newest solution naughty?


Naughty Alien(Posted 2008) [#22]
hey Ross buddy...theoretically, what you suggested should work, but approximation is not good due lost UV set if object is not exactly in center as originally should be..your solution working but object is a bit out of position in comparison with original...I have to check again what could possibly goes wrong..


Ross C(Posted 2008) [#23]
It should be perfectly fine. Your not touching the UV set, as it works simply by moving the mesh as a whole. I will post my code i used for the cannon game.


Kryzon(Posted 2008) [#24]
EDIT: someone already posted this...nvm.


Ross C(Posted 2008) [#25]
Naughty, here it is:

Just call the centremesh function with the mesh you wish to centre on the local 0,0,0 pivot and it will centre the mesh and keep its position.




Pongo(Posted 2008) [#26]
Ross-

You should put those in the code archives. I'm sure others will have use for them at some point down the road.


Ross C(Posted 2008) [#27]
Will do