Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So here's how to do it.</p> <p>First you need to store the eight points in each bounding box in a predictable manner, for instance, each bit in an integer representing its position about the center of the box. What you can do is use 0x4 to represent EAST (+x) (zero in that bit means WEST) 0x2 to represent NORTH (zero in that bit means SOUTH) and 0x1 to represent TOP (zero in that bit means BOTTOM). Now you can universally access all points in the same position of the bounding box by specifying a position number. (all zero bit numbers are just zero. Java warns that these assignments are meaningless, but they help with readability.)</p> <p>Setting up the bounding box: (do this for every bounding box)</p> <pre><code> pointList = new Point3d[8]; pointList[WEST|SOUTH|BOTTOM] = new Point3d(x0,y0,z0); pointList[WEST|SOUTH|TOP] = new Point3d(x0,y0,z1); pointList[WEST|NORTH|BOTTOM] = new Point3d(x0,y1,z0); pointList[WEST|NORTH|TOP] = new Point3d(x0,y1,z1); pointList[EAST|SOUTH|BOTTOM] = new Point3d(x1,y0,z0); pointList[EAST|SOUTH|TOP] = new Point3d(x1,y0,z1); pointList[EAST|NORTH|BOTTOM] = new Point3d(x1,y1,z0); pointList[EAST|NORTH|TOP] = new Point3d(x1,y1,z1); </code></pre> <p>Next, calculate the normal from your view angle. (Yaw is left/right rotation, pitch is up/down rotation)</p> <pre><code> float nx = -(float)Math.cos(yaw); float ny = (float)Math.sin(yaw); float nz = (float)Math.sin(pitch); </code></pre> <p>I think the negative is right, but if everything is invisible on one side, just reverse it :)</p> <p>Calculate the Characteristic Point, which is just the index that you're going to check in every bounding box because it represents the point in every box that will be nearest to the plane on the invisible side:</p> <pre><code> int characteristicPoint = (nx&lt;0?WEST:EAST)| (ny&lt;0?SOUTH:NORTH)| (nz&lt;0?BOTTOM:TOP); </code></pre> <p>Make sure that every bounding box is set up such that x,y,z aught are lesser than x,y,z prime (x0 = x aught, x1 = x prime) </p> <p>Then collect your characteristic ('check') point, your set of normals ('frustrum'), and your camera position (x,y,z) and do this with every bounding box:</p> <pre><code> float checkA = ((bounds.pointList[check].x-position.x)*frustrum.x) + ((bounds.pointList[check].y-position.y)*frustrum.y); float checkB = ((bounds.pointList[check].x-position.x)*frustrum.x) + ((bounds.pointList[check].z-position.z)*frustrum.z); if(checkB&gt;=0&amp;&amp;checkA&gt;=0) { visible = true; return; } else if(checkB&lt;0&amp;&amp;checkA&lt;0) { visible = false; return; } else { float checkC = ((bounds.pointList[check].y-position.y)*frustrum.y) + ((bounds.pointList[check].z-position.z)*frustrum.z); if(checkC&gt;=0) { visible = true; return; } else { visible = false; return; } } </code></pre> <p>This is simple linear algebra (IMO) and the logic is as follows: If the point is on the positive side of two or more of the lines representing the plane, it is visible. If it is on the negative side of tow or more of the lines representing the plane, it is invisible. You calculate the first two, and if they are different (one negative, one positive) you check the third and take that value.</p> <p>It is important to note the following property when treating lines as equations:</p> <pre><code>-x-y = p != x+y = p </code></pre> <p>The line is the same, but its implicit 'facing' is inverse.</p> <p>I hope this helps someone else with this question. It was hard, but enjoyable, to figure out.</p> <p>This could be made more efficient by storing the first half of CheckA, I suppose :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload