# Hitbox setup

This topic is 2036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### 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.

##### 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.

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


##### 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

##### 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);


##### 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

##### 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.

##### 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.

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

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

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 13
• 9
• 11
• 15