使球弹跳在另一个球 [英] Making a ball bounce inside another ball

查看:291
本文介绍了使球弹跳在另一个球的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有大量的信息,如何使球相互碰撞,他们的外边界,但我无法找到任何东西,包括球弹跳在另一个球

There is plenty of information about how to make balls collide with one another on their outer boundaries, but I couldn't find anything that covers balls bouncing inside another ball.

什么是做到这一点的最好方法是什么?我相信很多人都会从中受益,包括我自己。

What would be the best way to accomplish this? I believe many people would benefit from this, including myself.

推荐答案

这是很简单的使用Java 2D。

It is quite simple using Java 2D.

/** A method to determine if area1 is entirely contained within area2. */
public boolean doAreasEnclose(Area area1, Area area2) {
    Area temp = new Area(area1);
    temp.subtract(area2);
    Rectangle r = temp.getBounds();
    boolean enclosed = r.getWidth()==0 && r.getHeight()==0;
    return enclosed;
}

的障碍所示:

The obstacle is shown:

  • 绿色如果不与球员接触
  • ORANGE 如果相交。
  • 红色如果全部包围。
  • GREEN if not in contact with the player
  • ORANGE if intersecting.
  • RED if entirely enclosing.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class ShapeContainment {

    private BufferedImage img;
    private Area[] obstacles = new Area[2];
    private Area walls;

    int x; 
    int y;
    int xDelta = 3;
    int yDelta = 2;

    /** A method to determine if area1 is entirely contained within area2. */
    public boolean doAreasEnclose(Area area1, Area area2) {
        Area temp = new Area(area1);
        temp.subtract(area2);
        Rectangle r = temp.getBounds();
        boolean enclosed = r.getWidth()==0 && r.getHeight()==0;
        return enclosed;
    }

    /** A method to determine if two instances of Area intersect */
    public boolean doAreasCollide(Area area1, Area area2) {
        boolean collide = false;

        Area collide1 = new Area(area1);
        collide1.subtract(area2);
        if (!collide1.equals(area1)) {
            collide = true;
        }

        Area collide2 = new Area(area2);
        collide2.subtract(area1);
        if (!collide2.equals(area2)) {
            collide = true;
        }

        return collide;
    }

    ShapeContainment() {
        int w = 400;
        int h = 200;
        img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        final JLabel imageLabel = new JLabel(new ImageIcon(img));
        x = w/2;
        y = h/2;

        //circle 
        obstacles[0] = new Area(new Ellipse2D.Double(40, 40, 60, 60));

        int[] xTriangle = {330,360,345};
        int[] yTriangle = {60,60,40};
        //triangle 
        obstacles[1] = new Area(new Ellipse2D.Double(300, 100, 60, 60));

        walls = new Area(new Rectangle(0,0,w,h));

        ActionListener animate = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                animate();
                imageLabel.repaint();
            }
        };
        Timer timer = new Timer(50, animate);

        timer.start();
        JOptionPane.showMessageDialog(null, imageLabel);
        timer.stop();
    }

    public void animate() {
        Graphics2D g = img.createGraphics();
        g.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);

        g.setColor(Color.BLUE);
        g.fillRect(0, 0, img.getWidth(), img.getHeight());
        x+=xDelta;
        y+=yDelta;
        int s = 15;
        Area player = new Area(new Ellipse2D.Double(x, y, s, s));

        // Acid test of edge collision;
        if (doAreasCollide(player,walls)) {
            if ( x+s>img.getWidth() || x<0 ) {
                xDelta *= -1;
            } 
            if(y+s>img.getHeight() || y<0 ) {
                yDelta *= -1;
            }
        }
        g.setColor(Color.ORANGE);
        for (Area obstacle : obstacles) {
            if (doAreasEnclose(player, obstacle)) {
                g.setColor(Color.RED);
            } else if (doAreasCollide(obstacle, player)) {
                g.setColor(Color.ORANGE);
            } else {
                g.setColor(Color.GREEN);
            }
            g.fill(obstacle);
        }

        g.setColor(Color.YELLOW);
        g.fill(player);


        g.dispose();
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                new ShapeContainment();
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

从code看到这个答案。

这篇关于使球弹跳在另一个球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆