Help needed- asteroids collision detection

I am working on a school assignment where we are making “Asteroids” in processing.py
My school uses a slightly modified version of processing.py: for example, mouseX is mouse.x. I need help with my collision detection with the asteroids, which is off


class A:
    def __init__(self, x, y, r, t, vel_x, vel_y):
        self.cor, self.r, self.t, self.o, self.vel, self.f = PVector(x, y), r, t, [], PVector(vel_x, vel_y), False
        self.o = [randint(-15, 15) for i in range(0, self.t)]
        
    def drawObstacle(self):
        noFill()     
        pushMatrix()
        translate(self.cor.x, self.cor.y)
        beginShape()

        for i in range(0, self.t):
            angle = map(i, 0, self.t, 0, pi * 2)
            r = self.r + self.o[i]
            vertex(r * cos(angle), r * sin(angle))

        endShape(CLOSE)
        popMatrix()    

    def moveObstacle(self):
        self.cor.x, self.cor.y = self.cor.x + self.vel.x * 0.01, self.cor.y + self.vel.y  * 0.01

        if self.cor.x > width + self.r or self.cor.x < -self.r: self.vel.x = -self.vel.x
        if self.cor.y > height + self.r or self.cor.y < -self.r: self.vel.y = -self.vel.y  
            
        for a in aList: 
            if (self.cor.x + self.r > a.cor.x - a.r and self.cor.x + self.r < a.cor.x + a.r and self.cor.y + self.r > a.cor.y - a.r and self.cor.y + self.r < a.cor.y + a.r):
                self.vel.x, self.vel.y, a.vel.x, a.vel.y = -self.vel.x, -self.vel.y, -a.vel.x, -a.vel.y
            
    def checkRemove(self):       
        if self.f:
            if self.r > 15:
                for i in range(-1, 1): aList.append(A(self.cor.x - i * self.r, self.cor.y + i * self.r, randint(5, int(self.r * 0.75)), int(self.r * 0.25), randint(-50, 50), randint(-50, 50)))
1 Like

Welcome, commie630!

The laser-asteroid collisions and asteroid-asteroid collisions appear to work fine, so I assume that you’re referring to player-asteroid collisions?

As this is a school assignment, I’m going to avoid giving you any code :speak_no_evil:. But I’ll give you a hint –

The following lines detect asteroid-asteroid collisions:

        ...

        for a in aList: 
            if (self.cor.x + self.r > a.cor.x - a.r and self.cor.x + self.r < a.cor.x + a.r and self.cor.y + self.r > a.cor.y - a.r and self.cor.y + self.r < a.cor.y + a.r):
                ...

Think about how you can use an adapted version of this to detect player-asteroid collisions.

1 Like
1 Like

I’m still going to code the player asteroid collisions- what I am mainly referring to is the asteroid-asteroid collisions which are off. Since the asteroids are spheres, I tried using collision detection between circles, which proved to work only sometimes.

I moved the collision detection to the draw loop. I’m drawing circles to visualize each collider. Each time there’s a collision, Processing draws a line between the centers of each circle:

    ...
    def moveObstacle(self):
        self.cor.x, self.cor.y = self.cor.x + self.vel.x * 0.01, self.cor.y + self.vel.y  * 0.01

        if self.cor.x > width + self.r or self.cor.x < -self.r: self.vel.x = -self.vel.x
        if self.cor.y > height + self.r or self.cor.y < -self.r: self.vel.y = -self.vel.y  
        '''
        for a in aList: 
            if (self.cor.x + self.r > a.cor.x - a.r and self.cor.x + self.r < a.cor.x + a.r and self.cor.y + self.r > a.cor.y - a.r and self.cor.y + self.r < a.cor.y + a.r):
                self.vel.x, self.vel.y, a.vel.x, a.vel.y = -self.vel.x, -self.vel.y, -a.vel.x, -a.vel.y
        '''
...

def draw():
    background(0)
    pl.drawPlayer()    
    
    for a in aList: 
        # visualize collider
        stroke(0x88FFFF00)
        circle(a.cor.x, a.cor.y,  a.r*2)
        
        for b in aList:
            distance = dist(a.cor.x, a.cor.y + a.r, b.cor.x, b.cor.y + a.r)
            # rebound if collision detected
            if distance > 0 and distance < a.r + b.r:
                stroke(0x8800FF00)
                line(a.cor.x, a.cor.y, b.cor.x, b.cor.y)
                b.vel *= -1
                # for calculating accurate rebound angles, checkout:
                # https://processing.org/examples/circlecollision.html
        
        strokeWeight(1)
        stroke('#FFFFFF')
        
        a.drawObstacle()
        ...

It seems to work okay, and you get a better visual sense of what’s happening (to help with debugging). You’ll need to add some code to your game that prevents asteroids from spawning on top of one another.

I’ve included a comment with a reference for getting your rebound angles accurate (if this is important to you). For greater accuracy, you could look at using a physics library.

1 Like