Vertical Stacking of Meshes

Hey everyone, I’m just diving into babylon, but I’m loving it. I am stuck on something though. I want a user to be able to build custom vehicles for a game (different wheel block,engine, and weapon configurations). Maybe there’s something built in that I just haven’t discovered yet, but I can’t figure out how I’m going to stack all the different parts (wheels on bottoms, then engines, then weapons). Is there a way to tell babylon that I have like 7 different meshes I want to “connect” together but have control over each part independently? I’ve looked at merging meshes, and saw on another thread about using an invisible parent node and adding children. A more detailed example of what I’d want could be: a set of tires, 2 engines, and 3 weapons stacked vertically – with tires being on the bottom and weapons on top.

Take a look at what I have so far. Babylon.js Playground
Right now I have an engine and a set of tires that just overlap each other. If I just try to apply a position like tires.position.x = 3; I get an error.

Thanks for all the help in advance!

More information on parents Use a Parent - Babylon.js Documentation

Transform a parent and children are transformed, transform a child and it is relative to the parent. It is usually more straightforward to apply transformations to the parent after the child is added.

You can use a transformNode (as opposed to an empty or invisible mesh) as a a parent to hold a group of things together. However in your case I would use a fundamental part of your vehicle such as the body or chassis.

This is a simple example of a very basic car put together in this way Simple Car Following Path - Babylon.js Documentation and a more complicated one Making a Simple Driven Car - Babylon.js Documentation

4 Likes

Thanks, I went the transformNode route! So I think I’m close, but I’m missing something in my playground: https://www.babylonjs-playground.com/#9GWITR#3

In lines 305-308 I set the parent node for two meshes and only one is rotating. I set the engine’s class to extend BABYLON.Mesh so both the spinning box and the the engine should now be Mesh classes, but only the box is spinning. Any idea what’s going on?

Edit: a babylon box is spinning, but my custom-created class isn’t. This is what I’m trying to accomplish, but with a custom mesh class https://www.babylonjs-playground.com/#2JKA91#80

Hopefully this PG is helpful https://www.babylonjs-playground.com/#9GWITR#4.

Had to use my way of parenting so it made sense to me. One issue is combining rotations, the use of .rotate(axis, angle, space) results in the application of a rotationQuaternion to the mesh, so then applying a rotation.z in the animation then produces nonsense or no rotation.

At first I tried orientating the axel just using the rotation property but then any rotation in the animation overwrote or combined with previous rotations to produce rotations about the wrong axis . Since any cumulative method of applying rotations uses rotationQuaternions this is what I did in the end. Finally I just produced the cumulative effects of rotation using the multiplication of rotationQuaternions.

Note when you stop the wheels rotating you will need to apply a rotation to the axles to place them in the correct orientation.

1 Like

Thanks! There’s a lot in there that I have yet to understand fully but I see what you’re doing!