Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I recently created a 3d engine, and have gotten to what is (as I've found) the hardest part so far. I need to sort the objects and then draw them. I've looked at sorting algorithms, but my case is rather specialized. I have an array of vertices stored inside the Face3D object which I've created, and need to draw faces correctly when overlapping, Firstly, am I thinking about this correctly? Draw the back vertices first? Secondly, How would I sort this? Here's my code for the Face3D file:

package me.dylan.ThirdDimension.src;

import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.Polygon; import java.awt.Shape; import java.awt.geom.Point2D; import java.util.ArrayList;

public class Face3D { Polygon face=new Polygon(); ArrayList Lines = new ArrayList(); ArrayList Vertices = new ArrayList(); Color color; public Face3D(int x,int y, int z, int width, int height, int depth,Main m,Color c) { color=c; Vertices.add(new Point3D(x, y, z, m)); Vertices.add(new Point3D(x, y+height, z, m)); Vertices.add(new Point3D(x+width, y+height, z+depth, m)); Vertices.add(new Point3D(x+width, y, z+depth, m)); for(int i=0;i Vertices.get(i).update(); Point2D tmp = new Point2D.Double(); tmp.setLocation((int)Vertices.get(i).x2d, (int)Vertices.get(i).y2d); face.addPoint((int)Math.round(tmp.getX()),(int)Math.round(tmp.getY())); }

} public void paint(Graphics g) { g.setColor(color); Graphics2D g2d = (Graphics2D)g; face=new Polygon(); for(int i=0;i<Vertices.size();i++) { Vertices.get(i).update(); Point2D tmp = new Point2D.Double(); tmp.setLocation((int)Vertices.get(i).x2d, (int)Vertices.get(i).y2d); face.addPoint((int)tmp.getX(),(int)Math.round(tmp.getY())); } g2d.fillPolygon(face); }


I'd just like to apologize if my question is unclear, it's difficult for me to explain this.

share|improve this question
Are you using Face3D to draw a 3D shape (like a cube) and are wondering which faces to draw first (so that the back faces are not above the front faces)? –  MatthewRz Jan 7 '13 at 0:07
Yes, that's exactly what I'm trying to do, thanks. –  Dylan Katz Jan 7 '13 at 0:13
I would maybe try sorting them based on the highest depth of each Face of the 3D shape. If you have 6 Faces with 4 points in each, get the deepest point in each Face and sort the Faces by that factor. This should work because it draws the deeper Faces first, then the ones closer to you (hope this makes sense). –  MatthewRz Jan 7 '13 at 0:24
Thank you, worked perfectly. –  Dylan Katz Jan 7 '13 at 3:51
No problem, I actually had a problem similar to yours so I am glad it worked! –  MatthewRz Jan 7 '13 at 6:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.