• ### Announcements

GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Followers 0

# Hitbox setup

## 19 posts in this topic

I'm trying to build hitboxes around the bones of a model. I took the absolute matrix for each bone and used that to position and orient the boxes. Then I took used the distance between the current bone and its parent as the box length. The result I got was this mess:

[attachment=20405:RuinValor 2014-03-13 18-42-11-46.png]

Also I tried shifting the center to be at the point between the bone and its parent:

[attachment=20406:RuinValor 2014-03-13 19-15-20-71.png]

Which is a bit better but they're still not quite connecting up. Anyone have some advice on how to do this?
1

##### Share on other sites

Given the three orthogonal axes defined by a bone matrix, you could search for extreme vertices that are affected by the bone in the underlying mesh. If you search in both the positive and negative directions you'll end up with six planes that form an oriented box. This box you can then transform into world space using the same matrices as used for the bones.

Or you can transform the box by the inverse of its bone matrix if you need an AABB that's positioned at the origin. Would take up less memory.

1

##### Share on other sites
Doing that, I'd still end up with the same orientation I'm using now, which seems to be the main issue. I can post the code if it'll help but there's not much to it.
1

##### Share on other sites
Here's the code

private static void BoneDraw(Model model)
{
const float width = 1f;
var bones = model.Bones;

foreach(var bone in bones)
{
var parent = bone.Parent;

if(parent != null)
{
var boneMatrix = model.FinalBoneMatrices[bone.Number];
var parentMatrix = model.FinalBoneMatrices[parent.Number];
var length = Vector3.Distance(parentMatrix.TranslationVector, boneMatrix.TranslationVector);
var dir = Vector3.Normalize(parentMatrix.TranslationVector - boneMatrix.TranslationVector);
var center = boneMatrix.TranslationVector + dir * length / 2;

var box = new Box(center, length, width, width);
var temp = Matrix3X3.CreateFromMatrix(boneMatrix);

box.OrientationMatrix = temp;

boneBoxDictionary[bone.Number] = box;
}
}
}

1

##### Share on other sites
Hello.
What are the parameters of that Box() function?

I don't understand why you're orienting a box with the matrix from either of the bones (the child bone in this case).
There's no guarantee that the either of the bones are facing in the direction that would make the box bridge the two bones, since you don't know how the skeleton is arranged in space.

Therefore, it would make sense to consider all joints as points without orientation, and to generate a "look at" rotation matrix or quaternion from both the "dir" vector that you calculated and an additional vector such as the "up" vector of the parent bone, and use this resulting matrix or quaternion to orient the box.

An example of such a method:
- https://docs.unity3d.com/Documentation/ScriptReference/Quaternion.SetLookRotation.html
- http://www.gamedev.net/topic/555545-look-vector-to-rotation-matrix/ Edited by Kryzon
1

##### Share on other sites
The Box function is defined as: Box(Vector3 position, float width, float height, float length)

I did try creating a look-at matrix before, but it didn't turn out well. Mainly the leg bones end up really out of place:

[attachment=20456:RuinValor 2014-03-16 23-16-33-92.png]

And the code for that was just:

var lookAt = Matrix.LookAtRH(center, parentMatrix.TranslationVector, boneMatrix.Up);
var temp = Matrix3X3.CreateFromMatrix(lookAt);

1

##### Share on other sites
Try replacing the "parentMatrix.TranslationVector" argument with the "dir" vector.
You might have to negate "dir" since you calculated it from the child to the parent.

Using "dir" as the forward vector for the look-at matrix, the box should point to both the extremity bones. Edited by Kryzon
1

##### Share on other sites
Hmm, that's a bit odd to use the dir vector there. The 2nd parameter is supposed to be the target position. Anyway here's what that looks like:

[attachment=20460:RuinValor 2014-03-16 23-46-19-82.png]

Negating the dir doesn't change much.
1

##### Share on other sites
I see. I had thought the LookAtRH() would take in a position, the forward and up vectors.
It seems to take source position, target position and up vector instead.

Perhaps this will work:
var lookAt = Matrix.LookAtRH(parentMatrix.TranslationVector, boneMatrix.TranslationVector, parentMatrix.Up);
The starting point is the parent, the target point is the child and the up vector is from the starting point.
1

##### Share on other sites
Still a rather odd result from that:

[attachment=20461:RuinValor 2014-03-16 23-59-52-82.png]
1

##### Share on other sites

In my opinion you have several issues with your code, try something like this :

private static void BoneDraw(Model model)
{
const float width = 1f;
var bones = model.Bones;

// define new array
var bonesHasBeenDrawn =... // !!!

// first pass: draw parent->child bones !
foreach(var bone in bones)
{
var parent = bone.Parent;

if(parent != null && boneHasBeenDrawn[parent.Number]!=true ) // !!!
{
var boneMatrix = model.FinalBoneMatrices[bone.Number];
var parentMatrix = model.FinalBoneMatrices[parent.Number];

//handle the parent bone, not the bone ! therfor swap parent and bone here
var length = Vector3.Distance(parentMatrix.TranslationVector, boneMatrix.TranslationVector);
var dir = Vector3.Normalize(boneMatrix.TranslationVector-parentMatrix.TranslationVector); // !!!
var center = parentMatrix.TranslationVector + dir * length / 2; // !!!

var box = new Box(center, length, width, width);
// use the parent here !
var temp = Matrix3X3.CreateFromMatrix(parentMatrix); //!!!

box.OrientationMatrix = temp;

boneBoxDictionary[bone.Number] = box;

// done
boneHasBeenDrawn[parent.Number] = true;
}
}

// second pass: draw bones without child and therefor without known length
foreach(var bone in bones)
{

if(boneHasBeenDrawn[bone.Number]!=true )
{
var boneMatrix = model.FinalBoneMatrices[bone.Number];

//virtual length
var length = 1.0; // default length for unknown bones

// point along z-axis, or whatever your lookat axis is
var center = boneMatrix.TranslationVector + boneMatrix.LookAtVector * length *0.5 ;

var box = new Box(center, length, width, width);
// use the parent her !
var temp = Matrix3X3.CreateFromMatrix(boneMatrix);

box.OrientationMatrix = temp;

boneBoxDictionary[bone.Number] = box;

// done
boneHasBeenDrawn[bone.Number] = true;
}
}

}


First of, if you take the parent bone as direction, then you are actually drawing the parent bone and you need to orient it relative to it and not to the current bone. The second issue is, that you dont handle leaf-bone with it, therefor you need to draw the leaf-bones with an virtual length (the length is unknown).

Edited by Ashaman73
2

##### Share on other sites
So I gave that a try, minus the 2nd pass stuff since there is no "LookAtVector" and I don't think I actually want the end bones anyway, since those should be things like the finger tips and the toes. This is what I got:

[attachment=20542:RuinValor 2014-03-21 18-08-42-37.png]

The fingers and legs are perfect. The hips and arms are the main issue now.
1

##### Share on other sites

The problem is, that bones are just a transformation matrix, without any length (this is more or less cosmetic in the modelling tool). Therefor you will have an issue with your approach, if the bones are not directly connected to its parents (this is the case if you model the bones with an offset). The lookat vector of the your bone matrix is most likely either the Y or the Z vector and it would be better in this case to use it instead of the difference vector between bone and parent. In other words, the second pass for all bones would be the more accurate way to do it.

1

##### Share on other sites

The lookat vector of the your bone matrix is most likely either the Y or the Z vector and it would be better in this case to use it instead of the difference vector between bone and parent. In other words, the second pass for all bones would be the more accurate way to do it.

I'm still not sure which vector you're referring to exactly, so I tried the Forward/Up/Right vectors from the parent matrix, and also tried a few like this:

var dir = Matrix.LookAtRH(parentMatrix.TranslationVector, boneMatrix.TranslationVector, parentMatrix.Up).Forward;

Result was just weirdly placed bones:

[attachment=20595:RuinValor 2014-03-24 20-10-34-90.png]

The artist took a look at the bones and thought maybe it had something to do with the transforms on the arm bones being world aligned instead of bone aligned(?), but changing them didn't seem to have any effect. I've attached several pictures of the bones as seen in Maya along with the hierarchy. Maybe someone can spot something wrong from there.
1

##### Share on other sites

I see that whenever the boxes in the first picture align with the bones, their joints (spheres) in the rig-picture align as well.

The flat-laying boxes are the ones that seem to have axially aligned joints.

1

##### Share on other sites
Yea like I said, the artist (riu) noticed that as well and tried adjusting the right arm bones so they were aligned, but it didn't seem to change the orientation of the boxes at all.
1

##### Share on other sites

I'm still not sure which vector you're referring to exactly, so I tried the Forward/Up/Right vectors from the parent matrix, and also tried a few like this:

It is the vector, the bones are pointing along. A bone is eventually only a transformation matrix, the representation in the modelling tool will draw the bone starting at the transformation matrix and point along one axis of it (eg. z-axis, but this depends on the modelling tool). It is important, that the direction of the bone depends only on the final transform matrix of bone and not on the parent transform. The only useful thing is, that you could use the relationship between parent and child to guess the length of a bone, but this only works for 'connected' bones.

You should start with the bones and a virtual length of eg 1 (or some other useful, displayable length) and leave the parent out of it completly. Try to get the orientation of the bones right in the first place, once this has been done, try to optimize the length by looking at the parent-child relationshipment.

Something like this:

	foreach(var bone in bones)
{
var boneMatrix = model.FinalBoneMatrices[bone.Number];
var length = 1.0; // default length for unknown bones

// point along z-axis, or whatever your lookat axis is
var center = boneMatrix.TranslationVector + boneMatrix.Forward* length *0.5 ;

var box = new Box(center, length, width, width);
// use the parent her !
var temp = Matrix3X3.CreateFromMatrix(boneMatrix);

box.OrientationMatrix = temp;

boneBoxDictionary[bone.Number] = box;
}

1

##### Share on other sites
Doing that gives me this:

[attachment=20603:RuinValor 2014-03-26 12-31-55-61.png]

Which I guess looks right, but if I try to calculate the length it just ends up like the image from the previous post. Also it seems like the boxes that are lying flat correspond to the round ball joints from the Maya images.
1

##### Share on other sites

To be honest, there seems to be something else wrong. I would expect kind of symmetrically build up according to the maya model, but there are some clear asymmetrically bones (leg,arms,hips). So, the question is, is the skeletal animation system working at all ? Is your box rendering code ok ? Eg the green bone at the right arm is quite off, the same could be say about the left green leg bone, why ?

1

##### Share on other sites
The skinned animation system works just fine and the box rendering code should be fine, I've never had any issues with it. The reason for that green bone on the right arm is because its a shield attachment bone. I'm not sure what the deal is with the one on the left leg though.
0

## Create an account

Register a new account