Issue with graphics, processing python

Hi all,
I have created a Rubik’s cube and it works really well, however when a piece turns, the piece also appears in its original position at the same time. The problem is I rotate it, then I draw it but for some reason it draws both the rotated and unrotated cube at the same time.
Any help with getting rid of this would be much appreciated
To see what I mean run code and press ‘f’

add_library('peasycam')
from peasy import PeasyCam

#This procedure is a procedure built into the platform which always runs once in the beginning of the program so that any setup can be done
def setup():
    #Creating the window
    size(800, 800, P3D)
    background(23, 117, 46)
    textSize(10)
    
    #Setting up to allow for the user to rotate the cube
    cam = PeasyCam(this, 100)
    cam.setMinimumDistance(105);
    cam.setMaximumDistance(105);
    




#This class is involved in beginning the program
class createCubes:
    def __init__(self):
        self.cubelist = []
    
    #This method calls the cube class 27 times to create 27 cubes
    def create(self):
        a = []
        b = []
        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)
                b.append(a)
                a = []
            self.cubelist.append(b)
            b = []
            
            



#This class creates the smaller individual cubes and stores all the information about them
class Cube:
    def __init__(self, x, y, z):
        
        #These attributes store the current position of each of the objects of cube
        self.x_curr = x
        self.y_curr = y
        self.z_curr = z
        
        #These attributes are the solved positions of the position of each of the objects of cube
        #They are private as they should never be accessed
        self.__x_set = x
        self.__y_set = y
        self.__z_set = z
        
        #List containing all of the faces of the individual cube
        self.faces = []
    
    #This method assigns 6 faces to each cube
    def getFaces(self):
        for index in range(0,6):
            newFace = Faces(index)
            newFace.getinformation()
            self.faces.append(newFace)
    
    #This method is called and it checks the colour of the face which is to be drawn next and then it changes the drawing colour to that colour
    def showColour(self, each):
        if each.colour == 'white':
            fill(251, 255, 255)
        elif each.colour == 'blue':
            fill(77, 77, 255)
        elif each.colour == 'green':
            fill(57, 255, 20)
        elif each.colour == 'orange':
            fill(255, 95, 31)
        elif each.colour == 'yellow':
            fill(255, 237, 39)
        elif each.colour == 'red':
            fill(255, 49, 49)
    


    #This method displays all of the small cubes
    def show(self, dir, axis, face, animating, angle):
        a = False
        #r is half of the side length
        r = 6
        
        #pushmatrix allows for each cube to be drawn individually
        pushMatrix()
        if animating == True:
            if axis == 'z':
                if face == self.z_curr:
                    rotateZ(angle*dir)
                    
                    
            elif axis == 'x':
                if face == self.x_curr:
                    rotateX(angle*dir)

            
            elif axis == 'y':
                if face == self.y_curr:
                    rotateY(angle*dir)
        
        
        #translate moves the cube to the postition of its centre to be drawn
        
        

        translate(self.x_curr * 2 * r, self.y_curr * 2 * r, self.z_curr * 2 * r)
        
        
        #The way this program draws each cube is face by face to deal with colouring each face. Each face is drawn with a square, who's 4 vertices are defined in terms of r as below
        
        beginShape(QUADS)
        
        for each in self.faces:
            if each.pos_curr == [0, 0, 1]:
                self.showColour(each)
                
                vertex(r, r, r)
                vertex(r, -r, r)
                vertex(-r, -r, r)
                vertex(-r, r, r)
            
            elif each.pos_curr == [0, -1, 0]:
                self.showColour(each)
                
                vertex(r, r, r)
                vertex(r, r, -r)
                vertex(-r, r, -r)
                vertex(-r, r, r)
            
            elif each.pos_curr == [1, 0, 0]:
                self.showColour(each)
                
                vertex(r, r, r)
                vertex(r, r, -r)
                vertex(r, -r, -r)
                vertex(r, -r, r)
            
            elif each.pos_curr == [0, 0, -1]:
                self.showColour(each)
                
                vertex(r, r, -r)
                vertex(r, -r, -r)
                vertex(-r, -r, -r)
                vertex(-r, r, -r)
                
            elif each.pos_curr == [0, 1, 0]:
                self.showColour(each)
                
                vertex(r, -r, r)
                vertex(r, -r, -r)
                vertex(-r, -r, -r)
                vertex(-r, -r, r)
            
            elif each.pos_curr == [-1, 0, 0]:
                self.showColour(each)
                
                vertex(-r, r, r)
                vertex(-r, r, -r)
                vertex(-r, -r, -r)
                vertex(-r, -r, r)
        
        endShape()
        popMatrix()





#This class is a class for each of the faces of the individual cubes
class Faces:
    def __init__(self, index):
        #Each face will have a set number ie 1, and this will correspond to its colour
        self.faceNumber = index
        
        #Each face will have two vectors to show which way it is currently facing and which way it will be facing when solved
        self.pos_curr = [] 
        self.__pos_set = []
        
        #Each face will have a colour
        self.colour = ''
    
    #Assigning all of the information depending on which face it is
    def getinformation(self):
        if self.faceNumber == 0:
            self.colour = 'white'
            self.pos_curr = [0, 1, 0]
            self.__pos_set = [0, 1, 0]
            
        elif self.faceNumber == 1:
            self.colour = 'blue'
            self.pos_curr = [0, 0, 1]
            self.__pos_set = [0, 0, 1]
            
        elif self.faceNumber == 2:
            self.colour = 'orange'
            self.pos_curr = [1, 0, 0]
            self.__pos_set = [1, 0, 0]
            
        elif self.faceNumber == 3:
            self.colour = 'green'
            self.pos_curr = [0, 0, -1]
            self.__pos_set = [0, 0, -1]
            
        elif self.faceNumber == 4:
            self.colour = 'red'
            self.pos_curr = [-1, 0, 0]
            self.__pos_set = [-1, 0, 0]
            
        elif self.faceNumber == 5:
            self.colour = 'yellow'
            self.pos_curr = [0, -1, 0]
            self.__pos_set = [0, -1, 0]





class Moves:
    def __init__(self, cubelist):
        self.face = ''
        self.axis = ''
        self.rotation = ''
        self.matrix = []
        self.animating = False
        self.angle = 0
        self.amount = 0.01
        self.cubelist = cubelist
    
    def getmove(self, move):
        self.angle = 0
        #moves: 
        #u upper clockwise, y upper anticlockwise
        #d lower clockwise, c lower anticlockwise
        #f front clockwise, g front anticlockwise
        #b back clockwise, v upper anticlockwise
        #l left clockwise, k upper anticlockwise
        #r right clockwise, t upper anticlockwise
        #m middle x clockwise, n upper anticlockwise
        #s middle z clockwise, a upper anticlockwise
        #e middle y clockwise, w upper anticlockwise
        if move == 'u':
            self.axis = 'y'
            self.face = '1'
            self.rotation = '-1'
            
        elif move == 'y':
            self.axis = 'y'
            self.face = '1'
            self.rotation = '-1'
            
        elif move == 'd':
            self.axis = 'y'
            self.face = '-1'
            self.rotation = '-1'
            
        elif move == 'c':
            self.axis = 'y'
            self.face = '-1'
            self.rotation = '1'
            
        elif move == 'e':
            self.axis = 'y'
            self.face = '0'
            self.rotation = '-1'
            
        elif move == 'w':
            self.axis = 'y'
            self.face = '0'
            self.rotation = '1'
        
        
        
        elif move == 'f':
            self.axis = 'z'
            self.face = '1'
            self.rotation = '1'
            
        elif move == 'g':
            self.axis = 'z'
            self.face = '1'
            self.rotation = '-1'
            
        elif move == 'b':
            self.axis = 'z'
            self.face = '-1'
            self.rotation = '-1'
            
        elif move == 'v':
            self.axis = 'z'
            self.face = '-1'
            self.rotation = '1'
            
        elif move == 's':
            self.axis = 'z'
            self.face = '0'
            self.rotation = '-1'
            
        elif move == 'a':
            self.axis = 'z'
            self.face = '0'
            self.rotation = '1'
        
        
        
        elif move == 'l':
            self.axis = 'x'
            self.face = '-1'
            self.rotation = '-1'
            
        elif move == 'k':
            self.axis = 'x'
            self.face = '-1'
            self.rotation = '1'
            
        elif move == 'r':
            self.axis = 'x'
            self.face = '1'
            self.rotation = '-1'
            
        elif move == 't':
            self.axis = 'x'
            self.face = '1'
            self.rotation = '1'
            
        elif move == 'm':
            self.axis = 'x'
            self.face = '0'
            self.rotation = '-1'
            
        elif move == 'n':
            self.axis = 'x'
            self.face = '0'
            self.rotation = '1'
        
        dir = int(self.rotation)
        if self.axis == 'x': 
            self.matrix = [[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.animating = True
            
        elif self.axis == 'y':
            self.matrix = [[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.animating = True
            
        elif self.axis == 'z':
            self.matrix = [[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.animating = True
        self.animatings()
        
    def animatings(self):
        if self.angle < HALF_PI:
            background(79, 117, 110)
            self.angle += self.amount
            for i in self.cubelist:
                for each in i:
                    for n in each:
                        n.show(int(self.rotation), self.axis, int(self.face), self.animating, self.angle)
        
        else:
            for i in self.cubelist:
                for each in i:
                    for n in each:
                        if self.axis == 'z':
                            check = n.z_curr
                        elif self.axis == 'y':
                            check = n.y_curr
                        elif self.axis == 'x':
                            check = n.x_curr
                        if check == int(self.face):
                            newx = self.matrix[0][0]*n.x_curr + self.matrix[0][1]*n.y_curr + self.matrix[0][2]*n.z_curr
                            newy = self.matrix[1][0]*n.x_curr + self.matrix[1][1]*n.y_curr + self.matrix[1][2]*n.z_curr
                            newz = self.matrix[2][0]*n.x_curr + self.matrix[2][1]*n.y_curr + self.matrix[2][2]*n.z_curr
                            n.x_curr = newx
                            n.y_curr = newy
                            n.z_curr = newz
                            print('hi')
                            for b in n.faces:
                                if self.axis == 'z':
                                    x = -self.matrix[0][0]*b.pos_curr[0] + -self.matrix[0][1]*b.pos_curr[1] + -self.matrix[0][2]*b.pos_curr[2]
                                    y = -self.matrix[1][0]*b.pos_curr[0] + -self.matrix[1][1]*b.pos_curr[1] + -self.matrix[1][2]*b.pos_curr[2]
                                    z = self.matrix[2][0]*b.pos_curr[0] + self.matrix[2][1]*b.pos_curr[1] + self.matrix[2][2]*b.pos_curr[2]
                                elif self.axis == 'x':
                                    x = self.matrix[0][0]*b.pos_curr[0] + self.matrix[0][1]*b.pos_curr[1] + self.matrix[0][2]*b.pos_curr[2]
                                    y = -self.matrix[1][0]*b.pos_curr[0] + -self.matrix[1][1]*b.pos_curr[1] + -self.matrix[1][2]*b.pos_curr[2]
                                    z = -self.matrix[2][0]*b.pos_curr[0] + -self.matrix[2][1]*b.pos_curr[1] + -self.matrix[2][2]*b.pos_curr[2]
                                elif self.face == 'y':
                                    x = self.matrix[0][0]*b.pos_curr[0] + self.matrix[0][1]*b.pos_curr[1] + self.matrix[0][2]*b.pos_curr[2]
                                    y = self.matrix[1][0]*b.pos_curr[0] + self.matrix[1][1]*b.pos_curr[1] + self.matrix[1][2]*b.pos_curr[2]
                                    z = self.matrix[2][0]*b.pos_curr[0] + self.matrix[2][1]*b.pos_curr[1] + self.matrix[2][2]*b.pos_curr[2]
                                print(b.pos_curr)
                                newVector = [x, y, z]
                                print(newVector)
                                print('---------')
                                b.pos_curr = newVector
            self.animating = False





begin = createCubes()
begin.create()
move = Moves(begin.cubelist)


#The draw procedure is another inbuilt subroutine. This one, however is called 30 times a second.
def draw():
    background(79, 117, 110)
    if keyPressed:
        if key == 'f':
            move.getmove('f')
            delay(800)
        elif key == 'g':
            move.getmove('g')
            delay(800)
        elif key == 'r':
            move.getmove('r')
            delay(800)
    
    if move.animating == True:
        move.animatings()
    
    #This small section of code just calls for all of the cubes to be drawn again each time the draw procedure is called
    for i in begin.cubelist:
        for each in i:
            for n in each:
                n.show('', '', '', '', False)

    
    
1 Like

I found the error, I was displaying the cube twice, once rotated and once not

1 Like