This site requires JavaScript, please enable it in your browser!
Greenfoot back
Pointifix
Pointifix wrote ...

2014/3/19

Ball Collision Detection and Rebound?

1
2
Pointifix Pointifix

2014/3/19

#
Hi guys, now i have a not that simple question. This code should check if my billiardballs are colliding, if they are it should change there speed and there direction, but jeah it does not do as i want it to do, anyone ever did this before and could help me a little bit with that?
List billiardball = getObjects(BilliardBall.class);
        
        if(billiardball.size() > 1)
        {
            List storeballs = null;
            
            for(Object obj : billiardball)
            {
                for(int i = 0;i < billiardball.size();i++)
                {
                    if(obj.equals(billiardball.get(i)))
                    {
                        if(i + 1 < billiardball.size())i++;
                        else break;
                    }
                    if(storeballs != null && storeballs.contains(obj))break;
                    
                    double distance = Math.hypot(((BilliardBall)obj).getExactX() - ((BilliardBall)billiardball.get(i)).getExactX(),((BilliardBall)obj).getExactY() - ((BilliardBall)billiardball.get(i)).getExactY());
                    
                    if(distance <= ((BilliardBall)obj).radius + ((BilliardBall)billiardball.get(i)).radius)
                    {
                        double tangent = 90 + Math.toDegrees(Math.asin((((BilliardBall)billiardball.get(i)).getExactX() - ((BilliardBall)obj).getExactX()) / distance));
                        double rebound = 180 + tangent - (((BilliardBall)obj).getRotation() - tangent);
                        
                        double store_speed = ((BilliardBall)obj).speed;
                        
                        ((BilliardBall)obj).speed = ((BilliardBall)billiardball.get(i)).speed;
                        ((BilliardBall)billiardball.get(i)).speed = store_speed;
                        
                        ((BilliardBall)obj).setRotation((int)rebound);
                        ((BilliardBall)obj).move(((BilliardBall)obj).speed);
                        
                        storeballs.add(storeballs.size(),(BilliardBall)billiardball.get(i));
                    }
                }
            }
        }
Entity1037 Entity1037

2014/3/19

#
I'm not sure whaty ou're doing here... are you using the move and turn methods provided by greenfoot? If so, you can just subtract you're ball's current rotation from the surface's rotation (keeping in mind that 0 is really 360) and get it's bounce trajectory that way (just make sure that you convert negative integers like -90 into 270).
Entity1037 Entity1037

2014/3/19

#
I think that will work anyway.
Pointifix Pointifix

2014/3/19

#
im using them to get the balls out of the collision area again, cause otherwhise the angles would be changed more then once i think i used the same principle as in my rebound simulation scenario, with the difference, that i first have to know the angle of my tangent the balls are hitting at (as you can see at double tangent = .... jeah but it does not always work fine
Entity1037 Entity1037

2014/3/19

#
Couldn't you just turn the ball (I'll just call it object a) you're ball object (object b) is hitting with the turnTowards() method after saving his original rotation, and then use that to calculate the bounce trajectory off of the tangent of the point on object a that was intersected by object b in order to find object b's new trajectory, then reload object a's old coordinates, save object b's new coordinates, turn b towards a, and then calculate the bounce trajectory for a?
Pointifix Pointifix

2014/3/20

#
you mean using the turnTowards method to get the angle of my tangent to calculate the rebound angle yes thats possible, but still does the same as my principle
Entity1037 Entity1037

2014/3/20

#
Alright then. I'm confused on how it doens't always work fine. Is it because one just turns before the other detects it, or...?
Pointifix Pointifix

2014/3/20

#
jeah if they are overlapping too much i think they change directions more then once
Entity1037 Entity1037

2014/3/21

#
Maybe make the balls move so that they are right next to each other when they collide (so make sure that when the ball collides, it does move(-x) so that it doesn't intersect the ball anymore), then calculate the trajectory of both balls all from one of the balls, and prevent the other ball from moving and activating the same method that calculates the trajectory of the both balls. Also, make sure to use a collision method like getAnIntersectingObject(Class class) that's provided by the SpriteSheet class (found here: http://www.greenfoot.org/scenarios/8502) so it detects collisions with the colored part of the balls, and not the square image. Note: the description of the scenario says it's get*One*IntersectingObject, but it's not.
Pointifix Pointifix

2014/3/21

#
mhhmm i found some other pool games in greenfoot that could help ;) maybe ill get ideas from that
Entity1037 Entity1037

2014/3/21

#
Alright. Oh, I just realized that my method would glitch if three balls bounced into each other at once. Hm...........
Pointifix Pointifix

2014/3/21

#
i uploaded my demo version, the balls still get stuck together sometimes, but i dont know why :( http://www.greenfoot.org/scenarios/11146
Entity1037 Entity1037

2014/3/21

#
The main problem seems to be that the balls get stuck into each other. Maybe calculate the amount that the balls are interesecting, and move the one that first hits the other one back so it no longer intersects it (move it by making the ball face the other, add 180 degrees so it faces away, and move it). I mean their perfect circles, so the amount they intersect should be easily calculable.
Pointifix Pointifix

2014/3/22

#
mhhmm could be a possibility, by the way im already moving them once with their new velocitys to get them out of collisision but i will try that ;)
Pointifix Pointifix

2014/3/22

#
Look at my scenario again, i found the BIG fail i´ve made ;D
There are more replies on the next page.
1
2