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

Thread: Collision question

  1. #1
    Junior Member
    Join Date
    Jul 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Collision question

    The code doesn't work, when I launch the code the balls are shown but they do not bounce of the side of the screen. I would appreciate it if someone could also help me create a code for each balls colliding with each other e.g when the balls hit each other they bounce off each other.

    float [] x={0,100,400,0,300,50,800};
    float [] y={100,200,500,50,0,30,600};
    float [] dx={(3),(4),(6),(8),(10), (2), (5)};
    float [] dy={ (5), (5), (3), (5), (1), (6), (8)};
    float [] xx={3,4,6,8,10,2,5};
    float [] yy={5,5,3,5,1,6,8};
    float ballSize;
     
    void setup() {
    size(1920, 1080);
    for (int i=0; i<=6; i++) {
    xx[i]=xx[i]*2;
    yy[i]=yy[i]*2;
    }
    }
     
    void draw() {
    noStroke();
    ballSize= 60;
    fill(0, 10);
    rect(0,0, width, height);
    fill(255);
    ellipse(x[0],y[0], ballSize, ballSize);
    ellipse(x[1],y[1], ballSize, ballSize);
    ellipse(x[2],y[2], ballSize, ballSize);
    ellipse(x[3],y[3], ballSize, ballSize);
    ellipse(x[4],y[4], ballSize, ballSize);
    ellipse(x[5],y[5], ballSize, ballSize);
    ellipse(x[6],y[6], ballSize, ballSize);
     
     
    for(int i=0; i<=6; i++) {
    x[i]+=(dx[i]);
    y[i]+=(dy[i]);
    xx[i]+=0.0001;
    yy[i]+=0.0001;
    }
    for (int i=0; i<=6; i++) {
    if(x[i]>=width-50) {
    dx[i]=(-xx[i]);
    }
    }
    for (int i=0; i<6; i++) {
    if (y[i]>=height-50) {
    dy[i]=(yy[i]);
    }
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Collision question

    Where in the code are the tests for collision with each of the 4 sides?

    Note: The posted code has lost all of its indentations making it hard to read. Please post properly indented code.

    Also the posted code can not be copied for testing - it is missing statements like imports, class declaration and main method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. game development- collision detection question
    By david37370 in forum Collections and Generics
    Replies: 11
    Last Post: July 21st, 2013, 12:54 PM
  2. Why isn't my collision working?
    By dan0194 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 5th, 2013, 01:57 AM
  3. collision
    By game06 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 11th, 2013, 02:02 AM
  4. Help with collision!
    By Dr.Code in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 4th, 2012, 04:09 PM
  5. Collision
    By risen375 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 28th, 2011, 07:02 AM