Creating a Box from vectors

I have as input a point and 3 vectors originating from that point and spanning/describing a box. This box doesnt have to be parallel to the axes, which means I can’t just take the length from those 3 vectors and use them as width, depth and height for MeshBuilder.CreateBox().
What I want is to create mesh/box that is described by those vectors. I tried using MeshBuilder.ExtrudeShape but couldnt get it to work (see Babylon.js Playground)

I think it might not be quite clear what exactly you are trying to do. When you say, “a box that is described by those vectors”, it could mean a few different things:

  1. The point is the center of the box (my first guess)
  2. The point is one of the corners of the box (if so, which?)
  3. The vectors describe the distance from the center to the faces of the box
  4. The vectors describe the entire length of a given dimension for the box

Either way, the MeshBuilder.CreateBox API seems like a better fit than ExtrudeShape, even if you have to modify the created box afterwards to make it align with the given axis vectors.

You could either create a box and rotate it according to the difference between the world origin and the three vectors. Or you could compute the vertex positions manually and apply the VertexData to the box?

Yeah I was having a hard time trying to formulate my problem, sorry for that. I was also looking for an image that showed my problem/situation on google, but couldn’t find one.
What I have is a point, which is the back-left lower corner of the box, and three vectors from that point that describe the width, height and depth of the box (see this very rough sketch :sweat_smile:)

What I am trying now is creating a box with width, depth and height set to the .length() of those vectors that I get as input. Then I change the box’s position to match my point from input.
At that point, the only thing missing is the rotation since it is still parallel to the x and z axis.
I tried some things to rotate them, but couldn’t get any of them to work: The box changed position after changing the pivot or adding a mesh as parent. I think I’ll have to look more into the rotation stuff and I’ll probably / hopefully get it to work.

In that case, how about defining the vertices manually? You can get the remaining corners of the box by adding the axis vectors; no rotation would be necessary. It should be quite simple if this is all you need.

For some examples, see this page: Create Custom Meshes From Scratch | Babylon.js Documentation

1 Like

Yeah I think this would be the correct/best solution for the problem that I presented. I’m using a box/simple meshes to get to know BabylonJS, but I will have to move on to creating some more complicated shapes in a few days where I cant just define every vertex manually, but will still need to rotate them. So sadly this wont work for me, but thank you very much for the help, I didn’t know about VertexData before, and it seems like something that could come in handy for other things.

Well, you can certainly create very complex meshes in code (or use an existing factory method if it fits the requirements). Can’t really say more unless you have a more specific problem, but feel free to ask again should you get stuck with other shapes in the future :slight_smile: