{
float intersectTime = (box.x - origin.x) / motion.x;
float intersectY = origin.y + motion.y * intersectTime;
float intersectZ = origin.z + motion.z * intersectTime;
if(intersectY >= box.y && intersectY <= box.y + box.h && intersectZ >= box.z && intersectZ <= box.z + box.d)
return new DriveableHit(driveable, type, intersectTime);
}
else if(origin.x > box.x + box.w) //Check face x = o.x + d.x
{
float intersectTime = (box.x + box.w - origin.x) / motion.x;
float intersectY = origin.y + motion.y * intersectTime;
float intersectZ = origin.z + motion.z * intersectTime;
if(intersectY >= box.y && intersectY <= box.y + box.h && intersectZ >= box.z && intersectZ <= box.z + box.d)
return new DriveableHit(driveable, type, intersectTime);
}
}
//Z - axis and faces z = box.z and z = box.z + box.d
if(motion.z != 0F)
{
if(origin.z < box.z) //Check face z = box.z
{
float intersectTime = (box.z - origin.z) / motion.z;
float intersectX = origin.x + motion.x * intersectTime;
float intersectY = origin.y + motion.y * intersectTime;
if(intersectX >= box.x && intersectX <= box.x + box.w && intersectY >= box.y && intersectY <= box.y + box.h)
return new DriveableHit(driveable, type, intersectTime);
}
else if(origin.z > box.z + box.d) //Check face z = box.z + box.d
{
float intersectTime = (box.z + box.d - origin.z) / motion.z;
float intersectX = origin.x + motion.x * intersectTime;
float intersectY = origin.y + motion.y * intersectTime;
if(intersectX >= box.x && intersectX <= box.x + box.w && intersectY >= box.y && intersectY <= box.y + box.h)
return new DriveableHit(driveable, type, intersectTime);
}
}
//Y - axis and faces y = box.y and y = box.y + box.h
if(motion.y != 0F)
{
if(origin.y < box.y) //Check face y = o.y
{
float intersectTime = (box.y - origin.y) / motion.y;
float intersectX = origin.x + motion.x * intersectTime;
float intersectZ = origin.z + motion.z * intersectTime;
if(intersectX >= box.x && intersectX <= box.x + box.w && intersectZ >= box.z && intersectZ <= box.z + box.d)
return new DriveableHit(driveable, type, intersectTime);
}
else if(origin.y > box.y + box.h) //Check face x = box.y + box.h
{
float intersectTime = (box.y + box.h - origin.y) / motion.y;
float intersectX = origin.x + motion.x * intersectTime;
float intersectZ = origin.z + motion.z * intersectTime;
if(intersectX >= box.x && intersectX <= box.x + box.w && intersectZ >= box.z && intersectZ <= box.z + box.d)
return new DriveableHit(driveable, type, intersectTime);
}
}
return null;
}