Pivots/re-centering mesh

Blitz3D Forums/Blitz3D Programming/Pivots/re-centering mesh

Bnesiba(Posted 2006) [#1]
is there a way to use pivots or something similar to re-center a mesh if it's center is off?


DH(Posted 2006) [#2]
Well, the question is what sort of center are you looking for?

There is the center of the mesh as designated by the modeller (whoever modelled the mesh),

And there's the center of the mesh where you take its min and max axis distances and recenter it based on that.

You can do the latter in Blitz3d relativly easily.


Bnesiba(Posted 2006) [#3]
i really need to be able to add a center of rotation to the mesh.

normally when you use rotateentity() on a mesh which has an offcenter center(the modelling one) it rotates around that, whereever it might be.

i'm trying to make a point that will rotate the meshes so that they rotate on a point instead of around it regardless of the center positioned by the modeller.


(tu) sinu(Posted 2006) [#4]
position a pivot on the mesh where you want it's centre to be, then parent the mesh to the pivot and then apply rotations/movements to the pivot


John Blackledge(Posted 2006) [#5]
Perhaps this will do the trick:

;--------------------
Function CentreAnimMesh(mesh,mode=0)
;--------------------
; mode=0 : centre on 3 axes
; mode=1 : centre on x,z; align to y bottom
Local origx#,origy#,origz#
Local allx#,ally#,allz#
Local newx#,newy#,newz#
Local cc,c,childent,mult

	; get orig position
	origx# = EntityX#(mesh)
	origy# = EntityY#(mesh)
	origz# = EntityZ#(mesh)
	
	; get 'all' vertices positions
	allx# = MeshAllX#(mesh)
	If mode=0 Then ally# = MeshAllY#(mesh)
	If mode=1 Then ally# = MeshAllYlowest#(mesh)
	allz# = MeshAllZ#(mesh)

	; find difference
	newx# = (origx# - allx#)
	newy# = (origy# - ally#)
	newz# = (origz# - allz#)
	
	; move one or all meshes
	cc = CountChildren(mesh)
	If cc = 0
		PositionMesh mesh, newx#,newy#,newz#
	Else
		For c = 1 To cc
			childent = GetChild(mesh,c)
			mult = 2
			PositionMesh childent, mult * newx#,mult * newy#,mult * newz#
		Next
	EndIf

End Function

;--------------------
Function MeshAllX#(parent)
;--------------------
Local LeftX#,RightX#
Local cc,c,childent,vx#,x#

	cc = CountChildren(parent)
	If cc = 0
		x# = MeshX#(parent)
	Else
		For c = 1 To cc
			childent = GetChild(parent,c)
			vx# = MeshX#(childent)
			If vx#<LeftX# Then LeftX#=vx#
			If vx#>RightX# Then RightX#=vx#
		Next
		x# = LeftX# + ( (RightX# - LeftX#) / Float(2) )
	EndIf

Return x#
End Function

;--------------------
Function MeshAllY#(parent)
;--------------------
Local BottomY#,TopY#
Local cc,c,childent,vy#,y#

	cc = CountChildren(parent)
	If cc = 0
		y# = MeshY#(parent)
	Else
		For c = 1 To cc
			childent = GetChild(parent,c)
			vy# = MeshY#(childent)
			If vy#<BottomY# Then BottomY#=vy#
	    If vy#>TopY# Then TopY#=vy#
		Next
		y# = BottomY# + ( (TopY# - BottomY#) / Float(2) )
	EndIf

Return y#
End Function

;--------------------
Function MeshAllYlowest#(parent)
;--------------------
Local BottomY#,TopY#
Local cc,c,childent,vy#,y#

	cc = CountChildren(parent)
	If cc = 0
		y# = BottomMost#(parent)
	Else
		For c = 1 To cc
			childent = GetChild(parent,c)
			vy# = BottomMost#(childent)
			If vy#<BottomY# Then BottomY#=vy#
	    If vy#>TopY# Then TopY#=vy#
		Next
		y# = BottomY# + ( (TopY# - BottomY#) / Float(2) )
	EndIf

Return y#
End Function

;--------------------
Function MeshAllZ#(parent)
;--------------------
Local BackZ#,FrontZ#
Local cc,c,childent,vz#,z#

	cc = CountChildren(parent)
	If cc = 0
		z# = MeshZ#(parent)
	Else
		For c = 1 To cc
			childent = GetChild(parent,c)
			vz# = MeshZ#(childent)
			If vz#<BackZ# Then BackZ#=vz#
			If vz#>FrontZ# Then FrontZ#=vz#
		Next
		z# = BackZ# + (FrontZ# - BackZ#)/ 2
	EndIf

Return z#
End Function

;------------------------------------------------------------------------------------
;--------------------
Function MeshX#(mesh)
;--------------------
Local LeftX#,RightX#
Local s,su,v,vx#,x#

	For s = 1 To CountSurfaces(mesh)
		su = GetSurface(mesh,s)
		For v = 0  To CountVertices(su)-1
			vx# = VertexX#(su,v)
			If vx#<LeftX# Then LeftX#=vx#
			If vx#>RightX# Then RightX#=vx#
		Next
	Next
  x# = LeftX# + ( (RightX# - LeftX#) / Float(2) )

Return x#
End Function

;--------------------
Function MeshY#(mesh)
;--------------------
Local BottomY#,TopY#
Local s,su,v,vy#,y#

	For s = 1 To CountSurfaces(mesh)
		su = GetSurface(mesh,s)
		For v = 0  To CountVertices(su)-1
			vy# = VertexY#(su,v)
			If vy#<BottomY# Then BottomY#=vy#
	    If vy#>TopY# Then TopY#=vy#
		Next
	Next
	y# = BottomY# + ( (TopY# - BottomY#) / Float(2) )

Return y#
End Function

;--------------------
Function MeshZ#(mesh)
;--------------------
Local BackZ#,FrontZ#
Local s,su,v,vz#,z#

	For s = 1 To CountSurfaces(mesh)
		su = GetSurface(mesh,s)
		For v = 0  To CountVertices(su)-1
			vz# = VertexZ#(su,v)
			If vz#<BackZ# Then BackZ#=vz#
			If vz#>FrontZ# Then FrontZ#=vz#
		Next
	Next
	z# = BackZ# + (FrontZ# - BackZ#) / 2

Return z#
End Function

;--------------------
Function BottomMost#(mesh)
;--------------------
Local BottomY#,TopY#
Local s,su,v,vy#

	For s = 1 To CountSurfaces(mesh)
		su = GetSurface(mesh,s)
		For v = 0  To CountVertices(su)-1
			vy# = VertexY#(su,v)
			If vy#<BottomY# Then BottomY#=vy#
			If vy#>TopY# Then TopY#=vy#
		Next
	Next

Return BottomY#
End Function



Stevie G(Posted 2006) [#6]
Or you could use this to center the mesh ... would need to be exapnded upon for children.

Fitmesh Mesh, -Meshwidth(Mesh)*.5, -MeshHeight(Mesh)*.5, -MeshDepth(Mesh)*.5, Meshwidth( Mesh ), meshheight( Mesh ) , Meshdepth( Mesh )


Stevie


Bnesiba(Posted 2006) [#7]
thx john, that worked


Filax(Posted 2007) [#8]
Fitmesh seem not work with loadanimmesh ? any idea ?


jfk EO-11110(Posted 2007) [#9]
use it recursively. There may be some diffrences between B3D and 3ds/x animeshes. Probably bones should not be scaled, or only the top parent, not sure of that. ENtityClass$ may be useful here.