I am still having a bit of an issue with this. It worked fine with my original test level (a box) , but I’ve since tested it with some slightly more complex levels. First of all some info - My levels are made up of non-convex sectors, which are made up of non-convex surfaces. I’m looping through each sector, then through each surface of the sector. If it passes the above calculation for every surface of a sector, then I know what sector the camera is in.
The problem is, it starts to return a negative cosine for some surfaces when it shouldn’t. For example, I’ve made a test level with 3 box sectors. It works fine for the first 2 sectors, but one surface of the third sector always returns a negative. Any thoughts?
public void getsector()
{
campos = new Vector3f(camera.posx, camera.posy, camera.posz);
for(int i=0; i<level.getworld_sectors(); i+=1)
{
int g = level.sector[i].surfaces[0];
Inner:
for(int s=0; s<level.sector[i].surfaces[1]; s+=1)
{
direction = Vector3f.sub(campos, level.surface[g+s].surfacep, null);
float cos = Vector3f.dot(level.surface[g+s].normal, direction);
if(cos < 0.0f)
{
break Inner;
}
if(cos > 0.0f)
{
if(s == level.sector[i].surfaces[1]-1)
{
System.out.println("Sector: "+i);
break;
}
}
}
}
}
[edit]
So I figured it out, it turns out my opengl translations were screwed up, so the camera wasn’t where I thought. So the above code works fine!