This site requires JavaScript, please enable it in your browser!
Greenfoot back
wabuilderman
wabuilderman wrote ...

2014/5/20

3d Cube problem

wabuilderman wabuilderman

2014/5/20

#
I am a fairly advanced programmer, and have gone on and done unity/3d design (hence the lengthy period of my abscence). Here is the problem: I have a array (sorted by the members z coordinates), and use that to determine the draw order. it seems to work fine, other than I get this wierd anomaly while moving the cube around. Its not an error message, but it just looks wierd. Here is the line in question:
public GreenfootImage DrawSolid(GreenfootImage ImageToRender, ArrayList<Face> faces)
    {
        int i = 0;
        while (i < faces.size())
        {
           //if visible (lower z = closest to camera)
           // A "Face" has several variable in it, already assgined
           // I need to Sort them by this very specific means: the AverageZ floating point variable
           ArrayListTools tools = new ArrayListTools();
           faces = tools.SortFaceByVariable(faces);
           ImageToRender = drawFace(faces.get(i).getV1(), faces.get(i).getV2(), faces.get(i).getV3(), ImageToRender, faces.get(i).getCol());
           i++;
        }
        return ImageToRender;
    }
here is the link to the game: http://www.greenfoot.org/scenarios/11505
wabuilderman wabuilderman

2014/5/20

#
Please also tell me if there is any obvious problems in my code. I am a messy coder.
danpost danpost

2014/5/20

#
It appears, though I have not looked at the code, that the rendering of the sides of the cube are getting incorrect coordinates for the corners of where its face is drawn.
wabuilderman wabuilderman

2014/5/20

#
Hmm, can you look at the code? I don't think that that is it.
wabuilderman wabuilderman

2014/5/20

#
I think it has more to do with how I am getting the "AverageZ" coordinate, found in the Face class. I will post the relevent code here:
public Face(Vertice vert1,Vertice vert2,Vertice vert3, Color col)
    {
        v1 = vert1;
        v2 = vert2;
        v3 = vert3;
        color = col;
        AverageX = (v1.getX() + v2.getX() + v3.getX())/ 3;
        AverageY = (v1.getY() + v2.getY() + v3.getY())/ 3;
        AverageZ = (v1.getZ() + v2.getZ() + v3.getZ())/ 3;
    }
Also, here is how the faces are organized in Z order:
public ArrayList SortFaceByVariable(ArrayList<Face> array)
   {
      int i = 0;
      while(i < array.size())
      {
          boolean shouldRepeat = true;
          while(shouldRepeat)
          {
              if(i<array.size()-1)
              {
                  if(-array.get(i).getAverageZ() > -array.get(i + 1).getAverageZ())
                  {
                      Face tempFace1 = array.get(i);
                      Face tempFace2 = array.get(i + 1);
                      array.set(i,tempFace2);
                      array.set(i+1,tempFace1);
                      if(i<array.size()-1)
                      {
                         i++;
                      }
                      else
                      {
                          shouldRepeat = false;
                          i = 0;
                      }
                  }
                  
                  else
                  {
                      shouldRepeat = false;
                  }
              }
              else
              {
                  shouldRepeat = false;
              }
          }
          i++;
      }
       return array;
   }
danpost danpost

2014/5/20

#
I am not so sure that just sorting by average z is enough (or even correct in itself). I believe you need to sort by closest distance to viewer taking into account all three dimensional axes. But even that may not be enough, especially if you are averaging the values of the vertices. A large panel may have an average that puts it way behind a smaller one and it could still be obstructing the smaller ones view. Unfortunately, I myself would not be able to give you a sufficient solution to the problem. Though, logic tells me (and there may be a better way -- as I am not that knowledgeable on this subject) that the sort needs to take into account whether two faces will intersect (from the viewer's perspective), and if they do, comparing the distances of points on both faces along the same line of sight should give you the paint order between the two faces.
wabuilderman wabuilderman

2014/5/24

#
umm... Not that I don't understand anything you said, but I have no clue how I would do that. It sounds like raycasting, And I haven't the slightest clue how that works
wabuilderman wabuilderman

2014/5/24

#
Can someone please show me how I might go about raycasting?
You need to login to post a reply.