I need a solution to build a cone in 3D.
It should be created with a method like this … CreateCone(float radius, Vector3 head, Vector3 end).
The result of the method should be a Mesh or a Vertex-Array.
A simple algorithm is also sufficient.
It’s harder than I thought.
I am working over the night on a solution, but nothing really works correctly.
As a first step, I am trying to find the right points in the room around a center point.
But these points must be orthogonal to the axis,
because the line between Point A and B can somehow be in the room.
I would just align it around the Center and you a transformation matrix to later to adjust the dimensions and position. e.g. you can say that the cone should have a height of one unit and a radius of 0.5f.
Then you need the tesselation + 2 vertices tesselation * 6 indices and then you create the following vertices and indices. It is untested, but it should work like that.
private void AddVertices()
{
// Top vertex
AddVertex(new VertexPositionColor(Vector3.Up, color)); // 0, 1, 0
// Center vertex
AddVertex(new VertexPositionColor(Vector3.Zero, color)); // 0, 0, 0
// Bottom circle vertices
for (var i = 0; i < tessellation; i++)
{
var position = GetCircleVector(i, tessellation) * 0.5f;
AddVertex(new VertexPositionColor(position, color));
}
}
private void AddIndices()
{
for (var i = 0; i < tessellation; i++)
{
// Skip top vertex and bottom vertex
var curr = i + 2;
var next = (curr + 1) % tessellation;
// Triangle from circle to top
AddIndices(offset, 0, next);
// Triangle from cricle to center
AddIndices(offset, 1, next);
}
}
private static Vector3 GetCircleVector(int i, int tessellation)
{
var angle = i * MathHelper.TwoPi / tessellation;
var dx = (float)Math.Cos(angle);
var dz = (float)Math.Sin(angle);
return new Vector3(dx, 0, dz);
}
I can render the cones in the 3d space.
I can also translate them in the space, but the main problem is the rotation of the cone.
The rotation depents on the start and the end point of the cone.
The code below shows the draw-method. The variables x,y,z is what I need for the rotation.
In my mind, these values should be generated by the start and end point of the cone.
I am searching since a few days to find a solution for that.
This combined matrix looks like the world matrix for an object. If that’s already the world matrix, then what is the first world matrix in your draw call?
How do you build it and what’s it’s purpose? Some kind of transformation hierarchy?
At some point i wrote out a example prism class for someone which is basically the same thing as a cone. If you flaten out the top of the prism you have a cone.
The full code is in the post linked below.
Specifically look to the methods BuildPrism(…) and GetPrismVertices(…) in the second part of it.
Basically all that example code can just be copy pasted to game1 if you want to run it.
I have a start vector A(x,y,z) and a vector B(x,y,z).
All I need to do is to transform a cone into that start and end point.
I can allready build a cone, and I can also translate it in the space, but I don’t know the
right angle.
Here is some code, it works but not perfect. The angle fits not perfectly into the given vector points A and B.
`
var d = endPosition - startPosition;
var len = d.Length();
var k = new Vector3(0, 1, 0);
var a = (float) Math.Acos(Vector3.Dot(d, k) / (d.Length() * k.Length()));
var rot = Matrix.CreateFromAxisAngle(
Vector3.Cross(
new Vector3(0, 1, 0), d) / d.Length(), a);
var color = Color.Lerp(
Color.Green, Color.Red,
Helper.MapRange(
len, (float)fMin, (float)fMax, 0f, 1f));
cylinderPrimitive.Draw(
world
* Matrix.CreateScale(1f, len, 1f)
* rot
* Matrix.CreateTranslation(startPosition + (0.5f * d)),
view,
projection,
color);
conePrimitive.Draw(
world
* rot
* Matrix.CreateScale(0.5f)
* Matrix.CreateTranslation(endPosition),
view,
projection,
color);`
coneUp is endPosition-startPosition.
coneForward is any vector that’s perpendicular to coneUp.
coneRight is perpendicular to coneUp as well as coneForward.
The length of coneUp, coneForward and coneRight determine the scale in the respective direction.