A little function for creating a Box between two points without getting inverted normals

Super simple. Twas having trouble with my cubes being drawn inside out (inverted normals) when using ‘public Box(Vector3f min, Vector3f max)’. Just swap this in and your set.



public static Box createBox(Vector3f vMin, Vector3f vMax) {

Vector3f vCenter = vMin.interpolate(vMax, 0.5f);



float extentX = Math.abs(vMax.x - vMin.x);

float extentY = Math.abs(vMax.y - vMin.y);

float extentZ = Math.abs(vMax.z - vMin.z);



return new Box(vCenter, extentX, extentY, extentZ);

}




(sorry if it’s basic, just looking at it from the perspective of somebody googling around like I was)

You might consider relabeling your parameters since they have nothing to do with “min” and “max” anymore.



I guess this is for the case when you don’t know which is min and which is max?



You could also do:

[java]

public static Box createBox(Vector3f v1, Vector3f v2) {

Vector3f min = new Vector3f(v1);

Vector3f max = new Vector3f(v2);

min.minLocal(v2);

max.maxLocal(v1);

return new Box(min, max);

}[/java]

1 Like

^ Excellent, thanks! That should probably be in the official code. Ie:



Current code:



public Box(Vector3f min, Vector3f max) {

super();

updateGeometry(min, max);

}




New:

public Box(Vector3f v1, Vector3f v2) {

super();

Vector3f min = new Vector3f(v1);

Vector3f max = new Vector3f(v2);

min.minLocal(v2);

max.maxLocal(v1);

updateGeometry(min, max);

}




I’m sure many people have been tripped up by this in the past.

I don’t think that many people get tripped up on it.



a) I think most use the center, extents form.

b) no real game will use JME’s box object except maybe during prototyping

c) most of the time, min and max is easily known ahead of time.



No reason to do extra work for the 95% of cases where min/max are correctly passed in, I guess.