Determine whether a vector intersects the up or down unit vector
You have four groups of repeating conditions:

bool crossX = (lastDiff.x() &lt; 0 &amp;&amp; diff.x() &gt; 0) || (lastDiff.x() &gt; 0 &amp;&amp; diff.x() &lt; 0);
bool crossZ = (lastDiff.z() &lt; 0 &amp;&amp; diff.z() &gt; 0) || (lastDiff.z() &gt; 0 &amp;&amp; diff.z() &lt; 0);
bool crossTouchX = (lastDiff.x() &lt;= 0 &amp;&amp; diff.x() &gt;= 0) || (lastDiff.x() &gt;= 0 &amp;&amp; diff.x() &lt;= 0);
bool crossTouchZ = (lastDiff.z() &lt;= 0 &amp;&amp; diff.z() &gt;= 0) || (lastDiff.z() &gt;= 0 &amp;&amp; diff.z() &lt;= 0);


Using these the original condition can be simplified to:

if ((crossX &amp;&amp; crossTouchZ) || (crossZ &amp;&amp; crossTouchX))
{
    // do stuff
}