Collisions and arrays on Processing Java

Need help with Collosions with moving objects, along with array backgrounds

1 Like

Hello and welcome to the forum!

Can you be more specific please?

Can you post your entire code please? Where are you stuck?


there is a nice online book with multiple chapters: Collision Detection

You can browse the book with the small arrows left and right on top of each page

In general

In general, when you have rectangles or balls in your array (as objects derived from a class?) you want to for-loop over the array and compare each ball with the ones after it in the array:

// Good
for(int i=0; i<list.length-1; i++) {
    for(int i2=i+1; i2<list.length; i2++) {
       if(list[i]   .... list [i2]..... // check collision

I mention that because some beginners compare every ball with every other ball (bad) which checks each pair twice and not only once:

// BAD 
for(int i=0; i<list.length; i++) {
    for(int i2=0; i2<list.length; i2++) {
       if(list[i]   .... list [i2]..... // check collision


see Bouncy Bubbles / Examples /

see Circle Collision with Swapping Velocities / Examples /

Warm regards,

Chrisir :wink:

1 Like

i was just not sure how to attempt the stars in the background and the moving red balls

  • where the background moves from left to right
  • the red balls moving towards triange

If you want the red balls to move towards the triangle and stop when they hit it, two easy ways are to use processing’s dist() function or make an AABB (axis aligned bounding box) class, give one to all objects, and use it to check collisions.

1 Like

if i was to make the ball reach the end and say lose or when triangle shoots a beam and say win, how would i attempt this??