# Help needed- asteroids collision detection

**URL:** <https://discourse.processing.org/t/help-needed-asteroids-collision-detection/17622>\
**Category:** Processing.py\
**Tags:** homework\
**Created:** [February 7, 2020, 11:52pm UTC](https://discourse.processing.org/t/help-needed-asteroids-collision-detection/17622 "2020-02-07T23:52:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![commie630](https://avatars.discourse-cdn.com/v4/letter/c/ee7513/32.png) [@commie630](https://discourse.processing.org/u/commie630)\
**Post date:** [February 7, 2020, 11:52pm UTC](https://discourse.processing.org/t/help-needed-asteroids-collision-detection/17622/1 "2020-02-07T23:52:56Z")

</div>

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

```auto

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)))
```

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [February 8, 2020, 4:28am UTC](https://discourse.processing.org/t/help-needed-asteroids-collision-detection/17622/2 "2020-02-08T04:28:55Z")

</div>

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 🙊. But I’ll give you a hint –

The following lines detect asteroid-asteroid collisions:

```auto
        ...

        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.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 8, 2020, 4:50am UTC](https://discourse.processing.org/t/help-needed-asteroids-collision-detection/17622/3 "2020-02-08T04:50:03Z")

</div>

> **[Collision Detection](http://jeffreythompson.org/collision-detection/poly-poly.php)**
>
> An online book about collision detection using Processing.

---

<div class="post-metadata">

**Author:** ![commie630](https://avatars.discourse-cdn.com/v4/letter/c/ee7513/32.png) [@commie630](https://discourse.processing.org/u/commie630)\
**Post date:** [February 8, 2020, 1:44pm UTC](https://discourse.processing.org/t/help-needed-asteroids-collision-detection/17622/4 "2020-02-08T13:44:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [February 8, 2020, 9:48pm UTC](https://discourse.processing.org/t/help-needed-asteroids-collision-detection/17622/5 "2020-02-08T21:48:10Z")

</div>

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:

```auto
    ...
    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](https://github.com/pybox2d/pypybox2d).
