View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Hitbox setup

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

19 replies to this topic

### #1Telanor  Members

Posted 13 March 2014 - 08:02 PM

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:

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

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

### #2eppo  Members

Posted 14 March 2014 - 03:22 AM

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.

### #3Telanor  Members

Posted 14 March 2014 - 01:48 PM

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.

### #4Telanor  Members

Posted 16 March 2014 - 01:53 PM

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;
}
}
}


### #5Kryzon  Prime Members

Posted 16 March 2014 - 08:56 PM

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, 16 March 2014 - 09:22 PM.

### #6Telanor  Members

Posted 16 March 2014 - 09:22 PM

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:

And the code for that was just:

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


### #7Kryzon  Prime Members

Posted 16 March 2014 - 09:44 PM

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, 16 March 2014 - 09:44 PM.

### #8Telanor  Members

Posted 16 March 2014 - 09:50 PM

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:

Negating the dir doesn't change much.

### #9Kryzon  Prime Members

Posted 16 March 2014 - 09:57 PM

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.

### #10Telanor  Members

Posted 16 March 2014 - 10:00 PM

Still a rather odd result from that:

### #11Ashaman73  Members

Posted 21 March 2014 - 04:50 AM

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, 21 March 2014 - 04:52 AM.

Ashaman

### #12Telanor  Members

Posted 21 March 2014 - 04:13 PM

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:

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

### #13Ashaman73  Members

Posted 24 March 2014 - 02:10 AM

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.

Ashaman

### #14Telanor  Members

Posted 24 March 2014 - 07:33 PM

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:

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.

### #15eppo  Members

Posted 25 March 2014 - 05:51 AM

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.

### #16Telanor  Members

Posted 25 March 2014 - 08:43 PM

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.

### #17Ashaman73  Members

Posted 26 March 2014 - 02:10 AM

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;
}


Ashaman

### #18Telanor  Members

Posted 26 March 2014 - 10:35 AM

Doing that gives me this:

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.

### #19Ashaman73  Members

Posted 27 March 2014 - 12:59 AM

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 ?

Ashaman

### #20Telanor  Members

Posted 27 March 2014 - 10:20 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.