//Copies keyframe data from given keyframe public void CopyKeyFrame(C_Keyframe priorKeyframe) { for (int i = 0; i < MAX_BONE_COUNT; i++) { C_Bone tempBone = new C_Bone(); //C# copies by reference. There's a clone function, but for now I'll do this so I don't overwrite data ='( //animationName = priorKeyframe.AnimationName;//***** no longer hold animation name | AnimationClass -> KeyframeClass -> BoneClass string name = priorKeyframe.m_boneArray[i].Name; Vector3 position = priorKeyframe.m_boneArray[i].Position; Vector3 positionEnd = priorKeyframe.m_boneArray[i].PositionEnd; float length = priorKeyframe.m_boneArray[i].Length; float angle = priorKeyframe.m_boneArray[i].Angle; uint childCount = priorKeyframe.m_boneArray[i].ChildCount; int parentNumber = priorKeyframe.m_boneArray[i].ParentNumber; tempBone.Name = name; tempBone.Position = position; tempBone.PositionEnd = positionEnd; tempBone.Length = length; tempBone.Angle = angle; tempBone.ChildCount = childCount; if (parentNumber != -1) tempBone.ParentNumber = parentNumber + MAX_BONE_COUNT;//also very important. Tells which bone it is attached to; if (tempBone.Parent != null && tempBone.Parent.ChildCount < MAX_BONE_COUNT) //if space for child { tempBone.Parent.ChildCount += 1;//increment parents child count tempBone.Position = tempBone.Parent.PositionEnd;//set x/y and this is relative to the end of parent bone } //all done, add to array m_boneArray[i] = tempBone; }//end forloop }
//update one bone inside a keyframe public void Update(C_Bone updatedBone, int index) { m_boneArray[index].Name = updatedBone.Name; m_boneArray[index].Position = updatedBone.Position; m_boneArray[index].PositionEnd = updatedBone.PositionEnd; m_boneArray[index].Length = updatedBone.Length; m_boneArray[index].Angle = updatedBone.Angle; m_boneArray[index].ChildCount = updatedBone.ChildCount; m_boneArray[index].ParentNumber = updatedBone.ParentNumber; }
private Vector3 m_positionStart, m_positionEnd; //bone start positon #endregion Fields #region Constructors public C_Bone() { m_name = ""; m_positionStart = Vector3.Zero; m_length = 0; m_angle = 0; m_childCount = 0; m_parentNumber = -1; m_keyFrame = -1; m_parent = null; m_children = new C_Bone[MAX_CHILD_BONES]; }
public void LoadKeyFrames() { l_bones.Clear(); animationList = new List<string>(); System.IO.StreamReader reader = new System.IO.StreamReader("../../Content/KeyframeData.txt"); while(!reader.EndOfStream) { C_Bone tempBone = new C_Bone(); //name, position, positionEnd, length, angle, childCount Vector3 tempVector = new Vector3(0,0,0); if (l_bones.Count() % MAX_CHAR_BONES == 0) reader.ReadLine(); tempBone.AnimationName = reader.ReadLine();//bone animation name tempBone.KeyFrame = int.Parse(reader.ReadLine()); tempBone.Name = reader.ReadLine();//bone name tempVector.X = float.Parse(reader.ReadLine()); tempVector.Y = float.Parse(reader.ReadLine()); tempBone.Position = tempVector; tempVector.X = float.Parse(reader.ReadLine()); tempVector.Y = float.Parse(reader.ReadLine()); tempBone.PositionEnd = tempVector; tempBone.Length = float.Parse(reader.ReadLine()); tempBone.Angle = float.Parse(reader.ReadLine()); tempBone.ChildCount = uint.Parse(reader.ReadLine()); tempBone.ParentNumber = int.Parse(reader.ReadLine()); if(tempBone.ParentNumber != -1) tempBone.Position = l_bones[tempBone.ParentNumber].PositionEnd; //done parsing, time to add stuff //see if animation name is listed. If not add it to list bool animationListed = false; for (int i = 0; i < animationList.Count; i++) if (animationList[i] == tempBone.AnimationName) animationListed = true; if (!animationListed) animationList.Add(tempBone.AnimationName); AddChild(0, tempBone); } reader.Close(); reader.Dispose(); #region HARDCODED /* ///////////////////////////////// //NEW FRAME --- KEYFRAME 0 ///////////////////////////////// //TODO: this is hard coded now, but will eventually read from file C_Bone tempBone = new C_Bone(); tempBone.Name = "head"; tempBone.Length = 30; tempBone.Angle = MathHelper.ToRadians(90); tempBone.Position = Vector3.Zero;// Only matters for root/head AddChild(0, tempBone); tempBone = new C_Bone(); tempBone.Name = "right arm"; tempBone.Length = 60; tempBone.Angle = MathHelper.ToRadians(20); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[0]; tempBone.ParentNumber = 0; AddChild(0, tempBone); tempBone = new C_Bone(); tempBone.Name = "right forearm"; tempBone.Length = 50; tempBone.Angle = MathHelper.ToRadians(45); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[1]; tempBone.ParentNumber = 1; AddChild(0, tempBone); tempBone = new C_Bone(); tempBone.Name = "left arm"; tempBone.Length = 60; tempBone.Angle = MathHelper.ToRadians(160); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[0]; tempBone.ParentNumber = 0; AddChild(0, tempBone); tempBone = new C_Bone(); tempBone.Name = "left forearm"; tempBone.Length = 50; tempBone.Angle = MathHelper.ToRadians(135); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[3]; tempBone.ParentNumber = 3; AddChild(0, tempBone); tempBone = new C_Bone(); tempBone.Name = "torso";//bone 6 or [5] tempBone.Length = 100; tempBone.Angle = MathHelper.ToRadians(90); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[0]; tempBone.ParentNumber = 0; AddChild(0, tempBone); tempBone = new C_Bone(); tempBone.Name = "left upper leg"; tempBone.Length = 75; tempBone.Angle = MathHelper.ToRadians(120); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[5]; tempBone.ParentNumber = 5; AddChild(0, tempBone); tempBone = new C_Bone(); tempBone.Name = "left lower leg";//bone 8 tempBone.Length = 75; tempBone.Angle = MathHelper.ToRadians(100); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[6]; tempBone.ParentNumber = 6; AddChild(0, tempBone); tempBone = new C_Bone(); tempBone.Name = "right upper leg";//bone 9 tempBone.Length = 75; tempBone.Angle = MathHelper.ToRadians(60); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[5]; tempBone.ParentNumber = 5; AddChild(0, tempBone); tempBone = new C_Bone(); tempBone.Name = "right lower leg";//bone 10 tempBone.Length = 75; tempBone.Angle = MathHelper.ToRadians(80); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[8]; tempBone.ParentNumber = 8; AddChild(0, tempBone); tempBone = new C_Bone(); tempBone.Name = "left foot"; tempBone.Length = 35; tempBone.Angle = MathHelper.ToRadians(180); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[7]; tempBone.ParentNumber = 7; AddChild(0, tempBone); tempBone = new C_Bone(); tempBone.Name = "right foot"; tempBone.Length = 35; tempBone.Angle = MathHelper.ToRadians(0); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[9]; tempBone.ParentNumber = 9; AddChild(0, tempBone); ///////////////////////////////// //NEW FRAME --- KEYFRAME 1 ///////////////////////////////// tempBone = new C_Bone(); tempBone.Name = "head"; tempBone.Length = 30; tempBone.Angle = MathHelper.ToRadians(45); tempBone.Position = Vector3.Zero;// Only matters for root/head AddChild(12, tempBone); tempBone = new C_Bone(); tempBone.Name = "right arm"; tempBone.Length = 60; tempBone.Angle = MathHelper.ToRadians(-20); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[12 + 0]; tempBone.ParentNumber = 12 + 0; AddChild(12, tempBone); tempBone = new C_Bone(); tempBone.Name = "right forearm"; tempBone.Length = 50; tempBone.Angle = MathHelper.ToRadians(155); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[12 + 1]; tempBone.ParentNumber = 12 + 1; AddChild(12, tempBone); tempBone = new C_Bone(); tempBone.Name = "left arm"; tempBone.Length = 60; tempBone.Angle = MathHelper.ToRadians(155); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[12 + 0]; tempBone.ParentNumber = 12 + 0; AddChild(12, tempBone); tempBone = new C_Bone(); tempBone.Name = "left forearm"; tempBone.Length = 50; tempBone.Angle = MathHelper.ToRadians(130); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[12 + 3]; tempBone.ParentNumber = 12 + 3; AddChild(12, tempBone); tempBone = new C_Bone(); tempBone.Name = "torso"; tempBone.Length = 100; tempBone.Angle = MathHelper.ToRadians(90); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[12 + 0]; tempBone.ParentNumber = 12 + 0; AddChild(12, tempBone); tempBone = new C_Bone(); tempBone.Name = "left upper leg"; tempBone.Length = 75; tempBone.Angle = MathHelper.ToRadians(205); tempBone.Position = Vector3.Zero;// Only matters for root/head 5 6 5 8 7 9 tempBone.Parent = l_bones[12 + 5]; tempBone.ParentNumber = 12 + 5; AddChild(12, tempBone); tempBone = new C_Bone(); tempBone.Name = "left ower leg";//bone 8 tempBone.Length = 75; tempBone.Angle = MathHelper.ToRadians(180); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[12 + 6]; tempBone.ParentNumber = 12 + 6; AddChild(12, tempBone); tempBone = new C_Bone(); tempBone.Name = "right upper leg"; tempBone.Length = 75; tempBone.Angle = MathHelper.ToRadians(-45); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[12 + 5]; tempBone.ParentNumber = 12 + 5; AddChild(12, tempBone); tempBone = new C_Bone(); tempBone.Name = "right lower leg"; tempBone.Length = 75; tempBone.Angle = MathHelper.ToRadians(170); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[12 + 8]; tempBone.ParentNumber = 12 + 8; AddChild(12, tempBone); tempBone = new C_Bone(); tempBone.Name = "left foot"; tempBone.Length = 35; tempBone.Angle = MathHelper.ToRadians(235); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[12 + 7]; tempBone.ParentNumber = 12 + 7; AddChild(12, tempBone); tempBone = new C_Bone(); tempBone.Name = "right foot"; tempBone.Length = 35; tempBone.Angle = MathHelper.ToRadians(235); tempBone.Position = Vector3.Zero;// Only matters for root/head tempBone.Parent = l_bones[12 + 9]; tempBone.ParentNumber = 12 + 9; AddChild(12, tempBone); */ #endregion vertices = new VertexPositionColor[l_bones.Count() * 2];//2 vertices per bone //after reading bone data load vertices for (int i = 0; i < l_bones.Count(); i++) { vertices[i * 2].Position = l_bones[i].Position; vertices[i * 2].Color = Color.Black; vertices[i * 2 + 1].Position = l_bones[i].PositionEnd; vertices[i * 2 + 1].Color = Color.Black; } //Load keyframe and animation indices i_keyFrame = new int[vertices.Length / (MAX_CHAR_BONES * 2)];//create index array for each keyframe for (int i = 0; i < i_keyFrame.Length; i++) i_keyFrame[i] = i * MAX_CHAR_BONES * 2; }
public void AddKeyFrame() { int start = l_bones.Count - MAX_CHAR_BONES;//start at first bone of the last keyframe int end = start + MAX_CHAR_BONES;//end at the last bone of the last keyframe string name, animationName;// bone name, and the animation bone belongs to Vector3 position, positionEnd;//bone start positon float length, angle;//bone length and angle (radians) uint childCount;//how many child bones does this bone have? int parentNumber; int keyFrame;//which key this bone belongs to //copy data from prior keyframe for (int i = start; i < end; i++) { C_Bone tempBone = new C_Bone(); //C# copies by reference. There's a clone function, but for now I'll do this so I don't overwrite data ='( animationName = l_bones[i].AnimationName; keyFrame = l_bones[i].KeyFrame +1;//IMPORTANT because this is the next keyframe name = l_bones[i].Name; position = l_bones[i].Position; positionEnd = l_bones[i].PositionEnd; length = l_bones[i].Length; angle = l_bones[i].Angle; childCount = l_bones[i].ChildCount; parentNumber = l_bones[i].ParentNumber; tempBone.AnimationName = animationName; tempBone.KeyFrame = keyFrame; tempBone.Name = name; tempBone.Position = position; tempBone.PositionEnd = positionEnd; tempBone.Length = length; tempBone.Angle = angle; tempBone.ChildCount = childCount; if(parentNumber != -1) tempBone.ParentNumber = parentNumber + MAX_CHAR_BONES;//also very important. Tells which bone it is attached to; AddChild(keyFrame, tempBone); } }
//Skeleton functions public void AddChild(int keyFrame, C_Bone newBone) { if (newBone.Parent != null && newBone.Parent.ChildCount < MAX_CHILD_COUNT) //if space for child { newBone.Parent.ChildCount += 1;//increment parents child count newBone.Position = newBone.Parent.PositionEnd;//set x/y and this is relative to the end of parent bone } //set data if (newBone.Name.Length == 0) newBone.Name = "bone " + l_bones.Count().ToString(); l_bones.Add(newBone); }
public const int MAX_BONE_COUNT = 12; // 12 is current maximum bone count!! #endregion Fields #region Constructors public C_Keyframe() { m_boneArray = new C_Bone[MAX_BONE_COUNT]; }