Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Collision

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Collision

    I need help writing a method that check if the two sprites blackhole and ball collide and when a collision occurs, the two sprites both disappear from the screen.

    public class AnimationClass extends Applet  implements Runnable {
        static int SCREENWIDTH = 640;
        static int SCREENHEIGHT = 480;
        Thread gameloop;
        Random rand = new Random();
     
        //double buffer objects
        BufferedImage backbuffer;
        Graphics2D g2d;
     
        Image background;
     
        //sprite variables
        AnimatedSprite ball;
        AnimatedSprite blackhole;
     
        private URL getURL(String filename) {
             URL url = null;
             try {
                 url = this.getClass().getResource(filename);
             }
             catch (Exception e) {}
             return url;
        }
     
        public void init() {
            //create the back buffer for smooth graphics
            backbuffer = new BufferedImage(SCREENWIDTH, SCREENHEIGHT,
                BufferedImage.TYPE_INT_RGB);
            g2d = backbuffer.createGraphics();
     
            //load the background image
            Toolkit tk = Toolkit.getDefaultToolkit();
            background = tk.getImage(getURL("woodgrain.png"));
     
            //load the ball animation strip
            ball = new AnimatedSprite(this, g2d);
            ball.load("xball.png", 8, 8, 64, 64);
            ball.setPosition(new Point2D(300,200));
            ball.setFrameDelay(1);
            ball.setVelocity(new Point2D(1,1));
            ball.setRotationRate(1.0);
     
            blackhole = new AnimatedSprite(this, g2d);
            blackhole.load("blackhole64x64x16.png", 8, 8, 64, 64);
            blackhole.setPosition(new Point2D(100,200));
            blackhole.setFrameDelay(2);
            blackhole.setVelocity(new Point2D(1,1));
            blackhole.setRotationRate(1.0);
     
        }
     
        public void start() {
            gameloop = new Thread(this);
            gameloop.start();
        }
     
        public void stop() {
            gameloop = null;
        }
     
        public void run() {
            Thread t = Thread.currentThread();
            while (t == gameloop) {
                try {
                    Thread.sleep(5);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                gameUpdate();
                repaint();
            }
        }
     
        public void gameUpdate() {
            //see if it's time to animate
            ball.updateAnimation();
            blackhole.updateAnimation();
     
            //update the ball position
            ball.updatePosition();
            if ((ball.position().X() < 0) || (ball.position().X() > SCREENWIDTH - 64)) {
                double x = ball.velocity().X() * -1;
                ball.setVelocity(new Point2D(x, ball.velocity().Y()));
                x = ball.position().X();
                ball.setPosition(new Point2D(x, ball.position().Y()));
            }
            if ((ball.position().Y() < 0) || (ball.position().Y() > SCREENHEIGHT - 64)) {
                double y = ball.velocity().Y() * -1;
                ball.setVelocity(new Point2D(ball.velocity().X(), y));
                y = ball.position().Y() + ball.velocity().Y();
                ball.setPosition(new Point2D(ball.position().X(), y));
            }
     
     
    			blackhole.updatePosition();
            if ((blackhole.position().X() < 0) || (blackhole.position().X() > SCREENWIDTH - 64)) {
    		            double x = blackhole.velocity().X() * -1;
    		            blackhole.setVelocity(new Point2D(x, blackhole.velocity().Y()));
    		            x = blackhole.position().X();
    		            blackhole.setPosition(new Point2D(x, blackhole.position().Y()));
    		        }
    		        if ((blackhole.position().Y() < 0) || (blackhole.position().Y() > SCREENHEIGHT - 64)) {
    		            double y = blackhole.velocity().Y() * -1;
    		            blackhole.setVelocity(new Point2D(blackhole.velocity().X(), y));
    		            y = blackhole.position().Y() + blackhole.velocity().Y();
    		            blackhole.setPosition(new Point2D(blackhole.position().X(), y));
            }
     
            //lastly, update the rotation
            ball.updateRotation();
            blackhole.updateRotation();
    }
     
     
        public void update(Graphics g) {
            //draw the background
            g2d.drawImage(background, 0, 0, SCREENWIDTH-1, SCREENHEIGHT-1, this);
     
            //draw the current frame of animation
            ball.draw();
            blackhole.draw();
     
            g2d.setColor(Color.BLACK);
            g2d.drawString("Position: " + ball.position().X() + "," +
                           ball.position().Y(), 5, 10);
            g2d.drawString("Velocity: " + ball.velocity().X() + "," +
                           ball.velocity().Y(), 5, 25);
            g2d.drawString("Animation: " + ball.currentFrame(), 5, 40);
     
     
     
            g2d.drawString("Position: " + blackhole.position().X() + "," +
    		                ball.position().Y(), 5,450 );
    		g2d.drawString("Velocity: " + blackhole.velocity().X() + "," +
    		                ball.velocity().Y(), 5, 465);
            g2d.drawString("Animation: " + blackhole.currentFrame(), 5, 480);
     
     
     
     
     
            paint(g);
        }
     
     
        public void paint(Graphics g) {
            //draw the back buffer to the screen
            g.drawImage(backbuffer, 0, 0, this);
        }
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,260
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Collision

    Can you explain what your problem is? If you get errors, please copy and paste the full text here.

    When posting code, please enclose it in code tags. See: BB Code List - Java Forums

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Collision

    Highlight tags will give you Java syntax highlighting:

    [highlight=java].. your code here ..[/highlight]

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Collision

    You are going to need to attach the sprite images to your thread so we can test this.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Collision

    If you can accept your sprites as rectangular shapes, you can use the Rectangle class to check whether they intersect using the Rectangle.intersects(..) method.

Similar Threads

  1. Need help with collision in a game!
    By Skyhigh32 in forum Java Theory & Questions
    Replies: 5
    Last Post: May 18th, 2011, 01:12 AM
  2. Collision Detecting
    By iams3b in forum AWT / Java Swing
    Replies: 0
    Last Post: February 6th, 2011, 01:48 AM
  3. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 11:39 AM
  4. for Loop for my 2D collision not working??
    By DarrenReeder in forum Loops & Control Statements
    Replies: 1
    Last Post: March 7th, 2010, 10:05 AM
  5. bounding box collision
    By beechy34 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 5th, 2010, 08:58 AM