Code archives/3D Graphics - Misc/Correct Offset

This code has been declared by its author to be Public Domain code.

Download source code

Correct Offset by starfox2002
;This little function corrects a mesh offset from its centre and positions the
;mesh's pivot into the center of the physical mesh. It does this by calculating the
;offset and positioning it. Created by David Dawkins, thanks also goes to BlitzSupport
;for the centermesh command. Command is free to use but with give credit towards me.
Function CorrectOffset(mesh)
;This little function corrects a mesh offset from its centre and positions the
;mesh's pivot into the center of the physical mesh. It does this by calculating the
;offset and positioning it. Created by David Dawkins, thanks also goes to BlitzSupport
;for the centermesh command. Command is free to use but with give credit towards me.
surf =GetSurface(mesh,1)
oldx# = VertexX(surf,1) : oldy# = VertexY(surf,1) : oldz# = VertexZ(surf,1)
TFormPoint(oldx,oldy,oldz,mesh,0) : oldx = TFormedX() : oldy = TFormedY() : oldz = TFormedZ()
mw = MeshWidth(mesh) : mh = MeshHeight(mesh) : md = MeshDepth(mesh)
FitMesh mesh,-mw/2,-mh/2,-md/2,mw,mh,md,1
newx# = VertexX(surf,1) : newy# = VertexY(surf,1) : newz# = VertexZ(surf,1)
TFormPoint(newx,newy,newz,mesh,0) : newx = TFormedX() : newy = TFormedY() : newz = TFormedZ()
offx# = oldx - newx : offy# = oldy - newy : offz# = oldz - newz
TranslateEntity mesh,offx,offy,offz
End Function

Comments

DJWoodgate2004
I was having problems with this so here is a variant that also allows you to specify a different offset after centering.

Function CorrectOffset(mesh,offx#=0,offy#=0,offz#=0)
Local x#,y#,z#, surf,s,v, minx#=100000,miny#=100000,minz#=100000
Local mw#=MeshWidth(mesh), mh#=MeshHeight(mesh), md#=MeshDepth(mesh)
For s=1 To CountSurfaces(mesh)
	surf=GetSurface(mesh,s)
	For v=0 To CountVertices(surf)-1
		x=VertexX(surf,v)
		y=VertexY(surf,v)
		z=VertexZ(surf,v)
		If x<minx Then minx=x
		If y<miny Then miny=y
		If z<minz Then minz=z
	Next
Next
TFormPoint minx+mw*0.5-offx,miny+mh*0.5-offy,minz+md*0.5-offz,mesh,GetParent(mesh)
TranslateEntity mesh,TFormedX(),TFormedY(),TFormedZ()
FitMesh mesh,-mw*0.5+offx,-mh*0.5+offy,-md*0.5+offz,mw,mh,md
End Function


BTW. It would be handy if blitz exposed the mesh bounds it calculates.


Code Archives Forum