3D graphics not working python

Hi I’m creating a Rubik’s cube simulator and I’m really confused because I have programmed it to create a 3 by 3 and it is for some reason creating a 4 by 4.
I have tried making a 2 by 2 and it makes a 3 by 3 but only i can only work with two faces
Any help much appreciated
Here is my code

add_library('peasycam')
from peasy import PeasyCam

import math

def setup():
    size(800, 800, P3D)
    cam = PeasyCam(this, 100)
    cam.setMinimumDistance(50)
    cam.setMaximumDistance(500)
    background(51)

#faces class

class Face:
    def __init__(self, index):
        self.index = index
        
    def getcolour(self):
        if self.index == 0:
            self.colour = 'white'
            self.pos_curr = [0,1,0]
            self.pos_set = [0,1,0]
            
        elif self.index == 1:
            self.colour = 'blue'
            self.pos_curr = [0,0,1]
            self.pos_set = [0,0,1]
            
        elif self.index == 2:
            self.colour = 'orange'
            self.pos_curr = [1,0,0]
            self.pos_set = [1,0,0]
            
        elif self.index == 3:
            self.colour = 'green'
            self.pos_curr = [0,0,-1]
            self.pos_set = [0,0,-1]
            
        elif self.index == 4:
            self.colour = 'red'
            self.pos_curr = [-1,0,0]
            self.pos_set = [-1,0,0]
            
        elif self.index == 5:
            self.colour = 'yellow'
            self.pos_curr = [0,-1,0]
            self.pos_set = [0,-1,0]
            
    def check(self):
        if self.pos_curr == self.pos_set:
            return True
        else:
            return False
        
        

#cube class

class Cube:
    def __init__(self, x, y, z):
        self.faces = []

        #current positions of each cube -2<x<2 
        self.x_curr = x
        self.y_curr = y
        self.z_curr = z
        
        #the solved positition of the cube
        self._xset = x
        self._yset = y
        self._zset = z
    
    def getfaces(self):
        for i in range (0,6):
            newFace = Face(i)
            newFace.getcolour()
            self.faces.append(newFace)
    
    def show(self):
        len_= 10
        r=10
        stroke(0)
        strokeWeight(1)
        pushMatrix()
        
        translate(self.x_curr*10,self.y_curr*10,self.z_curr*10)
        beginShape(QUADS)
        
        #front face
        for each in self.faces:
            if each.pos_curr == [0,0,1]:
                if each.colour == 'white':
                    fill(255,255,255)
                elif each.colour == 'blue':
                    fill(0,0,255)
                elif each.colour == 'green':
                    fill(0,255,0)
                elif each.colour == 'orange':
                    fill(255,69,0)
                elif each.colour == 'yellow':
                    fill(255,255,0)
                elif each.colour == 'red':
                    fill(255,0,0)
        vertex(r, r, r)
        vertex(r, -r, r)
        vertex(-r, -r, r)
        vertex(-r, r, r)

        #bottom face
        for each in self.faces:
            if each.pos_curr == [0,-1,0]:
                if each.colour == 'white':
                    fill(255,255,255)
                elif each.colour == 'blue':
                    fill(0,0,255)
                elif each.colour == 'green':
                    fill(0,255,0)
                elif each.colour == 'orange':
                    fill(255,69,0)
                elif each.colour == 'yellow':
                    fill(255,255,0)
                elif each.colour == 'red':
                    fill(255,0,0)
        vertex(r, r, r)
        vertex(r, r, -r)
        vertex(-r, r, -r)
        vertex(-r, r, r)
        
        #right face
        for each in self.faces:
            if each.pos_curr == [1,0,0]:
                if each.colour == 'white':
                    fill(255,255,255)
                elif each.colour == 'blue':
                    fill(0,0,255)
                elif each.colour == 'green':
                    fill(0,255,0)
                elif each.colour == 'orange':
                    fill(255,69,0)
                elif each.colour == 'yellow':
                    fill(255,255,0)
                elif each.colour == 'red':
                    fill(255,0,0)
        vertex(r, r, r)
        vertex(r, r, -r)
        vertex(r, -r, -r)
        vertex(r, -r, r)

        #back face
        for each in self.faces:
            if each.pos_curr == [0,0,-1]:
                if each.colour == 'white':
                    fill(255,255,255)
                elif each.colour == 'blue':
                    fill(0,0,255)
                elif each.colour == 'green':
                    fill(0,255,0)
                elif each.colour == 'orange':
                    fill(255,69,0)
                elif each.colour == 'yellow':
                    fill(255,255,0)
                elif each.colour == 'red':
                    fill(255,0,0)
        vertex(r, r, -r)
        vertex(r, -r, -r)
        vertex(-r, -r, -r)
        vertex(-r, r, -r)

        #up face
        for each in self.faces:
            if each.pos_curr == [0,1,0]:
                if each.colour == 'white':
                    fill(255,255,255)
                elif each.colour == 'blue':
                    fill(0,0,255)
                elif each.colour == 'green':
                    fill(0,255,0)
                elif each.colour == 'orange':
                    fill(255,69,0)
                elif each.colour == 'yellow':
                    fill(255,255,0)
                elif each.colour == 'red':
                    fill(255,0,0)
        vertex(r, -r, r)
        vertex(r, -r, -r)
        vertex(-r, -r, -r)
        vertex(-r, -r, r)

        #left face
        for each in self.faces:
            if each.pos_curr == [-1,0,0]:
                if each.colour == 'white':
                    fill(255,255,255)
                elif each.colour == 'blue':
                    fill(0,0,255)
                elif each.colour == 'green':
                    fill(0,255,0)
                elif each.colour == 'orange':
                    fill(255,69,0)
                elif each.colour == 'yellow':
                    fill(255,255,0)
                elif each.colour == 'red':
                    fill(255,0,0)
        vertex(-r, r, r)
        vertex(-r, r, -r)
        vertex(-r, -r, -r)
        vertex(-r, -r, r)

        endShape()
        popMatrix()
        
#Moves class

class Moves:
    def __init__(self):
        self.yes=True

    def create(self):
        a=[]
        b=[]
        self.cubeList = []
        for x in range(-1,2):
            for y in range(-1,2):
                for z in range(-1,2):
                    newCube = Cube(x, y, z)
                    newCube.getfaces()
                    a.append(newCube)
                    print(len(a))
                b.append(a)
                a=[]
            self.cubeList.append(b)
            b=[]
        

    def applyMatrix(self, rotation, matrix3D, face):
        for a in self.cubeList:
            for b in a:
                for i in b:
                    if face == 'z':
                        check = i.z_curr
                    elif face == 'y':
                        check = i.y_curr
                    elif face == 'x':
                        check = i.x_curr
                    if check == rotation:
                        #altering position of each cube
                        newx = matrix3D[0][0]*i.x_curr + matrix3D[0][1]*i.y_curr + matrix3D[0][2]*i.z_curr
                        newy = matrix3D[1][0]*i.x_curr + matrix3D[1][1]*i.y_curr + matrix3D[1][2]*i.z_curr
                        newz = matrix3D[2][0]*i.x_curr + matrix3D[2][1]*i.y_curr + matrix3D[2][2]*i.z_curr
                        i.x_curr = newx
                        i.y_curr = newy
                        i.z_curr = newz

                        #altering the rotation of each cube
                        for each in i.faces:
                            a = matrix3D[0][0]*each.pos_curr[0] + matrix3D[0][1]*each.pos_curr[1] + matrix3D[0][2]*each.pos_curr[2]
                            b = matrix3D[1][0]*each.pos_curr[0] + matrix3D[1][1]*each.pos_curr[1] + matrix3D[1][2]*each.pos_curr[2]
                            c = matrix3D[2][0]*each.pos_curr[0] + matrix3D[2][1]*each.pos_curr[1] + matrix3D[2][2]*each.pos_curr[2]
                            newVector = [a,b,c]
                            each.pos_curr = newVector
                            

    def turnZ(self):
        #for z rotations
        #user = input('f, m, b, F, M or B')
        user='f'
        if user == 'f':
            rotation = 1
            dir = -1
        elif user == 'F':
            rotation = 1
            dir = 1
        
        elif user == 'm':
            rotation = 0
            dir = -1
        elif user == 'M':
            rotation =0
            dir = 1

        elif user == 'b':
            rotation = -1
            dir = -1
        elif user == 'B':
            rotation =-1
            dir = 1
        matrix3D = [[round(cos(dir*HALF_PI)),-round(sin(dir*HALF_PI)),0],[round(sin(dir*HALF_PI)),round(cos(dir*HALF_PI)),0],[0,0,1]]    
        self.applyMatrix(rotation, matrix3D, 'z')

    def turnX(self):
        #for x rotations
        user = input('l, m, r, L, M or R')

        if user == 'r':
            rotation = 1
            dir = -1
        elif user == 'R':
            rotation = 1
            dir = 1
        
        elif user == 'm':
            rotation = 0
            dir = -1
        elif user == 'M':
            rotation =0
            dir = 1

        elif user == 'l':
            rotation = -1
            dir = -1
        elif user == 'L':
            rotation =-1
            dir = 1

        matrix3D = [[1,0,0],[0,round(cos(dir*HALF_PI)),-round(sin(dir*HALF_PI))],[0,round(sin(dir*HALF_PI)),round(cos(dir*HALF_PI))]]    
        self.applyMatrix(rotation, matrix3D, 'x')
        
    def turnY(self):
        #for y rotations
        user = input('d, m, u, D, M or U')

        if user == 'u':
            rotation = 1
            dir = -1
        elif user == 'U':
            rotation = 1
            dir = 1
        
        elif user == 'm':
            rotation = 0
            dir = -1
        elif user == 'M':
            rotation =0
            dir = 1

        elif user == 'd':
            rotation = -1
            dir = -1
        elif user == 'D':
            rotation =-1
            dir = 1

        matrix3D = [[round(cos(dir*HALF_PI)), 0, round(sin(dir*HALF_PI))], [0, 1, 0], [-round(sin(dir*HALF_PI)), 0, round(cos(dir*HALF_PI))]]    
        self.applyMatrix(rotation, matrix3D, 'y')

    def axis(self):
        #a = input('Would you like to rotate along x, y or z axis? ')
        a='z'
        if a == 'x':
            self.turnX()
        elif a =='y':
            self.turnY()
        else:
            self.turnZ()

a=Moves()
a.create()
a.axis()
def draw():
    background(51)
    counter=0
    for i in a.cubeList:
        for each in i:
            for z in each:
                z.show()
                counter+=1
    print(counter)```

@Daniel11 Hey mate, quick fix, the 3 for loops going over each face on line 223, 224, and 225 are going from range -1,2 make them from -1,1 and it works fine, i think range was including the 2 so was doing 4 in total, i tested it and works fine with that change

Hi, I tried that and it does create a 3 by 3. However, it only creates 8 small cubes and the axis only go from -1 to 0 so I can’t do rotations using it. And i tested it and the for loop was only going -1, 0. Im really so confused

hmm, ill keep looking then

1 Like

I was doing some experimentation just in the 2D:

add_library('peasycam')
from peasy import PeasyCam
def setup():
    size(600,600,P3D)
    cam = PeasyCam(this, 100)
    cam.setMinimumDistance(50)
    cam.setMaximumDistance(500)
    
def draw():
    background(51)
    for x in range(0,3):
        for y in range(0,3):
            r=10
            stroke(0)
            strokeWeight(1)
            pushMatrix()
            
            translate(x+10,y+10)
            beginShape(QUADS)
            vertex(r, r)
            vertex(r, -r)
            vertex(-r, -r)
            vertex(-r, r)
            endShape()
            popMatrix()

and it gave something really weird:
but i experimented with a constant in the translate function
and the larger the constant the closer it gets to a 4 by 4 untill it gets the constant 10 and then at 20 it creates a 3 by 3. Im so confused but it seems to work. Thank you for your help

1 Like

Was just going to point that out, on line 87, it seems maybe the cubes are overlapping, thats why its acting that way i suspect, if you make them 25 they separate completely

Just got it, change the value r on line 82 to r = 5 that saves playing with the translate

3 Likes