Re: Beginner and Vectors



"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.


.



Relevant Pages

  • Re: Beginner and Vectors
    ... >> I've created my spheres and positioned them and it looks cool. ... >> I've created a cylinder which is the same length as ... >> private void DrawCylinder(float xStart, float yStart, float zStart, float ... > A cylinder created with Mesh.Cylinder will have its axis along the Z-axis. ...
    (microsoft.public.win32.programmer.directx.managed)
  • Re: Obsolete Memory
    ... But you also say "during boot, the X axis will home"... ... Moving or not moving? ... the Z cylinder will pull the head back up and the control will fault to a 911 alarm. ... I assume you're talking about after boot when you would go into an E Stop. ...
    (alt.machines.cnc)
  • Re: Couple Old Style Space Opera questions...
    ... slightly misaligned to cylinder axis. ... cylinder is four kilometers wide and twenty ... rotation is three kilometers off the cylinder axis. ...
    (rec.arts.sf.science)
  • Re: Gravity Inside A Cylinder
    ... > and constructed from something very massive, such as neutronium (kind ... > the object would feel a gravitational pull towards A ... > along the length of the cylinder. ... but only at the axis. ...
    (sci.physics)
  • Re: insert part "transfers" in multi body part
    ... A temporary axis is an axis implied by a cylindrical face ... Surfaces refers to surface bodies, ... you inserted was only a solic cylinder, then there will be no surfaces to ... training and may not have done the tutorials. ...
    (comp.cad.solidworks)

Quantcast