center pivots to objects

Blitz3D Forums/Blitz3D Programming/center pivots to objects

Osoko(Posted 2006) [#1]
Hi there, Iwant to have a plane rotating according to the camera ( Y-90°).
I place the plane in 3D apps, but when it rotates in blitz, it uses the center pivot (0,0,0).
I want to use the center pivot of the object, how to do ?


b32(Posted 2006) [#2]
So the plane is a mesh (not created using CreatePlane()) ?
Do you know the coordinates of the center pivot of the plane ? You should use PositionMesh to move the plane according to it's center pivot. Then use PositionEntity to move the plane back to where it was in the first place.
Ie:
PositionMesh plane, 0, 0, -5
PositionEntity plane, 0, 0, +5


Matty(Posted 2006) [#3]
Or you could place a pivot or any other blitz entity at the point you wish the object to rotate about and then parent the first entity to the second and do your rotations with the second entity. I am not sure I understand your question fully though.


Osoko(Posted 2006) [#4]
yes, the plane is a mesh, not using CreatePlane.
I just want to calculate the center point of the mesh, according to it's vertex position.


Osoko(Posted 2006) [#5]
Genrally, when you import a mesh from 3D apps, how to get it's position relative to 0,0,0.
When using for example entityX/Y/Z i'll return 0, and not the position of the center of the mesh.


b32(Posted 2006) [#6]
Depends on what meshes you are loading. For a .X or .3DS you can use VertexX, VertexY, VertexZ on every vertex. Usually I try to find the maximum/minimum X/Y/Z coordinates. The middle point is the average: (max+min)/2. I never used .B3D, so there might be another way to get its centre position. However, I think it is easiest to create the model around 0, 0, 0 in the editor.
(edit)
If I'm correct, models allways rotate around their own (0, 0, 0). In world coordinates that is EntityX(mesh, 1), EntityY(mesh, 1), EntityZ(mesh, 1). Or TFormPoint 0, 0, 0, mesh, 0->then read TFormedX/Y/Z()


Osoko(Posted 2006) [#7]
Yes, it's easier, but the purpose is to have the model in a correct location in the editor, and after in blitz.
i've tried this for the pivot (according it's work on plane in X format )

SurfaceRef#=GetSurface ( Mesh, 1 )

TFormPoint VertexX(SurfaceRef#,0),0,0,Mesh,0

Xmin#= TFormedX()

TFormPoint VertexX(SurfaceRef#,1),0,0,Mesh,0

Xmax# = TFormedX()

TFormPoint 0,VertexY(SurfaceRef#,0),0,Mesh,0

Ymin#= TFormedY()

TFormPoint 0,VertexY(SurfaceRef#,2),0,Mesh,0

Ymax#= TFormedY()

TFormPoint 0,0,VertexZ(SurfaceRef#,0),Mesh,0

Zmin#= TFormedZ()

TFormPoint 0,0,VertexZ(SurfaceRef#,2),Mesh,0

Zmax#= TFormedZ()

pivotX#=(Xmin#+Xmax#)/2
pivotY#=(Ymin#+Ymax#)/2
pivotZ#=(Zmin#+Zmax#)/2

Well it's give the center pivot of the object.
After that, i position the object with these values negatives to set it on the center of the scene.
then i copy it to another mesh, and place the new mesh in the original location. The new mesh will now rotate on his axe.
It's quiet complicated to do posing in 3D apps with blitz !

Now, i want the object to be facing the camera, but not with pointEntity, the object ( a character map in 2d Plane ) has to rotate only in Y.

I've lakes in math. How to calculate Y according to the camera position ? ( The plane remains static, only rotate in Y)

Is it a simple way for doing what i do ?

Thank's


Stevie G(Posted 2006) [#8]
Why not just center the mesh with this function.

Function MESHcenter( Mesh )

  W# = meshwidth( Mesh )
  H# = meshheight( Mesh )
  D# = meshdepth( Mesh )
  fitmesh - W*.5, -H*.5, -D*.5, W, H, D

end function


I think you need to explain what your trying to do a bit better 'cos as far as I can see people have already given you the solution.

Stevie


b32(Posted 2006) [#9]
Hmm, Fitmesh doesn't seem to work on a plane. I think it is because the depth becomes infinitive (or at least very big)
For the rotation, you could still use PointEntity, then read EntityYaw, rotate the object back and apply the EntityYaw again:
;store original pitch/roll
p# = entitypitch(mesh)
r# = entityroll(mesh)
pointentity mesh, mesh2
;read new yaw
y# = entityyaw(mesh)
rotateentity mesh, p, y, r

However, that is not a very nice method.
You can also use Atan2(x, y). It returns the angle between (0,0) and (x,y)
So, use only the X/Z of the entities, sort of like this:
x#=EntityX(mesh1,1)-EntityX(mesh2,1)
y#=EntityZ(mesh1,1)-EntityZ(mesh2,1)
ang#=450-Atan2(x,y)
rotateentity mesh2, entitypitch(mesh2,1), ang, entityroll(mesh2,1),1


----(edit)
Wait, this works:
RotateEntity mesh, 0, EntityYaw(camera, 1), 0


Stevie G(Posted 2006) [#10]
@ bram32, I'm well aware that a plane cannot be treated as a mesh but this guys terminology is very confusing.

In his example above using tform he has access to vertex info so it isn't a plane entity class, it must be a mesh quad. In which case, this centering method will work.

Stevie


b32(Posted 2006) [#11]
Ah, I see. Well, sometimes Basic is easier than English I guess. Anyway, I have used fitmesh on all types of meshes when I was testing my editor, and I discovered that if you try to fitmesh a mesh quad, it will dissapear:



Stevie G(Posted 2006) [#12]
Hmmm, yes, the reason being that the mesh depth is 0. I guess that that function doesn't work for meshes where all points are coplanar.

Why on earth the guy can't just center his "plane" in the mesh editor before saving or create a quad ( as above ) or use a sprite is beyond me. Sometimes people really do overcomplicate the simplest things.

Stevie


b32(Posted 2006) [#13]
I think Osoko wants to place the objects in the 3d editor, but when he does that, the planes don't rotate around their own axis anymore, but around (0,0,0)


Osoko(Posted 2006) [#14]
Sorry for having answer ealier, i just come back from work.

Yes bram32. your right, the plane ( 2dcharcaterwithAplha.X ) was rotating in (0,0,0).
The code with Tform permits to recenter it, then i parent it with a gizmo, and i replace them. Now, i rotate the gizmo which has a correct center



Your code :
x#=EntityX(mesh1,1)-EntityX(mesh2,1)
y#=EntityZ(mesh1,1)-EntityZ(mesh2,1)
ang#=0-Atan2(x,y)
rotateentity mesh2, entitypitch(mesh2,1), ang, entityroll(mesh2,1),1

works perfectly have just replaced 450 by 0.
Why 450 ?

Thank a lot for the math, i think it's trivial for you programmers, but i'm essentiely a graphist which learn the code.


b32(Posted 2006) [#15]
Ah .. well, sometimes it needs the 450, but apparently only in 2D. I'm using a lot of Cos/Sin with Atan2, so I think that is where I picked up the habit.