..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??
Pivot Point centering
Blitz3D Forums/Blitz3D Programming/Pivot Point centering There are a couple of functions in the Code Archive that do exactly this :)
..huh..I must be blind then..can you post some linky, please..
..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..
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..
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.
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.
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?
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?
..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
http://www.filefactory.com/file/b8a7f5/n/Test_b3d
name of the sphere in hierarchy is Sphere01
ask your artist to do this perhaps? ;)
..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..
I have this problem recently. I load the objects in 3DMAx adjust the ppivit and save. That was the cure for my case
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... :)
- 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... :)
Ultimate Unwrap 3D is perfect for that. It will take you less than 15 seconds to load the model, select all, center and save.
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.
Look at this chunk of code:
Graphics3D 640,380,0,2 SetBuffer BackBuffer() cube = CreateCube() PositionMesh cube,5,5,5 cam = CreateCamera() PositionEntity cam,0,0,-20 While Not KeyHit(1) TurnEntity cube,1,1,1 If KeyHit (57) Then FitMesh cube,-1,-1,-1,2,2,2 UpdateWorld() RenderWorld() Text 10,10,"press space to center cube pivot" Flip Wend End
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.
..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..
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()
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!
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!
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)?
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)?
In blitz, finding and moving the centre can be done in real time. It's practically instant.
@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..
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..
Have you tried my newest solution naughty?
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..
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.
EDIT: someone already posted this...nvm.
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.
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.
Function centre_mesh(mesh) tx# = find_centre_x(mesh) ty# = find_centre_y(mesh) tz# = find_centre_z(mesh) FitMesh mesh,MeshWidth#(mesh)*-0.5,MeshHeight(mesh)*-0.5,MeshDepth(mesh)*-0.5,MeshWidth(mesh),MeshHeight(mesh),MeshDepth(mesh) PositionEntity mesh,tx,ty,tz,True End Function Function find_centre_y#(entity) hv# = find_highest_vertex#(entity) lv# = find_lowest_vertex#(entity) centre_y# = lv + ((hv - lv)/2) Return centre_y End Function Function find_centre_x#(entity) rm# = find_rightmost_vertex#(entity) lm# = find_leftmost_vertex#(entity) centre_x# = lm + ((rm-lm)/2) DebugLog(" mesh width = "+ (rm-lm)) Return centre_x End Function Function find_centre_z#(entity) zm# = find_zmost_vertex#(entity) zl# = find_zleast_vertex#(entity) centre_z# = zl + ((zm - zl)/2) Return centre_z End Function Function find_lowest_vertex#(mesh) lowest# = 999999.0 no_surfaces = CountSurfaces(mesh) For sloop = 1 To no_surfaces Surface_Handle = GetSurface(mesh, sloop) For loop = 0 To CountVertices(surface_handle)-1 TFormPoint VertexX(surface_handle,loop),VertexY(surface_handle,loop),VertexZ(surface_handle,loop),mesh,0 If TFormedY#() < lowest Then lowest = TFormedY#() End If Next Next Return lowest End Function Function find_highest_vertex#(mesh) highest# = -999999.0 no_surfaces = CountSurfaces(mesh) For sloop = 1 To no_surfaces Surface_Handle = GetSurface(mesh, sloop) For loop = 0 To CountVertices(surface_handle)-1 TFormPoint VertexX(surface_handle,loop),VertexY(surface_handle,loop),VertexZ(surface_handle,loop),mesh,0 If TFormedY#() > highest Then highest = TFormedY#() End If Next Next Return highest End Function Function find_leftmost_vertex#(mesh) leftmost# = 999999.0 no_surfaces = CountSurfaces(mesh) For sloop = 1 To no_surfaces Surface_Handle = GetSurface(mesh, sloop) For loop = 0 To CountVertices(surface_handle)-1 TFormPoint VertexX(surface_handle,loop),VertexY(surface_handle,loop),VertexZ(surface_handle,loop),mesh,0 If TFormedX#() < leftmost Then leftmost = TFormedX#() End If Next Next Return leftmost End Function Function find_rightmost_vertex#(mesh) rightmost# = -999999.0 no_surfaces = CountSurfaces(mesh) For sloop = 1 To no_surfaces Surface_Handle = GetSurface(mesh, sloop) For loop = 0 To CountVertices(surface_handle)-1 TFormPoint VertexX(surface_handle,loop),VertexY(surface_handle,loop),VertexZ(surface_handle,loop),mesh,0 If TFormedX#() > rightmost Then rightmost = TFormedX#() End If Next Next Return rightmost End Function Function find_zmost_vertex#(mesh) ; the furthest away on the Z axis zmost# = -999999.0 no_surfaces = CountSurfaces(mesh) For sloop = 1 To no_surfaces Surface_Handle = GetSurface(mesh, sloop) For loop = 0 To CountVertices(surface_handle)-1 TFormPoint VertexX(surface_handle,loop),VertexY(surface_handle,loop),VertexZ(surface_handle,loop),mesh,0 If TFormedZ#() > zmost Then zmost = TFormedZ#() End If Next Next Return zmost End Function Function find_zleast_vertex#(mesh) ; the closest on the Z axis zleast# = 999999.0 no_surfaces = CountSurfaces(mesh) For sloop = 1 To no_surfaces Surface_Handle = GetSurface(mesh, sloop) For loop = 0 To CountVertices(surface_handle)-1 TFormPoint VertexX(surface_handle,loop),VertexY(surface_handle,loop),VertexZ(surface_handle,loop),mesh,0 If TFormedZ#() < zleast Then zleast = TFormedZ#() End If Next Next Return zleast End Function
Ross-
You should put those in the code archives. I'm sure others will have use for them at some point down the road.
You should put those in the code archives. I'm sure others will have use for them at some point down the road.
Will do