collision detection with random objects

edited August 2017 in Python Mode

I am continuing my learning of how processing with python works. I have created a small window with a character/rectangle that moves around it using the arrow keys. I have also included a step counter and score at the bottom of the screen. All of this works well and I understand it.

The next thing I want to try is adding a random object to the window that will have collision detection PLUS a moving object like a bullet, that fires from one side to the other repeatedly.

BLOCK 1) I am going to create a random block (same size as the moving rectangle) and place it somewhere in the window 2) If the rectangle hits the block, I want collision detection and for it not to be able to move through it

BULLET 1) The bullet will be something small moving from left to right 2) If the bullet hits the moving rectangle, I want the rectangle to go back to the start.

Right now I have the random block placement, and I tried to make it so that it would be placed on the same coordinates as the rectangle (that is why I am using r1, r2 to round it to an even number). I am sure there is an easier way of doing it, but this works.

I do not have any collision detection yet. I thought I might have to add "OR" to the keyReleased conditionals:

if (keyCode == LEFT) and x1>=1 OR

But I am not sure if that would work. Here is the code so far. Any help is greatly appreciated it. And I do understand this isn't probably the most efficient way of doing things, but so far I do understand how I am doing it.

add_library("minim")

x1=100
x2=20
y1=0
y2=20
steps=0

def setup():
    global sf, steps, r1, r2
    size(200, 200)
    background(255) # white
    minim=Minim(this)
    sf=minim.loadFile("light ping.mp3", 2048)
    r1= int(random(5,155))
    r2 = int(r1/10) * 10
    print("This is the random number", r1)
    print("This is the random number", r2)


def draw():
    background(255)
    global x1,y1,x2,y2, steps
    fill(39,79,188)
    rect(r2,r2,x2,y2)

    fill(155)
    stroke(1)
    rect(x1, y1, x2, y2)
    noStroke()
    rect(0,0,5, height-25)
    rect(0,height-25,width, 5)
    rect(width-5, 0, 5, height-25)
    rect(0,0,width, 5)
    # score header
    text("SCORE", 10, height-5) 
    # steps counter
    text(steps, 60, height-5)



def keyReleased():
    global x1, y1, x2, y2, steps
    steps=steps+1
    rect(x1, y1, x2, y2)
    if (key==CODED):
        if (keyCode == LEFT) and x1>=1:
            x1=x1-10
            print(x1)
            rect(x1,y1,x2,y2)            

        elif (keyCode == RIGHT) and x1<=169:
            x1=x1+10
            print(x1)
            rect(x1,y1,x2,y2)

        elif (keyCode == UP) and y1>=1:
            y1=y1-10
            print(y1)
            rect(x1,y1,x2,y2)

        elif (keyCode == DOWN) and y1<=140:
            y1=y1+10
            print(y1)
            rect(x1,y1,x2,y2)

        else:
            fill(255,255,0)
            rect(x1,y1,x2,y2)
            sf.play()
            sf.rewind()

Answers

  • I have created the bullet moving from left to right, getting it to collide is challenging. Here is the new code :

    add_library("minim")
    
    diam=5
    x1=100
    x2=20
    y1=0
    y2=20
    x3=0
    y3=100
    steps=0
    
    def setup():
        global sf, steps, r1, r2, diam
        size(200, 200)
        background(255) # white
        minim=Minim(this)
        sf=minim.loadFile("light ping.mp3", 2048)
        r1= int(random(5,155))
        r2 = int(r1/10) * 10
        print("This is the random number", r1)
        print("This is the random number", r2)
    
    
    def draw():
        background(255)
        global x1,y1,x2,y2, steps, diam, x3, y3
        fill(39,79,188)
        rect(r2,r2,x2,y2)
        bullet()
        fill(155)
        stroke(1)
        rect(x1, y1, x2, y2)
        noStroke()
        rect(0,0,5, height-25)
        rect(0,height-25,width, 5)
        rect(width-5, 0, 5, height-25)
        rect(0,0,width, 5)
        # score header
        text("SCORE", 10, height-5) 
        # steps counter
        text(steps, 60, height-5)
    
    
    def bullet():
        global diam, x3, y3
        ellipse (x3,y3,diam,diam)
        x3=x3+1
        print("This is where the ellipse is ",x3)
        if x3==200:
            x3=0
        elif x3>=r2 and y3<=r2:
            x3=0
    
    def keyReleased():
        global x1, y1, x2, y2, steps
        steps=steps+1
        rect(x1, y1, x2, y2)
        if (key==CODED):
            if (keyCode == LEFT) and x1>=1:
                x1=x1-10
                print(x1)
                rect(x1,y1,x2,y2)            
    
            elif (keyCode == RIGHT) and x1<=169:
                x1=x1+10
                print(x1)
                rect(x1,y1,x2,y2)
    
            elif (keyCode == UP) and y1>=1:
                y1=y1-10
                print(y1)
                rect(x1,y1,x2,y2)
    
            elif (keyCode == DOWN) and y1<=140:
                y1=y1+10
                print(y1)
                rect(x1,y1,x2,y2)
    
            else:
                fill(255,255,0)
                rect(x1,y1,x2,y2)
                sf.play()
                sf.rewind()
    
  • Yeah I am having a hard time with the collision detection on the block. Any help??

  • Answer ✓

    Never mind, got it. I created a third variable r3=r2+20 -- which is the size of the side of the rectangle. So then I just had to set it up the conditional statements so that the y value was between r2 and r3.

    add_library("minim")
    
    diam=5
    
    
    x1=100
    x2=20
    y1=0
    y2=20
    x3=0
    y3=100
    steps=0
    
    def setup():
        global sf, steps, r1, r2, r3, diam
        size(200, 200)
        background(255) # white
        minim=Minim(this)
        sf=minim.loadFile("light ping.mp3", 2048)
        r1= int(random(5,155))
        r2 = int(r1/10) * 10
        r3=r2+20
        print("This is the random number", r1)
        print("This is the random number", r2)
    
    
    def draw():
        background(255)
        print(str(mouseX) + " : " + str(mouseY))
        global x1,y1,x2,y2, steps, diam, x3, y3, r3
        fill(39,79,188)
        #random block
        rect(r2,r2,x2,y2)
        #call bullet function
        bullet()
        fill(155)
        stroke(1)
        rect(x1, y1, x2, y2)
        noStroke()
        rect(0,0,5, height-25)
        rect(0,height-25,width, 5)
        rect(width-5, 0, 5, height-25)
        rect(0,0,width, 5)
        # score header
        text("SCORE", 10, height-5) 
        # steps counter
        text(steps, 60, height-5)
    
    
    def bullet():
        global diam, x3, y3, r3
        ellipse (x3,y3,diam,diam)
        x3=x3+1
        print("This is where the ellipse is ",x3)
        if x3==200:
            x3=0
        elif (x3==r2) and (y3>=r2) and (y3<=r3):
            x3=0
    
    def keyReleased():
        global x1, y1, x2, y2, steps
        steps=steps+1
        rect(x1, y1, x2, y2)
        if (key==CODED):
            if (keyCode == LEFT) and x1>=1:
                x1=x1-10
                print(x1)
                rect(x1,y1,x2,y2)            
    
            elif (keyCode == RIGHT) and x1<=169:
                x1=x1+10
                print(x1)
                rect(x1,y1,x2,y2)
    
            elif (keyCode == UP) and y1>=1:
                y1=y1-10
                print(y1)
                rect(x1,y1,x2,y2)
    
            elif (keyCode == DOWN) and y1<=140:
                y1=y1+10
                print(y1)
                rect(x1,y1,x2,y2)
    
            else:
                fill(255,255,0)
                rect(x1,y1,x2,y2)
                sf.play()
                sf.rewind()
    
Sign In or Register to comment.