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

2011/8/23

Create a 3D cube

Advenging Advenging

2011/8/23

#
I want create a 3d cube,wich you can rotate with w,a,s and d. But when the cube rotate disappearing some cube faces. See http://greenfootgallery.org/scenarios/3417 Thanks for your help. Advenging
public class ZeichenBlatt  extends Actor
{    
    int P2[][] = {{1,1,-1},{-1,1,-1},{-1,-1,-1},{1,-1,-1}};
    int[] dx = new int[4];
    int[] dy = new int[4];
    int P3[][] = {{1,1,1},{-1,1,1},{-1,-1,1},{1,-1,1}}; 
    int[] dx2 = new int[4];
    int[] dy2 = new int[4];
    int P4[][] = {{1,1,-1},{1,1,1},{-1,1,1},{-1,1,-1}}; 
    int[] dx3 = new int[4];
    int[] dy3 = new int[4];
    int P5[][] = {{1,1,-1},{1,1,1},{1,-1,1},{1,-1,-1}}; 
    int[] dx4 = new int[4];
    int[] dy4 = new int[4];
    int P6[][] = {{1,-1,-1},{1,-1,1},{-1,-1,1},{-1,-1,-1}};
    int[] dx5 = new int[4];
    int[] dy5 = new int[4];
    int P7[][] = {{1,-1,-1},{1,1,-1},{1,1,1},{1,-1,1}};
    int[] dx6 = new int[4];
    int[] dy6 = new int[4];
    double  e     = 100; 
    int     alpha = 0; 
    int     beta  = 0;
    double  a;    
    double  b;
    double  Mx = 320; 
    double  My = 240;
    public ZeichenBlatt() {
        GreenfootImage img = new GreenfootImage(640,480);
        setImage(img);
        img.setColor(Color.BLACK); 
        img.fill();
    }

    public void act() {
        GreenfootImage img = getImage();
        img.clear();
        // Mx=Mx+0.2;
        img.setColor(Color.BLACK); 
        img.fill();
        if (Greenfoot.isKeyDown("q") )
        {            
            e=e+0.2;
        }
        if (Greenfoot.isKeyDown("e") )
        {            
            e=e-0.2;
        }
        if (Greenfoot.isKeyDown("a") )
        {            
            beta=beta+1;
        }
        if (Greenfoot.isKeyDown("d") )
        {            
            beta=beta-1;
        }
        if (Greenfoot.isKeyDown("w") )
        {            
            alpha=alpha+1;
        }
        if (Greenfoot.isKeyDown("s") )
        {            
            alpha=alpha-1;
        }
        if (Greenfoot.isKeyDown("left") )
        {            
            Mx=Mx-2;
        }
        if (Greenfoot.isKeyDown("right") )
        {            
            Mx=Mx+2;
        }
        if (Greenfoot.isKeyDown("up") )
        {            
            My=My-2;
        }
        if (Greenfoot.isKeyDown("down") )
        {            
            My=My+2;
        }
       
        for(int i2=0; i2<4; i2++) {
            dx[i2] = toPx(P2[i2], alpha, beta);
            dy[i2] = toPy(P2[i2], alpha, beta);
        }
        for(int i3=0; i3<4; i3++) {
            dx2[i3] = toPx(P3[i3], alpha, beta);
            dy2[i3] = toPy(P3[i3], alpha, beta);
        }
        for(int i4=0; i4<4; i4++) {
            dx3[i4] = toPx(P4[i4], alpha, beta);
            dy3[i4] = toPy(P4[i4], alpha, beta);
        }
        for(int i5=0; i5<4; i5++) {
            dx4[i5] = toPx(P5[i5], alpha, beta);
            dy4[i5] = toPy(P5[i5], alpha, beta);
        }
        for(int i6=0; i6<4; i6++) {
            dx5[i6] = toPx(P6[i6], alpha, beta);
            dy5[i6] = toPy(P6[i6], alpha, beta);
        }
         for(int i7=0; i7<4; i7++) {
            dx6[i7] = toPx(P7[i7], alpha, beta);
            dy6[i7] = toPy(P7[i7], alpha, beta);
        }      
        img.setColor(Color.WHITE);   
        img.fillPolygon(dx,dy,4);
        img.setColor(Color.YELLOW);   
        img.fillPolygon(dx2,dy2,4);
        img.setColor(Color.RED);   
        img.fillPolygon(dx3,dy3,4);
        img.setColor(Color.GREEN);   
        img.fillPolygon(dx4,dy4,4);
        img.setColor(Color.ORANGE);   
        img.fillPolygon(dx5,dy5,4);
        img.setColor(Color.BLUE);   
        img.fillPolygon(dx6,dy6,4);
    } 

    public int toPx(int A[], int alpha, int beta) {
        double a = Math.toRadians(alpha);
        double b = Math.toRadians(beta);
        double x = Mx+e*A[1]*Math.cos(b)-e*A[0]*Math.sin(b);// MX verschiebt würfel um x achse 1. Produkt gibt würfel seine form   
        return (int)Math.round(x);
    }

    public int toPy(int A[], int alpha, int beta) {
        double a = Math.toRadians(alpha);
        double b = Math.toRadians(beta);
        double y = My-e*A[2]*Math.cos(a)+e*A[0]*Math.sin(a)*Math.cos(b)+e*A[1]*Math.sin(a)*Math.sin(b);
        return (int)Math.round(y);
    }

}
poink poink

2011/8/23

#
Hi advenging, please, why don't you answer my questions? Firstly I made the effort to ask you in German, this morning in English.
Builderboy2005 Builderboy2005

2011/8/23

#
The reason they are all 'disappearing' is because you are always drawing them in the same order, and some sides are getting covered up by others by accident. For example you always draw the White side first, which means all other sides are always going to be in front of it. There is unfortunately no easy way to fix this, your best bet is to sort the sides in the order from back to front and draw them in that order.
poink poink

2011/8/23

#
Nevermind! I found another interesting article on Building Primitives. Cheers!
Advenging Advenging

2011/8/27

#
@Builderboy Thx for the tip. I want do it in my scenario. @poink Im afraid that I dont responded but this website is not facebook... . Nobody is every day online and answer all posts... in his Scenarios.... . Advenging
poink poink

2011/8/27

#
Yeah well, time tells in time you know. Just don't be surprised if you get responses here while the relativity of your post might not be of any importance for you anymore (but in one way or another, might be for others).
You need to login to post a reply.