Jump to content
  • entries
    943
  • comments
    5,899
  • views
    924,311

Bone Attachments


Josh

2,850 views

 Share

In Leadwerks Game Engine 4, bones are a type of entity. This is nice because all the regular entity commands work just the same on them, and there is not much to think about. However, for ultimate performance in Leadwerks 5 we treat bones differently. Each model can have a skeleton made up of bones. Skeletons can be unique for each model, or shared between models. Animation occurs on a skeleton, not a model. When a skeleton is animated, every model that uses that skeleton will display the same motion. Skeletons animations are performed on one or more separate threads, and the skeleton bones are able to skip a lot of overhead that would be required if they were entities, like updating the scene octree. This system allows for thousands of animated characters to be displayed with basically no impact on framerate:

This is not some special case I created that isn't practical for real use. This is the default animation system, and you can always rely on it working this fast.

There is one small issue that this design neglects, however, and that is the simple situation where we want to attach a weapon or other item to an animated character, that isn't built into that model. In Leadwerks 4 we would just parent the model to the bone, but we said bones are no longer entities so this won't work. So how do we do this? The answer is with bone attachments:

auto gun = LoadModel(world, "Models/gun.gltf");
gun->Attach(player, "R_Hand");

Every time the model animation is updated, the gun orientation will be forced to match the bone we specified. If we want to adjust the orientation of the gun relative to the bone, then we can use a pivot:

auto gun = LoadModel(world, "Models/gun.gltf");
auto gunholder = CreatePivot(world);
gunholder->Attach(player, "R_Hand");
gun->SetParent(gunholder);
gun->SetRotation(0,180,0);

Attaching an entity to a bone does not take ownership of the entity, so you need to maintain a handle to it until you want it to be deleted. One way to do this is to just parent the entity to the model you are attaching to:

auto gun = LoadModel(world, "Models/gun.gltf");
gun->Attach(player, "R_Hand");
gun->SetParent(player);
gun = nullptr; //player model keeps the gun in scope

In this example I created some spheres to indicate where the model bones are, and attached them to the model bones:

Bone attachments involve three separate objects, the entity, the model, and the bone. This would be totally impossible to do without weak pointers in C++.

  • Like 2
  • Thanks 1
 Share

7 Comments


Recommended Comments

Looks great!  What if we wanted to animate an object manually? Like use code to make a player walk over certain obstacles, is this possible?

Link to comment
34 minutes ago, Lethal Raptor Games said:

Looks great!  What if we wanted to animate an object manually? Like use code to make a player walk over certain obstacles, is this possible?

Bones can also be positioned with code, using many of the same commands as the entity system (although they are not the same code internally).

  • Thanks 1
Link to comment

The physics system is an IK system, and could be used for character joints. It's not easy, but renaming a bunch of commands would not make it any easier.

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...