find center point

Blitz3D Forums/Blitz3D Programming/find center point

Picklesworth(Posted 2004) [#1]
Is there a function or method in blitz 3d to refind the center point of an object? Because I just realised that I may have to redo a ton of stuff just because I foolishly loaded a wrongly rotated object and didn't remember to compensate for it properly. It currently will not work because the center point is actually on the right side of the object (and annoyingly, like all 3d modellers, wings 3d lacks an "align to center" command).


sswift(Posted 2004) [#2]
No, there is no function to do that, but you can write one easily.

Loop through all surfaces in the mesh. Loop through all vertices in each surface. Count the vertcies, and add their X Y and Z's together in three seperate variables. Then at the end divide the resulting X Y and Z values by the number of vertices. There's your center.

You can then loop through all the vertices again, get their position and then subtract the location of the center from their location. The result will be a mesh with it's center in the proper location... 0,0,0.


AntonyWells(Posted 2004) [#3]
type point
   field x#,y#,z# 
   field lx#,ly#,lz#
   field rx#,ry#,rz#
end type

function centrePoint.vector(mesh)
local lx#,ly#,lz#
local rx#,ry#,rz#
    for j=1 to countSurfaces(mesh)
       srf=getSurface(mesh,j)
       for k=1 to countVertices(srf)-1
           vX# =vertexX(srf,k)
           vY# =vertexY(srf,k)
           vZ# =vertexZ(srf,k)
           if vX<lx lx=vx 
           if vX>rx rx=vx
           if vY<ly ly=vy
           if vy>ry ry=vy
           if vz<lz lz=vz
           if vz>rz rz=vz
       next
   next
   out.point =new point
   out\x =lx+(rx-lx)/2.
   out\y =ly+(ry-ly)/2.
   out\z =lz+(rz-lz)/2.
   out\lx=lx:out\ly=ly:out\lz=lz
   out\rx=rx:out\ry=ry:out\rz=rz
end function


Should return the true centre of a mesh, in the form of a type.(Point\x,point\y,point\z)=actual 3d position of centre.
and lx,ly,lz = Left, bottom,in bounds, rx,ry,rz=right, top out bounds. (Err..a box..in other words ;) )


sswift(Posted 2004) [#4]
His name's Otacon. :-)


AntonyWells(Posted 2004) [#5]
Lol, just noticed the mistake...:) thx...


Picklesworth(Posted 2004) [#6]
all right thanks. I'll try using that to parent to a pivot and move the tokRB into the right spot tomorrow. Bye for now.


PetBom(Posted 2004) [#7]
Wouldn't It be easier to use EntityX, EntityY,EntityZ with MeshWidth,MeshHeight,MeshDepth to find out rx,ry,rz and lx,ly,lz instead of looping? I realize that this wouldn't work if you,ve used ScaleEntity on the mesh but otherwise I guess it would do the trick.

//PetBom


Picklesworth(Posted 2004) [#8]
oh, thanks petbom. I forgot about those functions. I think I'll have to rewrite my program because I really butchered it and can't quite get it right.


Picklesworth(Posted 2004) [#9]
ah, excellent, I got it working via a trial and error method for y and z axis.
[code]
MoveEntity ship01,out\lx,-3,-0.6
[\code]
I think I'll have to be more careful with future objects :D

Okay... then again, now my local x y and z coordinates are messed up. Is there a way I can rotate my ship entity that would still keep the same local x y and z coordinates?


big10p(Posted 2004) [#10]
It seems you can use fitmesh() to place the origin of a mesh to it's centre - or any other position - like so:

	Graphics3D 800,600,16

	SetBuffer BackBuffer()
	
	cam = CreateCamera()
	PositionEntity cam,0,0,-15
	
	light = CreateLight()

	cube = CreateCube()	
	w# = MeshWidth(cube)
	h# = MeshHeight(cube)
	d# = MeshDepth(cube)
	
	While Not KeyHit(1)

		If KeyHit(57) Then FitMesh cube,-w/2,-h,-d/2,w#,h#,d#

		TurnEntity cube,1,1,1

		UpdateWorld
		RenderWorld

		Text 10,10,"Press SPACE to place the cube's origin in it's base"
		
		Flip(1)
	Wend

	End



Beaker(Posted 2004) [#11]
Don't you have a /2 missing there?


big10p(Posted 2004) [#12]
No because I'm putting the origin in the base of the cube. ;)