I’m temporarily using this abomination of a function to do some collision detection, and it generates over a gigabyte of garbage each second. Due to this I get an average of 2-3 garbage collections per second, and most of them are taking more than 7ms. The funny thing is that the fantastically amazing Hotspot should be able to get rid of every single allocation of Vector3s thanks to escape analysis, but it’s not. For VM commands, I only use -server.
void solveCollision(Triangle triangle){
Vector3 center = new Vector3(position).add(0, height*0.5f, 0);
Vector3 v0 = triangle.transformV0(center, diameterOverHeight);
Vector3 v1 = triangle.transformV1(center, diameterOverHeight);
Vector3 v2 = triangle.transformV2(center, diameterOverHeight);
Vector3 point = closestPointTriangle(new Vector3(), v0, v1, v2);
float length = point.len();
mad(delta, point, -0.1f * Math.max(0, (wallCollisionRadius - length) / length));
//mad(position, point, -0.1f * Math.max(0, (wallCollisionRadius - length) / length));
//mad(velocity, point, -1f * Math.max(0, (wallCollisionRadius - length) / length));
}
public static Vector3 closestPointTriangle(Vector3 p, Vector3 a, Vector3 b, Vector3 c) {
// Optimized version as found in book Real time collision detection
// b - a
Vector3 ab = new Vector3(b).sub(a);
// c - a
Vector3 ac = new Vector3(c).sub(a);
// p - a
Vector3 ap = new Vector3(p).sub(a);
float d1 = ab.dot(ap);
float d2 = ac.dot(ap);
if (d1 <= 0.0f && d2 <= 0.0f) {
return a;
}
// p - b
Vector3 bp = new Vector3(p).sub(b);
float d3 = ab.dot(bp);
float d4 = ac.dot(bp);
if (d3 >= 0.0f && d4 <= d3) {
return b;
}
float vc = d1 * d4 - d3 * d2;
if (vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f) {
float v = d1 / (d1 - d3);
// a + (ab * v)
return mad(new Vector3(a), ab, v);
}
// p - c
Vector3 cp = new Vector3(p).sub(c);
float d5 = ab.dot(cp);
float d6 = ac.dot(cp);
if (d6 >= 0.0f && d5 <= d6) {
return c;
}
float vb = d5 * d2 - d1 * d6;
if (vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f) {
float w = d2 / (d2 - d6);
// a + (ac * w)
return mad(new Vector3(a), ac, w);
}
float va = d3 * d6 - d5 * d4;
if (va <= 0.0f && (d4 - d3) >= 0.0f && (d5 - d6) >= 0.0f) {
float w = (d4 - d3) / ((d4 - d3) + (d5 - d6));
// b + (c - b) * w
return mad(new Vector3(b), new Vector3(c).sub(b), w);
}
float denom = 1.0f / (va + vb + vc);
float vn = vb * denom;
float wn = vc * denom;
// a + ab * vn + ac * wn
// this can be done with one line
Vector3 abvn = new Vector3(ab).scl(vn);
Vector3 acwn = new Vector3(ac).scl(wn);
// return result
return new Vector3(a).add(abvn).add(acwn);
}