Vertex global position

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
LukeBitts
Posts: 21
Joined: Fri Apr 22, 2011 6:02 am

Vertex global position

Post by LukeBitts »

I'm having trouble with vertices position in my scene. I have 2 different nodes (and meshes) and I want to change the position of a single vertex on both nodes to make then ocupy the same place in the global 3D space. The problem is, the vertex position in the mesh buffer is local based on the node position (I'm supposing that) and I can't find a way to get the average point between the vertices to move them there.

This is what I tried (pseudocode):

Code: Select all

 
new_pos = ((node1.pos + vertex1.pos) + (node2.pos + vertex2.pos))/(2,2,2)
vertex1.pos = new_pos - node1.pos; //local position
vertex2.pos = new_pos - node2.pos;
 
and some of my code:

Code: Select all

 
scene::IMeshBuffer* meshBuffer = node->getMesh()->getMeshBuffer(0);
video::S3DVertex* mb_vertices = (video::S3DVertex*)meshBuffer->getVertices();
video::S3DVertex* ar_vertices;
 
if(around.e != NULL && around.e->getHeight() == height)
            {
                ar_vertices = (video::S3DVertex*)around.e->getNode()->getMesh()->getMeshBuffer(0)->getVertices();
                amm = 0.5f;
 
                core::vector3d<f32> new_pos1 = ((node->getPosition() + mb_vertices[41].Pos) + (around.e->getNode()->getPosition() + ar_vertices[65].Pos)) / core::vector3d<f32>(2.f,2.f,2.f);
 
                mb_vertices[41].Pos = mb_vertices[44].Pos = mb_vertices[46].Pos = mb_vertices[85].Pos = mb_vertices[87].Pos = new_pos1 - node->getPos();
            }
 
smso
Posts: 246
Joined: Fri Jun 04, 2010 3:28 pm
Location: Hong Kong

Re: Vertex global position

Post by smso »

Steps:
Get the absolute transformation matrix of the scene node.
Inverse the matrix.
Use the inverse to transform the 3d point to the vertex position.

Regards
smso
LukeBitts
Posts: 21
Joined: Fri Apr 22, 2011 6:02 am

Re: Vertex global position

Post by LukeBitts »

Am I doing this right?
(and what is this supposed to do?)

Code: Select all

 
core::CMatrix4<f32> mat = node->getAbsoluteTransformation();
mat = mat.getInverse(mat);
mat.transformVect(mb_vertices[41].Pos);
 
and after that, how do I find the average point between the two vertices and the average point local position on each mesh?
smso
Posts: 246
Joined: Fri Jun 04, 2010 3:28 pm
Location: Hong Kong

Re: Vertex global position

Post by smso »

But

Code: Select all

mb_vertices[41].Pos
is in local space. It should be something like:

Code: Select all

bool getVertexPos
(
    scene::ISceneNode* node,    
    const core::vector3df& 3dPos, // in world space
    core::vector3df& vertexPos // in local/model space
) 
{
    core::matrix4 matrix = node->getAbsoluteTransformation();
    core::matrix4 inverse;
    if (!matrix.getInverse(inverse))
        return false
    printf("Inverse matrix of node found\n");
    inverse.transformVect(vertexPos, 3dPos);
    return true;
}
and after that, how do I find the average point between the two vertices and the average point local position on each mesh?
I guess the "two vertices" mentioned mean the single 3d point in world space and and "average point local position" means vertex point in the mesh buffer.
The function above should give you the respectively vertex point in the mesh buffer for different nodes.

Regards
smso
LukeBitts
Posts: 21
Joined: Fri Apr 22, 2011 6:02 am

Re: Vertex global position

Post by LukeBitts »

Sorry, I was not clear enough.

This is what I'm trying to do:
Image
each hex is a node, the red dots are the vertices I have. I'm trying to find the green dot (the average position vector between the two red ones), but I need the average vector in local position in both nodes (so I can move the red vertices to the same (global) position as the green one, this way both vertices would occupy the same global point in the 3D space [though having different local positions])
smso
Posts: 246
Joined: Fri Jun 04, 2010 3:28 pm
Location: Hong Kong

Re: Vertex global position

Post by smso »

LukeBitts wrote:Sorry, I was not clear enough.

This is what I'm trying to do:
Image
each hex is a node, the red dots are the vertices I have. I'm trying to find the green dot (the average position vector between the two red ones), but I need the average vector in local position in both nodes (so I can move the red vertices to the same (global) position as the green one, this way both vertices would occupy the same global point in the 3D space [though having different local positions])
If you want to move the hexagon nodes in gray color together, then

Code: Select all

core::vector3d green = (red1 + red2) * 0.5f;
core::vector3d red1ToGreenVector = green - red1;
...


Regards
smso
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Vertex global position

Post by hybrid »

But I guess you don't need the inverse, but the normal transformation matrix. Because you want to go from local to world space, check the positions there, and derive a translation offset which you can add to one of the nodes via setPosition.
LukeBitts
Posts: 21
Joined: Fri Apr 22, 2011 6:02 am

Re: Vertex global position

Post by LukeBitts »

smso wrote:
LukeBitts wrote:...
If you want to move the hexagon nodes in gray color together, then

Code: Select all

core::vector3d green = (red1 + red2) * 0.5f;
core::vector3d red1ToGreenVector = green - red1;
...

Regards
smso
The problem with that is this:
Image
The position of the red vertex #2 inside the node #1 is the orange dot (since its position is local), so doing (red1+red2)/2 will give me the light green point, not the dark green one... that's why I think I need to do the math in the global space and then come translate it back to the local space.
hybrid wrote:But I guess you don't need the inverse, but the normal transformation matrix. Because you want to go from local to world space, check the positions there, and derive a translation offset which you can add to one of the nodes via setPosition.
so I use the normal transformation matrix to find the position in the global space? how do I translate it back to local after that?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Vertex global position

Post by hybrid »

Why would you want to translate this back? You only have to add the calculated offset to one of the meshes. Or, if you want to merge both meshes on the green point, to both meshes.
LukeBitts
Posts: 21
Joined: Fri Apr 22, 2011 6:02 am

Re: Vertex global position

Post by LukeBitts »

I'm sorry for being so insistent, math is not my strongest point .-.

how do I get the normal transformation matrix (and do I need the inverse or not?)

this is what I tried:

Code: Select all

 
core::vector3d<f32> vertexPos = mb_vertices[41].Pos;
core::CMatrix4<f32> matrix = node->getAbsoluteTransformation();
matrix.transformVect(mb_vertices[41].Pos,vertexPos);
 
and:

Code: Select all

 
core::vector3d<f32> vertexPos = mb_vertices[41].Pos;
core::CMatrix4<f32> matrix = node->getAbsoluteTransformation();
matrix.getInverse(matrix);
matrix.transformVect(mb_vertices[41].Pos,vertexPos);
 
but neither of these work.

and can someone explain to me why this (node->getPosition() + mb_vertices[41].Pos) doesnt give me world vertex position?
smso
Posts: 246
Joined: Fri Jun 04, 2010 3:28 pm
Location: Hong Kong

Re: Vertex global position

Post by smso »

Assume the hexagons, with side = s, lie on the xy plane. The distance between centers of 2 touching hexagons as shown above is given by:

Code: Select all

f32 xoffset = 2.0f *s * sin (60.0f * core::DEGTORAD);
Translation vector needed is:

Code: Select all

core::vector3df pos(xoffset, 0.0f, 0.0f);
Then offset the second hexagon from the first one:

Code: Select all

scene::ISceneNode* hex1 = ...
scene::ISceneNode* hex2 = ...
hex2->setPosition(pos);
 
Regards
smso
LukeBitts
Posts: 21
Joined: Fri Apr 22, 2011 6:02 am

Re: Vertex global position

Post by LukeBitts »

Image
I know how to position the hexes, what I'm trying to do is to merge the green area in them when they have neighbours
LukeBitts
Posts: 21
Joined: Fri Apr 22, 2011 6:02 am

Re: Vertex global position

Post by LukeBitts »

This actually works:

Code: Select all

(node->getPosition() + mb_vertices[41].Pos)
but I had to multiply the vertex position by the scale, like this:

Code: Select all

(node->getPosition() + mb_vertices[41].Pos*10)
Thanks for your time smso and hybrid :)
Post Reply