Currently struggling with collision detection, I am making a game that extends SimpleGame. I want to prevent my character passing through other objects on the terrain.
I assume that I should be putting code in the simpleUpdate() method? I've been using:
if (player.getWorldBound().intersects(target.getWorldBound())){
System.out.printline("INTERSECTION DETECTED!!!");
}
I see the warning on the console, however I am unsure how to move the player back to where they have come from. I was thinking of (using a Vector3f instance variable, lastPosition):
if (player.getWorldBound().intersects(target.getWorldBound())){
System.out.printline("INTERSECTION DETECTED!!!");
protected static void adjustEntityPosition(final MoveableEntity p_entity, final BoundingVolume p_worldBound) {
final Vector3f l_extent = ((BoundingBox) p_worldBound).getExtent(ms_tmp);
final Vector3f l_center = p_worldBound.getCenter();
final CurrentBounds l_currentBounds = p_entity.getCurrentBounds();
final float l_minX = l_currentBounds.getCurrentMinX();
final float l_reducedExtentX = l_extent.x;
final float l_objMaxX = l_center.x + l_reducedExtentX;
final float l_maxX = l_currentBounds.getCurrentMaxX();
final float l_objMinX = l_center.x - l_reducedExtentX;
final float l_xDiff = evaluateDifference(l_minX, l_objMaxX, l_maxX, l_objMinX);
final float l_minZ = l_currentBounds.getCurrentMinZ();
final float l_reducedExtentZ = l_extent.z;
final float l_objMaxZ = l_center.z + l_reducedExtentZ;
final float l_maxZ = l_currentBounds.getCurrentMaxZ();
final float l_objMinZ = l_center.z - l_reducedExtentZ;
final float l_zDiff = evaluateDifference(l_minZ, l_objMaxZ, l_maxZ, l_objMinZ);
final float l_minY = l_currentBounds.getCurrentMinY();
final float l_objMaxY = l_center.y + l_extent.y;
final float l_maxY = l_currentBounds.getCurrentMaxY();
final float l_objMinY = l_center.y - l_extent.y;
final float l_yDiff = evaluateDifference(l_minY, l_objMaxY, l_maxY, l_objMinY);
final float l_absX = FastMath.abs(l_xDiff);
final float l_absY = FastMath.abs(l_yDiff);
final float l_absZ = FastMath.abs(l_zDiff);
if (l_absZ > l_absX && l_absY > l_absX) {
ms_tmp1.set(l_xDiff, 0.0F, 0.0F);
} else if (l_absZ < l_absY) {
ms_tmp1.set(0.0F, 0.0F, l_zDiff);
} else {
ms_tmp1.set(0.0F, l_yDiff, 0.0F);
}
p_entity.moveAbsoluteBy(ms_tmp1);
}
private static float evaluateDifference(
final float p_min,
final float p_objMax,
final float p_max,
final float p_objMin) {
final float l_diff1 = p_objMax - p_min;
final float l_diff2 = p_objMin - p_max;
final float l_ret;
if (FastMath.abs(l_diff1) > FastMath.abs(l_diff2)) {
l_ret = l_diff2;
} else {
l_ret = l_diff1;
}
return l_ret;
}
ms_tmp & ms_tmp1 are static vectors to avoid object creation. moveableentity is the baseclass for all movable objects in my engine, just use spatial and replace moveabsolute by getlocaltranslation().addlocal() and you'll get a nice and smooth bounding box collision.
by using the "reset to last valid postion"-method, you will get stuck whenever you touch something.
and also … if the object is not using boundingbox … so there will be exception in casting … is there a way to stop the player no matter what it collides?