import java.awt.Color;
import java.awt.Graphics;
import java.util.Vector;import javax.swing.JFrame;
public class My_demo extends JFrame implements Runnable{ Thread t;
Vector<ARC> v;
ARC ac;
private static final long serialVersionUID = 1L;
public My_demo() {
this.setSize(600,400);
this.setVisible(true);
v=new Vector<ARC>();  // 初始化 集合类
t=new Thread(this);
t.start();
}
public static void main(String[] args) {
new My_demo();
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
 if(v!=null){   // 绘制 圆
 for(int i = 0 ; i <v.size() ; i ++){
 v.elementAt(i).paint(g);
 }
 }
}
int index;
public void run() {
while(true){
index++;
if(index%10==0&&v.size()<8){  // 圆的数目 定为8个 
ac=new ARC(100+v.size()*30, 100);
v.add(ac);
}
 if(v!=null){               //调用移动的方法
 for(int i = 0 ; i <v.size() ; i ++){  
 v.elementAt(i).move();
 }
 for(int i=0;i<v.size();i++){  //调用碰撞
 for(int j=0;j<v.size();j++){
 if(i==j){
 continue;
 }
 if(v.elementAt(i).delete(v.elementAt(i).x, v.elementAt(i).y, v.elementAt(j).x, v.elementAt(j).y))
 {
 
 ///////   这里 ////////
 
 
 
 
 int num;  // 碰撞到的时候处理            // 
 num=v.elementAt(i).dir;
 v.elementAt(i).dir=v.elementAt(j).dir;
 v.elementAt(j).dir=num;
 
 }
 }
 }
 }
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {

e.printStackTrace();
}
}
}}
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class ARC {
int x,y;
int dir=1;
Random r=new Random();
public ARC(int x,int y) {
this.x=x;
this.y=y;
}
void paint(Graphics g){
g.setColor(Color.red);
g.fillOval(x+15, y+15, 30, 30);
}
int index=0;
void move(){ //移动
index++;
if(index%30==0){
dir=r.nextInt(4);
}

switch (dir) {
case 0:
x+=1;
if(x>550){
x=550;
}

break;
case 1:
x-=1;
if(x<50){
x=50;
}

break;
case 2:
y+=1;
if(y>350){
y=350;
}

break;
case 3:
y-=1;
if(y<50){
y=50;
}

break;
}
}
boolean delete(int x1,int y1,int x2,int y2){   // 碰撞

int xx=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);

if(xx<900){
return true;
}
return false;

}
}现在的能出现圆,但是碰撞不准。
我的要的效果是,圆和圆不能重叠的效果。或是 碰到了就反反相移动都可以。
求大神帮忙解决,谢谢了、

解决方案 »

  1.   


    for(int i=0;i<array_length;i++){
    if(m_ball.isActive()){
    if(m_ball.getX()<=m_left.getX()+this.bar_x){//左碰撞m_ball.setDir_x(this.RIGHT);this.hitTheLine(isSound);if(m_left.active&&this.right_action){this.lose();}}if((m_ball.getX()+m_ball.getWidth())>=m_right.getX()){//右碰撞m_ball.setDir_x(this.LEFT);this.hitTheLine(isSound);if(m_right.active&&this.left_action){this.lose();}}if(m_ball.getY()<=m_up.getY()+this.bar_x){//上碰撞m_ball.setDir_y(this.DOWN);this.hitTheLine(isSound);if(m_up.active&&this.down_action){this.lose();}}if((m_ball.getY()+m_ball.getHeight())>=m_down.getY()){//下碰撞m_ball.setDir_y(this.UP);this.hitTheLine(isSound);if(m_down.active&&this.up_action){this.lose();}}
    }
      }
      

  2.   


    //
            // CD() Collision Detection,碰撞检测
            //
            //               |                 |
            //               |                 |
            //4             |      2         |     5    
            //               |                 |
            //               |                 |
            //----------A/////////////B----------
            //              //               // 
            //              //               //
            //              //               //
            //3            //    1         //     3
            //              //               //
            //              //               //
            //              //               //
            //              //               //
            //--------- C/////////////D----------
            //              |                 | 
            //              |                 | 
            //6            |      2         |     7
            //              |                 | 
            //              |                 | 
            //
            //R位圆形半径
            //
            //条件1:圆心在矩形内
            //条件2:圆心 x 坐标在矩形 x 坐标范围内,但圆心 y 坐标 不在 矩形 y 坐标范围内
            //条件3:圆心 y 坐标在矩形 y 坐标范围内,但圆心 x 坐标 不在 矩形 x 坐标范围内
            //条件4:圆心在矩形左上方
            //条件5:圆心在矩形右上方
            //条件6:圆心在矩形左下方
            //条件7:圆心在矩形的右下方
            //
            //条件1处理:直接返回true,碰撞成立
            //条件2处理:当圆心处于矩形上方的时候,计算圆心y坐标与矩形y坐标的差(即圆心与矩形上边直线的距离),小于R碰撞成立;下方类似,计算圆心y坐标与(矩形y坐标+矩形高度)的差,小于R碰撞成立
            //条件3处理:当圆心处于矩形左侧,计算圆心x坐标与矩形x坐标的差,小于R碰撞成立;右方类似,计算圆心x坐标与(矩形x坐标+矩形宽度)的差,小于R碰撞成立
            //条件4处理:圆心与点A的距离小于R碰撞成立
            //条件5处理:圆心与点B的距离小于R碰撞成立
            //条件6处理:圆心与点C的距离小于R碰撞成立
            //条件7处理:圆心与点D的距离小于R碰撞成立
            //
            // x, y为矩形坐标,w, h为矩形的宽和高,crclX, crclY为圆形的坐标
            private boolean CD() {                // 圆心坐标
                    int Ox = crclX + R;
                    int Oy = crclY + R;
                    
                    boolean bRet = false;
                    
                    boolean bool1 = (Ox <= x + w) && (Ox >= x); // 圆心x坐标在矩形x坐标范围内
                    boolean bool2 = (Oy <= y + h) && (Oy >= y); // 圆心y坐标在矩形y坐标范围内
                    
                    if (bool1 && bool2) // 条件1
                            return true;
                    else if (bool1) {        // 条件2
                            if (Oy <= y) 
                                    bRet = ((y - Oy) <= R);
                            else if (Oy >= y + h)
                                    bRet = ((Oy - (y + h)) <= R);
                    } else if (bool2) {        // 条件3
                            if (Ox <= x) 
                                    bRet = ((x - Ox) <= R);
                            else if (Ox >= x + w)
                                    bRet = (Ox - (x + w) <= R);
                    } else {
                            if (Ox <= x) {
                                    if (Oy <= y)                        // 条件4
                                            bRet = distanceSquare(Ox, Oy, x, y) <= R * R;
                                    else if (Oy >= y + h)        // 条件6
                                            bRet = distanceSquare(Ox, Oy, x, y+h) <= R * R;
                            }
                            else if (Ox >= x+w) {
                                    if (Oy <= y)                        // 条件5
                                            bRet = distanceSquare(Ox, Oy, x+w, y) <= R * R;
                                    else if (Oy >= y+h)                // 条件7
                                            bRet = distanceSquare(Ox, Oy, x+w, y+h) <= R * R;
                            }
                    }
                    
                    
                    return bRet;
            }