Hitbox setup

Started by
18 comments, last by Telanor 10 years ago

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;

			DeferredRenderer.ModelDrawer.Add(box);
			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;

			DeferredRenderer.ModelDrawer.Add(box);
			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).

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

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.

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.

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.

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.


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;

			DeferredRenderer.ModelDrawer.Add(box);
			boneBoxDictionary[bone.Number] = box;
	}
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.

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 ?

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.

This topic is closed to new replies.

Advertisement