net.minecraft.src
Class Bone

java.lang.Object
  extended by net.minecraft.src.Bone

public class Bone
extends java.lang.Object

The Bone class makes it possible to create skeletons, which should help you out in animating your mobs a little bit more easy. However, since you won't work with a graphical interface, creating bones will be different from what you are probably used to.

First, you will need to instantiate every Bone in the constructor of your model file. The default orientation, when all angles are set to zero, will be in the vector (0, 0, length), meaning it will always point backwards on a regular model. You can also set what its parent node is. If a Bone does not have a parent node, it is assumed it is the root node. Each Bone can only have one parent, but several children. Also, all children will inherit the offset position of the root node.

The neutral position basically defines in what direction the Bone normally faces when in rest. This will not affect the rotation of any model currently attached to it or the rotation of the child nodes, but will affect the position of the child nodes when recalculating their positions. The length always defines how far each child Bone will be placed, since child Bones are always placed at the end of their parent Bone.

Once you're ready to render, you can call the prepareDraw method. You only need to apply it to one Bone, since it will always search for the root node to execute the code there. It will then automatically rotate every child Bone and places them at the right position. Finally, use the setAnglesToModels method to rotate each model and place them at the correct spot. Note that if you also apply custom rotation for the individual models that you should apply that after you've run setAnglesToModels, since this will override the settings the model originally had. The best way to solve this is to make a separate method to rotate the Bones.

The following would be an example of a biped with a skeleton. It takes ModelBiped as an example and extends it with a skeleton. First, we have the part that goes in the constructor.

 // First, the origin will be placed. This is where the rest is attached to.
 skeletonOrigin = new Bone(0, 0, 0, 0);
 
 // Next, the entire skeleton is built up.
 skeletonHead = new Bone(-3.141593F / 2, 0, 0, 0, skeletonOrigin);
 skeletonBody = new Bone(3.141593F / 2, 0, 0, 12, skeletonOrigin);
 skeletonShoulderRight = new Bone(0, -3.141593F / 2, 0, 5, skeletonOrigin);
 skeletonShoulderLeft = new Bone(0, 3.141593F / 2, 0, 5, skeletonOrigin);
 skeletonArmRight = new Bone(3.141593F / 2, 0, 0, 12, skeletonShoulderRight);
 skeletonArmLeft = new Bone(3.141593F / 2, 0, 0, 12, skeletonShoulderLeft);
 skeletonPelvisRight = new Bone(0, -3.141593F / 2, 0, 2, skeletonBody);
 skeletonPelvisLeft = new Bone(0, 3.141593F / 2, 0, 2, skeletonBody);
 skeletonLegRight = new Bone(3.141593F / 2, 0, 0, 12, skeletonPelvisRight);
 skeletonLegLeft = new Bone(3.141593F / 2, 0, 0, 12, skeletonPelvisLeft);
 
 // Finally, all models will be attached to the skeletons.
 skeletonHead.addModel(bipedHead);
 skeletonHead.addModel(bipedHeadwear);
 skeletonBody.addModel(bipedBody);
 skeletonArmRight.addModel(bipedRightArm);
 skeletonArmLeft.addModel(bipedLeftArm);
 skeletonLegRight.addModel(bipedRightLeg);
 skeletonLegLeft.addModel(bipedRightLeg);
 


After that, you could replace anything in the setRotationAngles method with the following code. It's not a complete code, but you'll get the basics.

 skeletonHead.relativeAngles.angleY = f3 / 57.29578F;
 skeletonHead.relativeAngles.angleX = f4 / 57.29578F;
 skeletonArmRight.relativeAngles.angleX = MathHelper.cos(f * 0.6662F + 3.141593F) * 2.0F * f1 * 0.5F;
 skeletonArmRight.relativeAngles.angleZ = 0.0F;
 skeletonArmLeft.relativeAngles.angleX = MathHelper.cos(f * 0.6662F) * 2.0F * f1 * 0.5F;
 skeletonArmLeft.relativeAngles.angleZ = 0.0F;
 skeletonLegRight.relativeAngles.angleX = MathHelper.cos(f * 0.6662F) * 1.4F * f1;
 skeletonLegRight.relativeAngles.angleY = 0.0F;
 skeletonLegLeft.relativeAngles.angleX = MathHelper.cos(f * 0.6662F + 3.141593F) * 1.4F * f1;
 skeletonLegLeft.relativeAngles.angleY = 0.0F;
 


Finally, in the render method, you could use the following code.

 setRotationAngles(f, f1, f2, f3, f4, f5);
 skeletonOrigin.prepareDraw();
 skeletonOrigin.setAnglesToModels();
 


This should generate the same animation of the regular biped. Don't forget to add the individual render methods for each model though, as it won't automatically render them.

Author:
GaryCXJk

Field Summary
 Angle3D relativeAngles
           
 
Constructor Summary
Bone(float x, float y, float z, float l)
          Constructor to create a bone.
Bone(float x, float y, float z, float l, Bone parent)
          Constructor to create a bone.
Bone(float xOrig, float yOrig, float zOrig, float xRot, float yRot, float zRot, float l)
          Constructor to create a bone.
 
Method Summary
 void addModel(net.minecraft.src.ModelRenderer model)
          Attaches a model to the bone.
 void addModel(net.minecraft.src.ModelRenderer model, boolean inherit)
          Attaches a model to the bone.
 void addModel(net.minecraft.src.ModelRenderer model, boolean inherit, boolean isUpright)
          Attaches a model to the bone.
 void addModel(net.minecraft.src.ModelRenderer model, float x, float y, float z)
          Attaches a model to the bone with a given base rotation.
 void addModel(net.minecraft.src.ModelRenderer model, float x, float y, float z, boolean inherit)
          Attaches a model to the bone with a given base rotation.
 void addModel(net.minecraft.src.ModelRenderer model, float x, float y, float z, boolean inherit, boolean isUpright)
          Attaches a model to the bone with a given base rotation.
 void attachBone(Bone parent)
          Attaches the bone to a parent.
 void detachBone()
          Detaches the bone from its parent.
 Angle3D getAbsoluteAngle()
          Gets the current absolute angles.
 net.minecraft.src.Vec3D getPosition()
          Gets the current position of the bone.
 Bone getRootParent()
          Gets the root parent bone.
 void prepareDraw()
          Prepares the bones for rendering.
 void removeModel(net.minecraft.src.ModelRenderer model)
          Removes the given model from the Bone.
 void resetOffset()
          Resets the offset.
 void resetOffset(boolean doRecursive)
          Resets the offset.
 void setAnglesToModels()
          Sets the current angles of the Bone to the models attached to it.
 void setNeutralRotation(float x, float y, float z)
          Sets the current neutral rotation of the bone.
 net.minecraft.src.Vec3D setOffset(float x, float y, float z)
          Sets the current offset of the parent root Bone.
 void setRotations(float x, float y, float z)
          Sets the current rotation of the Bone, not calculating any parent bones in.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

relativeAngles

public Angle3D relativeAngles
Constructor Detail

Bone

public Bone(float x,
            float y,
            float z,
            float l)
Constructor to create a bone.

Parameters:
x - the x-rotation of the bone
y - the y-rotation of the bone
z - the z-rotation of the bone
l - the length of the bone

Bone

public Bone(float xOrig,
            float yOrig,
            float zOrig,
            float xRot,
            float yRot,
            float zRot,
            float l)
Constructor to create a bone.

Parameters:
xOrig - the x-offset of the origin
yOrig - the y-offset of the origin
zOrig - the z-offset of the origin
xRot - the x-rotation of the bone
yRot - the y-rotation of the bone
zRot - the z-rotation of the bone
l - the length of the bone

Bone

public Bone(float x,
            float y,
            float z,
            float l,
            Bone parent)
Constructor to create a bone. This attaches the bone to a parent bone, and will calculate its current position relative to the origin.

Parameters:
x - the x-rotation of the bone
y - the y-rotation of the bone
z - the z-rotation of the bone
l - the length of the bone
parent - the parent Bone node this Bone is attached to
Method Detail

detachBone

public void detachBone()
Detaches the bone from its parent.


attachBone

public void attachBone(Bone parent)
Attaches the bone to a parent. If the parent is already set, detaches the bone from the previous parent.

Parameters:
parent - the parent Bone node this Bone is attached to

setOffset

public net.minecraft.src.Vec3D setOffset(float x,
                                         float y,
                                         float z)
Sets the current offset of the parent root Bone. Note that this will always set the parent root Bone, not the current Bone, as its offset is determined by the offset, rotation and length of its parent.

Parameters:
x - the x-position
y - the y-position
z - the z-position
Returns:
a Vec3D with the new coordinates of the current bone

resetOffset

public void resetOffset()
Resets the offset.


resetOffset

public void resetOffset(boolean doRecursive)
Resets the offset.

Parameters:
doRecursive -

setNeutralRotation

public void setNeutralRotation(float x,
                               float y,
                               float z)
Sets the current neutral rotation of the bone. This is the same rotation as in the constructor.

Parameters:
x - the x-rotation of the bone
y - the y-rotation of the bone
z - the z-rotation of the bone

getRootParent

public Bone getRootParent()
Gets the root parent bone.

Returns:
the root parent Bone.

addModel

public void addModel(net.minecraft.src.ModelRenderer model)
Attaches a model to the bone. Its base rotation will be set to the neutral rotation of the model.

Parameters:
model - the model to attach

addModel

public void addModel(net.minecraft.src.ModelRenderer model,
                     boolean inherit)
Attaches a model to the bone. If inherit is true, it sets the base rotation to the neutral rotation of the Bone, otherwise it's set to the neutral rotation of the model.

Parameters:
model - the model to attach
inherit - whether the model should inherit the Bone's base rotations

addModel

public void addModel(net.minecraft.src.ModelRenderer model,
                     boolean inherit,
                     boolean isUpright)
Attaches a model to the bone. If inherit is true, it sets the base rotation to the neutral rotation of the Bone, otherwise it's set to the neutral rotation of the model. When isUpright is set, the model will be rotated (-PI / 2, 0, 0).

Parameters:
model - the model to attach
inherit - whether the model should inherit the Bone's base rotations
isUpright - whether the model is modeled in the upright position

addModel

public void addModel(net.minecraft.src.ModelRenderer model,
                     float x,
                     float y,
                     float z)
Attaches a model to the bone with a given base rotation.

Parameters:
model - the model to attach
x - the base x-rotation
y - the base y-rotation
z - the base z-rotation

addModel

public void addModel(net.minecraft.src.ModelRenderer model,
                     float x,
                     float y,
                     float z,
                     boolean inherit)
Attaches a model to the bone with a given base rotation. When inherit is true, it will add the Bone's neutral rotation to the given angles.

Parameters:
model - the model to attach
x - the base x-rotation
y - the base y-rotation
z - the base z-rotation
inherit - whether the model should inherit the Bone's base rotations

addModel

public void addModel(net.minecraft.src.ModelRenderer model,
                     float x,
                     float y,
                     float z,
                     boolean inherit,
                     boolean isUpright)
Attaches a model to the bone with a given base rotation. When inherit is true, it will add the Bone's neutral rotation to the given angles. When isUpright is set, the model will be rotated (-PI / 2, 0, 0).

Parameters:
model - the model to attach
x - the base x-rotation
y - the base y-rotation
z - the base z-rotation
inherit - whether the model should inherit the Bone's base rotations
isUpright - whether the model is modeled in the upright position

removeModel

public void removeModel(net.minecraft.src.ModelRenderer model)
Removes the given model from the Bone. Always detach the model before adding it to another Bone. The best thing however is to just keep the model to one bone.

Parameters:
model - the model to remove from the bone

getAbsoluteAngle

public Angle3D getAbsoluteAngle()
Gets the current absolute angles. The absolute angle is calculated by getting the sum of all parent Bones' relative angles plus the current relative angle. This must be called after using the prepareDraw method.

Returns:
an Angle3D object which holds the current angles of the current node.

getPosition

public net.minecraft.src.Vec3D getPosition()
Gets the current position of the bone. You should call this after all rotations and positions are applied, e.g. after prepareDraw has been called.

Returns:
a vector containing the current position relative to the origin.

prepareDraw

public void prepareDraw()
Prepares the bones for rendering. This will automatically take the root Bone if it isn't.


setRotations

public void setRotations(float x,
                         float y,
                         float z)
Sets the current rotation of the Bone, not calculating any parent bones in.

Parameters:
x -
y -
z -

setAnglesToModels

public void setAnglesToModels()
Sets the current angles of the Bone to the models attached to it.