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 doesn't work

  1. #1
    Junior Member
    Join Date
    Dec 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Collision doesn't work

    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,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Collision doesn't work

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    they do not bounce of the side of the screen.
    What statements should control that? How are you trying to debug the code to see why it is not doing what you want it to do?
    Add some print statements that print the values of the variables that control the object's location so you can see what the computer sees when it executes.
    Something like this:
      System.out.println("var1="+var1+", var2="+var2); //  show values
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Code doesn't work
    By Nicken99 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2014, 04:24 AM
  2. Need Urgent he-lp - Phys2D - Bodies doesn't bounce back after collision...
    By digital.optiplex in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 20th, 2013, 03:53 AM
  3. [SOLVED] .equals doesn't work?
    By Purple01 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 14th, 2012, 04:20 AM
  4. Why doesn't this work!
    By Alex-Green in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2012, 04:25 AM
  5. Why doesn't this work?
    By mailman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 11:19 PM