如何在java中进行碰撞检测?

发布于 2024-12-11 05:59:39 字数 5484 浏览 0 评论 0原文

我有两个图像以随机方向移动。现在,如果这些图像会相互碰撞,我希望它消失,这就是我进行碰撞检测的问题。有人可以帮我解决这个问题吗?

这是代码:

public class HumanBeing extends Sprite implements ImageObserver
{
private java.awt.Image humanImage;
private final World world;
private double x;
private double y;
private double speed;
private double direction = 1;
private java.util.List<Sprite> objects = new ArrayList<Sprite>();
private double angle;

public HumanBeing(World world, double x, double y, double speed)
{
    this.world =world;
    this.x = x;
    this.y = y;
    this.speed = convertToMeterPerSecond(speed);

    URL iU = this.getClass().getResource("human.jpg");
    ImageIcon icon = new ImageIcon(iU);
    humanImage = icon.getImage();
    objects.add(this);
}

public Image getImage()
{
    return humanImage;
}

@Override
public void move(long dt)
{
    double dt_s = dt / 1e9;
    double dx_m = speed * dt_s * Math.sin(angle);
    double dy_m = speed * dt_s * Math.cos(angle);

    final double right_wall = world.x1_world;
    final double up_wall = world.y1_world;
    final double down_wall = 0.0;
    final double left_wall = 0.0;

    x += dx_m;
    y += dy_m;

    if (x >= right_wall)
    {            
        setRandomDirection();
    }
    if (y > up_wall)
    {            
        setRandomDirection();
    }
    if (x <= left_wall)
    {            
        setRandomDirection();
    }
    if (y < down_wall)
    {            
        setRandomDirection();
    }
}

public void setRandomDirection()
{
    HumanBeing humanbeing = this;
    humanbeing.setAngle(Math.PI * 2 * Math.random());
}

@Override
public void render(Graphics2D g2d)
{
    AffineTransform t = g2d.getTransform();

    final double humanHeight = 1.6;// meter
    final double humanWidth = 1.8;  //meter

    final double foot_position_y = humanHeight;
    final double foot_position_x = humanWidth / 2;

    int xx = world.convertToPixelX(x - foot_position_x); // to find the upper-left corner
    int yy = world.convertToPixelY(y + foot_position_y); // to find the upper-left corner

    g2d.translate(xx, yy);

    // ratio for actual Image size

    double x_expected_pixels = humanHeight * board.meter;
    double y_expected_pixels = humanWidth * board.meter;

    double w = ((ToolkitImage) humanImage).getWidth();
    double h = ((ToolkitImage) humanImage).getHeight();

    double x_s = x_expected_pixels / w;
    double y_s = y_expected_pixels / h;

    g2d.scale(x_s, y_s);
    g2d.drawImage(getImage(), 0, 0, this); // upper left corner
    g2d.setColor(Color.BLACK);
    g2d.setTransform(t);
}


@Override
public void moveAt(double distance_x, double distance_y)
{
    this.x = distance_x;
    this.y = distance_y;
}

@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
{
    return false;
}

public void setAngle(double angle)
{
    this.angle = angle;
}
}

这是我的 HumanBeing 类

public class Chicken extends Sprite implements ImageObserver
{
private java.awt.Image chickenImage;

private final World world;
private double x;
private double y;
private double speed;
private double angle;

public Chicken(World world, double x, double y, double speed)
{
    this.world = world;
    this.x = x;
    this.y = y;
    this.speed = convertToMeterPerSecond(speed);

    URL iU = this.getClass().getResource("chicken.gif");
    ImageIcon icon = new ImageIcon(iU);
    chickenImage = icon.getImage();
}
public Image getImage()
{
    return chickenImage;
}
public void move(long dt)
{
    double dt_s = dt / 1e9;
    double dx_m = speed * dt_s * Math.sin(angle);
    double dy_m = speed * dt_s * Math.cos(angle);

    final double right_wall = world.x1_world;
    final double up_wall = world.y1_world;
    final double down_wall = 0.0;
    final double left_wall = 0.0;

    x += dx_m;
    y += dy_m;

    if (x >= right_wall)
    {
        x = right_wall;           
        setRandomDirection();
    }
    if (y > up_wall)
    {
        y = up_wall;          
        setRandomDirection();
    }
    if (x <= left_wall)
    {
        x = left_wall;           
        setRandomDirection();
    }
    if (y < down_wall)
    {
        y = down_wall;           
        setRandomDirection();
    }

}
public void setRandomDirection()
{
    Chicken chicken = this;
    chicken.setAngle(Math.PI * 2 * Math.random());
}

@Override
public void render(Graphics2D g2d)
{
    AffineTransform t = g2d.getTransform();
    double height = 0.3; //meter
    double width = 0.3;  //meter
    double chicken_footy = height;
    double chicken_footx = width/2;

    int xx = world.convertToPixelX(x - chicken_footx);
    int yy = world.convertToPixelY(y + chicken_footy);

    g2d.translate(xx, yy);

    double x_expected_pixels = width * board.meter;
    double y_expected_pixels = height * board.meter;
    double x_s = x_expected_pixels / ((ToolkitImage) chickenImage).getWidth();
    double y_s = y_expected_pixels / ((ToolkitImage) chickenImage).getHeight();

    g2d.scale(x_s, y_s);
    g2d.drawImage(getImage(), 0, 0, this);
    g2d.setColor(Color.BLACK);
    g2d.setTransform(t);
}

public void moveAt(double distance_x, double distance_y)
{
    this.x = (int) distance_x;
    this.y = (int) distance_y;
}
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
{
    return true;
}

public void setAngle(double angle)
{
    this.angle = angle;
}
}

这是我的鸡类

I have a two image that moves in a random direction. Now, if that images would bump each other I want it to disappear and that is my problem about making a collision detection. Could someone help me about this problem?

Here is the code:

public class HumanBeing extends Sprite implements ImageObserver
{
private java.awt.Image humanImage;
private final World world;
private double x;
private double y;
private double speed;
private double direction = 1;
private java.util.List<Sprite> objects = new ArrayList<Sprite>();
private double angle;

public HumanBeing(World world, double x, double y, double speed)
{
    this.world =world;
    this.x = x;
    this.y = y;
    this.speed = convertToMeterPerSecond(speed);

    URL iU = this.getClass().getResource("human.jpg");
    ImageIcon icon = new ImageIcon(iU);
    humanImage = icon.getImage();
    objects.add(this);
}

public Image getImage()
{
    return humanImage;
}

@Override
public void move(long dt)
{
    double dt_s = dt / 1e9;
    double dx_m = speed * dt_s * Math.sin(angle);
    double dy_m = speed * dt_s * Math.cos(angle);

    final double right_wall = world.x1_world;
    final double up_wall = world.y1_world;
    final double down_wall = 0.0;
    final double left_wall = 0.0;

    x += dx_m;
    y += dy_m;

    if (x >= right_wall)
    {            
        setRandomDirection();
    }
    if (y > up_wall)
    {            
        setRandomDirection();
    }
    if (x <= left_wall)
    {            
        setRandomDirection();
    }
    if (y < down_wall)
    {            
        setRandomDirection();
    }
}

public void setRandomDirection()
{
    HumanBeing humanbeing = this;
    humanbeing.setAngle(Math.PI * 2 * Math.random());
}

@Override
public void render(Graphics2D g2d)
{
    AffineTransform t = g2d.getTransform();

    final double humanHeight = 1.6;// meter
    final double humanWidth = 1.8;  //meter

    final double foot_position_y = humanHeight;
    final double foot_position_x = humanWidth / 2;

    int xx = world.convertToPixelX(x - foot_position_x); // to find the upper-left corner
    int yy = world.convertToPixelY(y + foot_position_y); // to find the upper-left corner

    g2d.translate(xx, yy);

    // ratio for actual Image size

    double x_expected_pixels = humanHeight * board.meter;
    double y_expected_pixels = humanWidth * board.meter;

    double w = ((ToolkitImage) humanImage).getWidth();
    double h = ((ToolkitImage) humanImage).getHeight();

    double x_s = x_expected_pixels / w;
    double y_s = y_expected_pixels / h;

    g2d.scale(x_s, y_s);
    g2d.drawImage(getImage(), 0, 0, this); // upper left corner
    g2d.setColor(Color.BLACK);
    g2d.setTransform(t);
}


@Override
public void moveAt(double distance_x, double distance_y)
{
    this.x = distance_x;
    this.y = distance_y;
}

@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
{
    return false;
}

public void setAngle(double angle)
{
    this.angle = angle;
}
}

this is my HumanBeing class

public class Chicken extends Sprite implements ImageObserver
{
private java.awt.Image chickenImage;

private final World world;
private double x;
private double y;
private double speed;
private double angle;

public Chicken(World world, double x, double y, double speed)
{
    this.world = world;
    this.x = x;
    this.y = y;
    this.speed = convertToMeterPerSecond(speed);

    URL iU = this.getClass().getResource("chicken.gif");
    ImageIcon icon = new ImageIcon(iU);
    chickenImage = icon.getImage();
}
public Image getImage()
{
    return chickenImage;
}
public void move(long dt)
{
    double dt_s = dt / 1e9;
    double dx_m = speed * dt_s * Math.sin(angle);
    double dy_m = speed * dt_s * Math.cos(angle);

    final double right_wall = world.x1_world;
    final double up_wall = world.y1_world;
    final double down_wall = 0.0;
    final double left_wall = 0.0;

    x += dx_m;
    y += dy_m;

    if (x >= right_wall)
    {
        x = right_wall;           
        setRandomDirection();
    }
    if (y > up_wall)
    {
        y = up_wall;          
        setRandomDirection();
    }
    if (x <= left_wall)
    {
        x = left_wall;           
        setRandomDirection();
    }
    if (y < down_wall)
    {
        y = down_wall;           
        setRandomDirection();
    }

}
public void setRandomDirection()
{
    Chicken chicken = this;
    chicken.setAngle(Math.PI * 2 * Math.random());
}

@Override
public void render(Graphics2D g2d)
{
    AffineTransform t = g2d.getTransform();
    double height = 0.3; //meter
    double width = 0.3;  //meter
    double chicken_footy = height;
    double chicken_footx = width/2;

    int xx = world.convertToPixelX(x - chicken_footx);
    int yy = world.convertToPixelY(y + chicken_footy);

    g2d.translate(xx, yy);

    double x_expected_pixels = width * board.meter;
    double y_expected_pixels = height * board.meter;
    double x_s = x_expected_pixels / ((ToolkitImage) chickenImage).getWidth();
    double y_s = y_expected_pixels / ((ToolkitImage) chickenImage).getHeight();

    g2d.scale(x_s, y_s);
    g2d.drawImage(getImage(), 0, 0, this);
    g2d.setColor(Color.BLACK);
    g2d.setTransform(t);
}

public void moveAt(double distance_x, double distance_y)
{
    this.x = (int) distance_x;
    this.y = (int) distance_y;
}
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
{
    return true;
}

public void setAngle(double angle)
{
    this.angle = angle;
}
}

this is my chicken class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘香 2024-12-18 05:59:39

像 Box 一样在精灵周围绘制不可见的矩形,然后使用下面的方法来检查它们是否相交:)

http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Rectangle.html#intersects(java.awt.Rectangle)

if(rect.intersect(anotherRect))
{
   disappear();
}

祝你好运

Draw invisible Rectangle around your sprites as like Box's and than use below method to check whether they are intersecting or not :)

http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Rectangle.html#intersects(java.awt.Rectangle)

if(rect.intersect(anotherRect))
{
   disappear();
}

Good luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文