Re: Beginner and Vectors
- From: "Robert Dunlop [MS MVP]" <rdunlop@xxxxxxxx>
- Date: Tue, 19 Apr 2005 08:24:57 -0700
"Iain" <Iain@xxxxxxxxxxxxxxxxxx> wrote in message
news:pyce8tppbi99.o8mu7q236yed$.dlg@xxxxxxxxxxxxx
> I'm just writing my first Direct3D program, so be kind....
>
> I want to join some sphere's with cylinders.
>
> I've created my spheres and positioned them and it looks cool.
>
> I've created a cylinder (with Mesh.Cylinder) which is the same length as
> the distance between the cylinders.
>
> what I need is a Matrix which will translate and rotate the cylinder so
> that one end is in the first sphere and the second end is in the second
> sphere.
>
> So I tried this
>
> private void DrawCylinder(float xStart, float yStart, float zStart, float
> xEnd, float yEnd, float zEnd)
A cylinder created with Mesh.Cylinder will have its axis along the Z-axis.
Your application needs to rotate the cylinder so that this axis transforms
to an axis defined by a vector between the two spheres. One way to do this
is to determine a single axis of rotation around which to rotate the
cylinder, and the angle of rotation required. The axis of rotation between
the original and final orientation of the cylinder will be perpendicular to
both the initial Z axis and the target axis, so we can use the cross product
to determine this axis:
Vector3 vZ=new Vector3(0,0,1);
Vector3 vDif=new Vector3(xEnd-xStart,yEnd-yStart,zEnd-zStart);
vDif.Normalize();
Vector3 cross=Vector3.Cross(vZ,vDif);
The call to Normalize() divides a vector by its own length so that the
resulting vector has a length of one, expressing only direction rather than
direction and distance.
Next, we need to find the angle of rotation around this axis, which will be
equal to the angle between the original Z axis and the new cylinder axis.
We can use the dot product to determine this, which has a result equal to
the cosine of the angle between two vectors:
float dot=Vector3.Dot(vZ,vDif);
float ang=(float) Math.Acos(dot);
Finally, we can compute our transformation around the axis of rotation:
Matrix mat=Matrix.Rotation(cross,ang);
As you note, you'll have to add translation to this matrix so that the ends
of the cylinder translate to the correct positions.
Another option might be to use the Matrix.LookAtLH method, or implement a
similar method based upon the algorithm shown in the docs for this function.
--
Robert Dunlop
The X-Zone
http://www.directxzone.com/
Microsoft DirectX MVP
-------------
The opinions expressed in this message are my own personal views and do not
reflect the official views of the Microsoft Corporation.
The MVP program does not constitute employment or contractual obligation
with Microsoft.
.
- Follow-Ups:
- Re: Beginner and Vectors
- From: Iain
- Re: Beginner and Vectors
- From: Iain
- Re: Beginner and Vectors
- References:
- Beginner and Vectors
- From: Iain
- Beginner and Vectors
- Prev by Date: Beginner and Vectors
- Next by Date: Re: Beginner and Vectors
- Previous by thread: Beginner and Vectors
- Next by thread: Re: Beginner and Vectors
- Index(es):
Relevant Pages
|