### Author Topic: Rotating a cylinder in a direction given by a simple vector  (Read 1183 times)

#### Knuffi

• byte
• Posts: 19
##### Rotating a cylinder in a direction given by a simple vector
« on: April 16, 2012, 10:40:51 pm »
I don't get it.

I have a cylinder representing a line. And on building it, I want to translate it to a certain point (working) and rotate it, that it looks into the same direction as a given vector. The last thing does not work.
When I give the vector
* (1/0/0), the cylinder's direction is the z-axis (or -z, I could not see),
* (0/1/0) the cylinder's direction is also the z-axis (or -z)
* (0/0/1) the cylinde's direction is the x-axis.

I changed the coordinate system to an "upright" as needed in school.

Code: [Select]
`public static void addGerade(Gerade g){ Object3D gt = Primitives.getCylinder(6, 0.1f, 150);// Changing the ccordinates from the calculated line to the ones needed to draw it double ax = g.getA().getX1(); double ay = -g.getA().getX3(); double az = g.getA().getX2(); double rx = g.getV().getX(); double ry = -g.getV().getZ(); double rz = g.getV().getY(); SimpleVector Aufpunkt = new SimpleVector (ax, ay, az); SimpleVector Richtung = new SimpleVector(rx, ry, rz); Matrix r = Richtung.getRotationMatrix(); gt.setRotationMatrix(r); gt.translate(Aufpunkt); gt.setTexture("linet"); gt.build(); world.addObject(gt); buffer.clear(java.awt.Color.DARK_GRAY); world.renderScene(buffer);// world.drawWireframe(buffer, Color.WHITE); world.draw(buffer); buffer.update(); }`

Almost the same code works fine with points (as already said, translating works) and even planes (translating and rotating so that the normal vector looks into a given direction.

Can anybody help?

#### EgonOlsen

• Posts: 11827
##### Re: Rotating a cylinder in a direction given by a simple vector
« Reply #1 on: April 16, 2012, 10:59:26 pm »
You have to keep in mind that the cylinder created by the Primitives class doesn't point into z-direction. If you ensure this by doing something like

Code: [Select]
`cyly = Primitives.getCylinder(6, 0.1f, 150);cyly.rotateX((float) Math.PI/2f);cyly.rotateMesh();cyly.clearRotation();`
and then apply your rotation, it should work.

#### Knuffi

• byte
• Posts: 19
##### Re: Rotating a cylinder in a direction given by a simple vector
« Reply #2 on: April 17, 2012, 07:14:09 am »
Thanks, it worked!

I tried rotating around x-axis, but I did not know that I had to clear the Rotation.