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 4 of 4

Thread: Need help with object collision

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Location
    Kalispell, MT
    Posts
    2
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with object collision

    I only have about 2 months experience with programming with java, so I apologize in advance for my huge wall of code.

    I created a program that will create 5 random shapes(ovals or rectangles) at random locations on the JFrame with random colors, moving in random directions, and bouncing off of the perimeter of the JFrame.

    I was wondering how i would be able to make these shapes repel in opposite directions when they collide with each other. I can't seem to work out the logic behind doing this, so any help at all would be much appreciated.

    This is the code i'm currently working with:

    package screen.saver.project;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.util.Random;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class ScreenSaver extends JPanel implements Runnable 
    {
        private Random random = new Random();
     
        //Diameter of shape
        int diameter = 50;
     
        //Random Placement generator for shape1
        private int x1 = random.nextInt(400);
        private int y1 = random.nextInt(400); 
     
        //Random Placement generator for shape2
        private int x2 = random.nextInt(400);
        private int y2 = random.nextInt(400);
     
        //Random Placement generator for shape3
        private int x3 = random.nextInt(400);
        private int y3 = random.nextInt(400);
     
        //Random Placement generator for shape4
        private int x4 = random.nextInt(400);
        private int y4 = random.nextInt(400);
     
        //Random Placement generator for shape5
        private int x5 = random.nextInt(400);
        private int y5 = random.nextInt(400);
     
        //Random Movement generator for shape1
        private int directionX1 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
        private int directionY1 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 ); 
     
        //Random Movement generator for shape2
        private int directionX2 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
        private int directionY2 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
     
        //Random Movement generator for shape3
        private int directionX3 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
        private int directionY3 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
     
        //Random Movement generator for shape4
        private int directionX4 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
        private int directionY4 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
     
        //Random Movement generator for shape5
        private int directionX5 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
        private int directionY5 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
     
        //Random color generator for shape1
        int red1 = random.nextInt(256);
        int blue1 = random.nextInt(256);
        int green1 = random.nextInt(256);
        Color Color1 = new Color( red1, blue1, green1 );
     
        //Random color generator for shape2
        int red2 = random.nextInt(256);
        int blue2 = random.nextInt(256);
        int green2 = random.nextInt(256);
        Color Color2 = new Color( red2, blue2, green2 );
     
        //Random color generator for shape3
        int red3 = random.nextInt(256);
        int blue3 = random.nextInt(256);
        int green3 = random.nextInt(256);
        Color Color3 = new Color( red3, blue3, green3 );
     
        //Random color generator for shape4
        int red4 = random.nextInt(256);
        int blue4 = random.nextInt(256);
        int green4 = random.nextInt(256);
        Color Color4 = new Color( red4, blue4, green4 );
     
        //Random color generator for shape5
        int red5 = random.nextInt(256);
        int blue5 = random.nextInt(256);
        int green5 = random.nextInt(256);
        Color Color5 = new Color( red5, blue5, green5 );
     
        //Random shape generators
        int Type_Of_Shape1 = 1 + random.nextInt( 2 );
        int Type_Of_Shape2 = 1 + random.nextInt( 2 );
        int Type_Of_Shape3 = 1 + random.nextInt( 2 );
        int Type_Of_Shape4 = 1 + random.nextInt( 2 );
        int Type_Of_Shape5 = 1 + random.nextInt( 2 );
     
     
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
     
            //Random Shape & Color1
            g.setColor( Color1 );
            if( Type_Of_Shape1 == 1 )
            {
                g.fillOval( x1, y1, 50, 50 );
            }
            if( Type_Of_Shape1 == 2 )
            {
                g.fillRect( x1, y1, 50, 50 );
            }
     
            //Random Shape & Color2
            g.setColor( Color2 );
            if( Type_Of_Shape2 == 1 )
            {
                g.fillOval( x2, y2, 50, 50 );
            }
            if( Type_Of_Shape2 == 2 )
            {
                g.fillRect( x2, y2, 50, 50 );
            } 
     
            //Random Shape & Color3
            g.setColor( Color3 );
            if( Type_Of_Shape3 == 1 )
            {
                g.fillOval( x3, y3, 50, 50 );
            }
            if( Type_Of_Shape3 == 2 )
            {
                g.fillRect( x3, y3, 50, 50 );
            } 
     
            //Random Shape & Color4
            g.setColor( Color4 );
            if( Type_Of_Shape4 == 1 )
            {
                g.fillOval( x4, y4, 50, 50 );
            }
            if( Type_Of_Shape4 == 2 )
            {
                g.fillRect( x4, y4, 50, 50 );
            } 
     
            //Random Shape & Color5
            g.setColor( Color5 );
            if( Type_Of_Shape5 == 1 )
            {
                g.fillOval( x5, y5, 50, 50 );
            }
            if( Type_Of_Shape5 == 2 )
            {
                g.fillRect( x5, y5, 50, 50 );
            }         
        }
     
        public void run() 
        {
            while(isVisible()) 
            {
                try 
                {
                    Thread.sleep(15);
                } catch(InterruptedException e) 
                {
                    System.out.println("interrupted");
                }
                move();
                repaint();
            }
        }
     
        public void move() 
        {
            //Move object1 & bounce off perimeter
            if( x1 + directionX1 < 0 || x1 + diameter + directionX1 > getWidth()) 
            {
                directionX1 *= -1;          
            }
            if( y1 + directionY1 < 0 || y1 + diameter + directionY1 > getHeight()) 
            {
                directionY1 *= -1;           
            }
            x1 += directionX1;
            y1 += directionY1; 
     
            //Move object2 & bounce off perimeter
            if( x2 + directionX2 < 0 || x2 + diameter + directionX2 > getWidth()) 
            {
                directionX2 *= -1;          
            }
            if( y2 + directionY2 < 0 || y2 + diameter + directionY2 > getHeight()) 
            {
                directionY2 *= -1;           
            }
            x2 += directionX2;
            y2 += directionY2; 
     
            //Move object3 & bounce off perimeter
            if( x3 + directionX3 < 0 || x3 + diameter + directionX3 > getWidth()) 
            {
                directionX3 *= -1;          
            }
            if( y3 + directionY3 < 0 || y3 + diameter + directionY3 > getHeight()) 
            {
                directionY3 *= -1;           
            }
            x3 += directionX3;
            y3 += directionY3; 
     
            //Move object4 & bounce off perimeter
            if( x4 + directionX4 < 0 || x4 + diameter + directionX4 > getWidth()) 
            {
                directionX4 *= -1;          
            }
            if( y4 + directionY4 < 0 || y4 + diameter + directionY4 > getHeight()) 
            {
                directionY4 *= -1;           
            }
            x4 += directionX4;
            y4 += directionY4; 
     
            //Move object5 & bounce off perimeter
            if( x5 + directionX5 < 0 || x5 + diameter + directionX5 > getWidth()) 
            {
                directionX5 *= -1;          
            }
            if( y5 + directionY5 < 0 || y5 + diameter + directionY5 > getHeight()) 
            {
                directionY5 *= -1;           
            }
            x5 += directionX5;
            y5 += directionY5; 
        }
     
        private void start() 
        {
            while(!isVisible()) 
            {
                try 
                {
                    Thread.sleep( 15 );
                } 
                catch( InterruptedException e ) 
                {
                    System.out.println( "Screen Saver Closed" );
                }
            }
            Thread Thread = new Thread( this );
            Thread.start();
        }
     
        public static void main(String[] args) 
        {
            ScreenSaver test = new ScreenSaver();
            JFrame frame = new JFrame( "Screen Saver" );
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.getContentPane().add( test );
            frame.setSize( 500,500 );
            frame.setVisible( true );
            test.start();
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with object collision

    make these shapes repel in opposite directions when they collide with each other.
    Have you tried taking a piece of paper and working out the changes in x and y that controls the motions of the shapes so that their motions go in opposite directions?
    Draw the shapes at the moment of collision and write down the next x,y values for where you want them to move after the collision.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Location
    Kalispell, MT
    Posts
    2
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with object collision

    But they are moving in random directions and random speeds, how would i be able to draw them at the moment of collision, when i'm not sure where they are going to collide?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with object collision

    Can you detect when they collide?
    I assume that at the moment of collision, they both will have an x,y location.
    For design purposes, chose an x,y location for two shapes and then look at what the next x,y locations will be when they start moving away from the point of collision. How do the changes in x,y before the collision compare to the changes in x,y after the collision? You are interested in the changes in x and y, not in their absolute values.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Collision
    By risen375 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 28th, 2011, 07:02 AM
  2. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  3. Collision Detecting
    By iams3b in forum AWT / Java Swing
    Replies: 0
    Last Post: February 6th, 2011, 01:48 AM
  4. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 10:39 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

Tags for this Thread