[MDX] Rotating vertices to face a point

Started by
1 comment, last by sheepsteak 16 years, 8 months ago
I'm creating a hollow 3D rectangular tube shape manually. I have a start node and an end node (points in world space) where the shape obviously begins and ends. I'm also given the width and height of the tube and the thickness of the wall. At the moment I'm creating 16 vertices and the required indices to create the shape. But I need the 8 vertices at each end to face each other along the vector that runs from the start to end nodes. Given that the end node could be in any direction from the start node I need to rotate the vertices on some axis to make them face each other. I guess I have the option of creating the vertices at each end in the x-z plane and then constructing a matrix to rotate them to the required position. Or, I could somehow work out as I'm creating each vertex where it should be in relation to the centre start node. I'm thinking the first option is best but my maths is a bit rusty. I know I need to find an arbitrary axis to rotate on and the angle but I'm a bit stumped after that. I'll eventually be reading in nodes from a file and the necessary measurements but I'm trying to get this sorted first. I've attached a screen of the shape I have so far. Bear in mind that in the screen the ends are facing each other by default.
Advertisement
Moved to Maths & Physics, as this seems to be theoretical rather than specific to DX coding.

I've been doing some similar work for my 3D Pipes in D3D10 project, and went with the matrix (via vertex blending) route. For static geometry it might not be the best, but it maps quite nicely to GPU-generated geometry and is pretty simple to code.

Work out the rotation matrix for the start and finish vectors and then blend your geometry accordingly.

I toyed with the idea of using a point-and-normal from the spline between two points to derive a plane equation and then generate a ring of points on that plane. Unfortunately my head exploded whilst trying to figure this out [embarrass]

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

That link seems to point back to this thread. [wink]

Well I think I'm almost there. But there are still a few problems. If I define the start node as on the origin and put the end node at (50, 5, 0) then the tube should point along the x-axis with a slight elevation. However, it looks like this...



As you can see there is a slight rotation around the x-axis. I'll post the code where I rotate the points.

// Rotate all vertices so that they point in direction of EndNode.// EndNode vertices are on top of the StartNode ones at the moment.Vector3 startToEnd;startToEnd = Vector3.Subtract(EndNode.Position, StartNode.Position);startToEnd.Normalize();           // Use dot product to obtain anglefloat angle;Vector3 currentTarget = new Vector3(0, 0, 1);angle = Vector3.Dot(currentTarget, startToEnd);angle = (float)Math.Acos(angle);// Use cross product to get the axis of rotation and create a matrixVector3 rotationAxis;rotationAxis = Vector3.Cross(currentTarget, startToEnd);Matrix rotationMatrix = Matrix.RotationAxis(rotationAxis, angle);           // Apply matrix to all verticesVector3 pos;for (int i = 0; i < 16; i++){pos = _vertices.Position;pos.TransformCoordinate(rotationMatrix);_vertices.Position = pos;}// Extrude along the vector from start node to end nodeVector3 temp;Vector3 endToStart = EndNode.Position - StartNode.Position;for (int i = 8; i < 16; i++){temp = _vertices.Position + endToStart;_vertices.Position = temp;}


Any idea where things are going wrong?

This topic is closed to new replies.

Advertisement