I’ve started to add collision with AABBs, I did this without camera rotation and it worked fine, but now when I added rotation, It broke. I’ve tried to add the x axis to the detection, but it either teleported me to the end of the level, or crashed the game. I have no idea how to add this and would like some help.
Here is my detection code for the forward direction (left, right, and backward is basically the same)
float dWithRotX = distance * (float) Math.sin(Math.toRadians(rotationX));
float dWithRotZ = distance * (float) Math.cos(Math.toRadians(rotationX));
for (int z = (int) (this.position.getZ()); z < (int) (this.position.getZ() + dWithRotZ + boundingDepth); z++)
{
// The maximum Z for this (poor) raycast.
int maxZ = (int) (this.position.getZ() + distance + boundingDepth) - 1;
// Get a bounding box at the current Z, could be null.
AxisAlignedBoundingBox potentialCollider = Planetary.getPlanetary().currentPlanet.getAABBAt(new Vector3f(Math.abs(this.position.getX()), this.position.getY(), z));
// Create a new bounding box for the camera at this position.
AxisAlignedBoundingBox potentialCameraCollider = new AxisAlignedBoundingBox(this, new Vector3f(this.position.getX(), this.position.getY(), z), boundingWidth, boundingHeight, boundingDepth);
// If the potential collider is not null.
if (potentialCollider != null)
{
// If the potential camera collider collides with the potential collider
if (potentialCameraCollider.intersects(potentialCollider))
{
// Break out of this raycast and don't move.
break;
}
// If we don't collide and we are at the last part of the raycast move the camera and break out of the raycast.
else if (z == maxZ)
{
position.x -= dWithRotX;
position.z += dWithRotZ;
break;
}
}
// If there is no collider at the raycast position, and we are at the last part of the raycast, move the camera and break out of the raycast.
else if (z == maxZ)
{
position.x -= dWithRotX;
position.z += dWithRotZ;
break;
}
}
And here is my AABB class
public class AxisAlignedBoundingBox
{
/**
* The parent of this AABB.
* */
public Object parent = null;
/**
* The minimum X of this collider.
* */
public float minX = 0.0f;
/**
* The minimum Y of this collider.
* */
public float minY = 0.0f;
/**
* The minimum Z of this collider.
* */
public float minZ = 0.0f;
/**
* The maximum X of this collider.
* */
public float maxX = 0.0f;
/**
* The maximum Y of this collider.
* */
public float maxY = 0.0f;
/**
* The maximum Z of this collider.
* */
public float maxZ = 0.0f;
/**
* The width of this collider.
* Stored for reference.
* */
private float width = 0.0f;
/**
* The height of this collider.
* Stored for reference.
* */
private float height = 0.0f;
/**
* The depth of this collider.
* Stored for reference.
* */
private float depth = 0.0f;
public AxisAlignedBoundingBox(Object parent, Vector3f position, float width, float height, float depth)
{
this.parent = parent;
this.minX = position.getX() - width;
this.minY = position.getY() - height;
this.minZ = position.getZ() - depth;
this.maxX = position.getX() + width;
this.maxY = position.getY() + height;
this.maxZ = position.getZ() + depth;
this.width = width;
this.height = height;
this.depth = depth;
}
/**
* Update this bounding box.
* */
public void update()
{
if (parent instanceof Camera)
{
Camera pCamera = (Camera) parent;
this.minX = pCamera.getPosition().getX() - width;
this.minY = pCamera.getPosition().getY() - height;
this.minZ = pCamera.getPosition().getZ() - depth;
this.maxX = pCamera.getPosition().getX() + width;
this.maxY = pCamera.getPosition().getY() + height;
this.maxZ = pCamera.getPosition().getZ() + depth;
}
}
/**
* Check if this Bounding Box intersects with the other bounding box.
* */
public boolean intersects(AxisAlignedBoundingBox boundingBox)
{
return (this.maxX >= boundingBox.minX) && (this.maxY >= boundingBox.minY) && (this.maxZ >= boundingBox.minZ) &&
(this.minX <= boundingBox.maxX) && (this.minY <= boundingBox.maxY) && (this.minZ <= boundingBox.maxZ);
}
}