This will allow you to grab an appropriate height for the end height of projectiles, for instance.
You use this to automatically apply perfect graphic/projectile heights for anything.
Notice how the arrow lands exactly at the perfect spot for each NPC:
rats are right at the ground, imps in the middle, and men at the top
First we need to be able to determine model height per model:
Code:
public static int modelHeight(int modelID) {
ModelDefinition definition = forID(modelID);
if (definition == null) {
return -1;
}
return Math.max(Math.abs(definition.highestY), Math.abs(definition.lowestY));
}
To determine our highest/lowest vertex values, simply loop through your model vertices:
Code:
void calculateModelBounds() {
lowestX = 32767;
lowestY = 32767;
lowestZ = 32767;
highestX = -32768;
highestY = -32768;
highestZ = -32768;
for (int i = 0; i < vertexCount; i++) {
int x = xVertices[i];
int y = yVertices[i];
int z = zVertices[i];
if (x < lowestX) {
lowestX = x;
}
if (x > highestX) {
highestX = x;
}
if (y < lowestY) {
lowestY = y;
}
if (y > highestY) {
highestY = y;
}
if (z < lowestZ) {
lowestZ = z;
}
if (z > highestZ) {
highestZ = z;
}
}
}
For NPCs for example, you need to loop through all of their models:
Code:
int npcModelHeight = -1;
for (int modelID : npc.getDefinition().getModelIDs()) {
int modelHeight = ModelDefinition.modelHeight(modelID);
if (modelHeight > npcModelHeight) {
npcModelHeight = modelHeight;
}
}
Once you have your highest model height, we divide by 8 (>>> 3) to get the center of the NPC:
Code:
int centerHeight = npcModelHeight >>> 3;
Finally, to determine the height to use for graphic end heights, it's just 50% more than the center height.
Code:
int headHeight = centerHeight * 1.5;
I recommend clamping this so a minimum of 8 and maximum of 96 for use as projectile end height, like so:
Code:
Math.max(8, Math.min(96, headHeight))
You're also able to determine the absolute peak point of the NPC, which is just double the center:
Code:
int peakHeight = centerHeight * 2;
Hope you enjoyed!