I am creating a project of bouncing balls.
The only problem with my project is with collision resolution when two balls intersect with each other.Collision detection is fine as when two balls intersect they bounce back but then they keep colliding again and again.When the balls collide with the walls they bounce correctly but I don't know why when they collide with each other there is a problem.
I have tried various code but still can't get it.
How can i do this ?
You can take help of this link as did I..
Ball to Ball Collision - Detection and Handling
Here is my code :
package com.example.bouncer;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.provider.SyncStateContract.Constants;
public class Ball {
private Point p; //Point p:Represents the x and y position of the Ball
private int c; //Represents the color of the Ball
private int r; //Represents the Radius of the Ball.
private int dx; //Integer dx:Represents the change in x position of ball
// Integer dy:Represents the change in y position of ball
private int dy;
private Paint paint; //Android Object holding the color for drawing on the canvas
public Ball(int x,int y,int col,int radius)
{
p=new Point(x,y);
c=col;
r=radius;
paint=new Paint();
dx=0;
dy=0;
}
public int getX()
{return p.x;
}
public int getY()
{
return p.y;
}
public int getRadius()
{return r;
}
public Paint getPaint()
{return paint;
}
public void setColor(int col)
{c=col;
}
public void goTo(int x,int y)
{p.x=x;
p.y=y;
}
public void setDX(int speed)
{dx=speed;
}
public void setDY(int speed)
{
dy=speed;
}
public void move()
{
p.x=p.x+dx;
p.y=p.y+dy;
}
public void bounce(Canvas canvas) //COLLISION DETECTION AND RESOLUTION WITH WALLS
{
move();
if(p.x>canvas.getWidth()|| p.x<0)
{
dx=dx * -1;
}
if(p.y>canvas.getWidth()|| p.y<0)
{
dy=dy * -1;
}
}
public void bounceoff(Ball b) //BALL TO BALL COLLSION DETECTION
{
float x = b.getX() - p.x;
float y = b.getY() - p.y;
float distanceSquared = x*x + y*y;
float radius = b.getRadius()+r;
float radiusSquared = radius*radius;
if (distanceSquared <= radiusSquared)
{
dx=dx * -1;
dy=dy * -1;
}
}
}
AnimationView.java
package com.example.bouncer;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
public class AnimationView extends View{
private final int FRAME_RATE=15;
private Paint paint;
private Handler h;
Ball myball;
Ball greenball;
Ball redball;
public AnimationView(Context context,AttributeSet attrs) {
super(context,attrs);
// TODO Auto-generated constructor stub
h=new Handler();
paint=new Paint();
paint.setColor(Color.BLUE);
myball=new Ball(100,100,Color.BLUE,50);
greenball=new Ball(200,200,Color.GREEN,50);
redball=new Ball(50,400,Color.RED,50);
myball.setDX(10);
myball.setDY(10);
greenball.setDX(-20);
greenball.setDY(-15);
redball.setDX(5);
redball.setDY(-5);
}
protected void onDraw(Canvas c)
{
myball.bounce(c);
greenball.bounce(c);
redball.bounce(c);
myball.bounceoff(myball);
greenball.bounceoff(greenball);
redball.bounceoff(redball);
c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
h.postDelayed(r, FRAME_RATE);
}
private Runnable r=new Runnable()
{ public void run()
{ invalidate();
}
};
}
If you want my complete project code check out the link.. https://stackoverflow.com/questions/22892736/balls-keep-colliding-again-and-again-android