You are viewing our Forum Archives. To view or take place in current topics click here.
[Java] Assistance with elastic collision responses
Posted:

[Java] Assistance with elastic collision responsesPosted:

ip
  • Gold Gifter
Status: Offline
Joined: Dec 30, 201211Year Member
Posts: 3,778
Reputation Power: 3016
Status: Offline
Joined: Dec 30, 201211Year Member
Posts: 3,778
Reputation Power: 3016
I've been looking at it for about six hours now and I am still stuck. I've got to the point to where I can check and see if the circles are colliding (distance formula) and have their velocities multiplied by -1 to reverse themselves. It looks very unrealistic and I don't want that. I have found a few websites that describe the equations but from the way they are describing them, it instantly makes my mind go blank. Such as this website...
[ Register or Signin to view external links. ]


I would just like some assistance explaining what the variables are and how they work and such as I have never taken Physics. Any help would be greatly appreciated. Below will be the code that I have right now. (Don't mind the class stuff)

/**
 * @(#)Collision.java
 *
 *
 * @author
 * @version 1.00 2015/4/1
 */

import java.awt.*;
import java.util.*;

public class Collision extends java.applet.Applet {
   
    /** Initialization method that will be called after the applet is loaded
     *  into the browser.
     */
    public void init() {
        // TODO start asynchronous download of heavy resources
    }
      CollisionShape cs = new CollisionShape(200, 200, 50);
        CollisionShape ct = new CollisionShape(800, 400, 100);
    public void paint(Graphics g) {
       
        while (2 != 1){
           cs.drawShape(g);
           ct.drawShape(g);
           
           // draw line connecting both circles
           //g.drawLine(cs.getX(), cs.getY(), ct.getX(), ct.getY());
           //draw box
           //g.drawLine(cs.getX() - 50, cs.getY() - 50, cs.getX() + 50, cs.getY() - 50);  // top
           //g.drawLine(cs.getX() + 50, cs.getY() - 50, cs.getX() + 50, cs.getY() + 50);  // r side
           //g.drawLine(cs.getX() + 50, cs.getY() + 50, cs.getX() - 50, cs.getY() + 50);  // bottom
           //g.drawLine(cs.getX() - 50, cs.getY() + 50, cs.getX() - 50, cs.getY() - 50);
           
           // draw collision points
           getCollisionPoints(g);
           
           cs.delay(10);
           
           g.setColor(Color.white);
           g.fillRect(0, 0, 1000, 650);
           
           if (Math.pow((100 + 50), 2) > Math.pow((cs.getX() - ct.getX()), 2) + Math.pow((cs.getY() - ct.getY()), 2)){
              cs.setxVelocity(cs.getxVelocity() * -1);
              cs.setyVelocity(cs.getyVelocity() * -1);
              
              ct.setxVelocity(ct.getxVelocity() * -1);
              ct.setyVelocity(ct.getyVelocity() * -1);
           }
           
           cs.bounceMove();
           ct.bounceMove();
        }
    }
    public void getCollisionPoints(Graphics g){
       if (Math.pow((100 + 50), 2) > Math.pow((cs.getX() - ct.getX()), 2) + Math.pow((cs.getY() - ct.getY()), 2)){
          int collisionPointX = ((cs.getX() * 100) + (ct.getX() * 50)) / (50 + 100);
          int collisionPointY = ((cs.getY() * 100) + (ct.getY() * 50)) / (50 + 100);
          g.setColor(Color.blue);
          g.fillOval(collisionPointX, collisionPointY, 25, 25);   
       }
    }
}
#2. Posted:
Clunge
  • 2 Million
Status: Offline
Joined: Dec 31, 200914Year Member
Posts: 1,095
Reputation Power: 202
Status: Offline
Joined: Dec 31, 200914Year Member
Posts: 1,095
Reputation Power: 202
Liability wrote I've been looking at it for about six hours now and I am still stuck. I've got to the point to where I can check and see if the circles are colliding (distance formula) and have their velocities multiplied by -1 to reverse themselves. It looks very unrealistic and I don't want that. I have found a few websites that describe the equations but from the way they are describing them, it instantly makes my mind go blank. Such as this website...
[ Register or Signin to view external links. ]


I would just like some assistance explaining what the variables are and how they work and such as I have never taken Physics. Any help would be greatly appreciated. Below will be the code that I have right now. (Don't mind the class stuff)

/**
 * @(#)Collision.java
 *
 *
 * @author
 * @version 1.00 2015/4/1
 */

import java.awt.*;
import java.util.*;

public class Collision extends java.applet.Applet {
   
    /** Initialization method that will be called after the applet is loaded
     *  into the browser.
     */
    public void init() {
        // TODO start asynchronous download of heavy resources
    }
      CollisionShape cs = new CollisionShape(200, 200, 50);
        CollisionShape ct = new CollisionShape(800, 400, 100);
    public void paint(Graphics g) {
       
        while (2 != 1){
           cs.drawShape(g);
           ct.drawShape(g);
           
           // draw line connecting both circles
           //g.drawLine(cs.getX(), cs.getY(), ct.getX(), ct.getY());
           //draw box
           //g.drawLine(cs.getX() - 50, cs.getY() - 50, cs.getX() + 50, cs.getY() - 50);  // top
           //g.drawLine(cs.getX() + 50, cs.getY() - 50, cs.getX() + 50, cs.getY() + 50);  // r side
           //g.drawLine(cs.getX() + 50, cs.getY() + 50, cs.getX() - 50, cs.getY() + 50);  // bottom
           //g.drawLine(cs.getX() - 50, cs.getY() + 50, cs.getX() - 50, cs.getY() - 50);
           
           // draw collision points
           getCollisionPoints(g);
           
           cs.delay(10);
           
           g.setColor(Color.white);
           g.fillRect(0, 0, 1000, 650);
           
           if (Math.pow((100 + 50), 2) > Math.pow((cs.getX() - ct.getX()), 2) + Math.pow((cs.getY() - ct.getY()), 2)){
              cs.setxVelocity(cs.getxVelocity() * -1);
              cs.setyVelocity(cs.getyVelocity() * -1);
              
              ct.setxVelocity(ct.getxVelocity() * -1);
              ct.setyVelocity(ct.getyVelocity() * -1);
           }
           
           cs.bounceMove();
           ct.bounceMove();
        }
    }
    public void getCollisionPoints(Graphics g){
       if (Math.pow((100 + 50), 2) > Math.pow((cs.getX() - ct.getX()), 2) + Math.pow((cs.getY() - ct.getY()), 2)){
          int collisionPointX = ((cs.getX() * 100) + (ct.getX() * 50)) / (50 + 100);
          int collisionPointY = ((cs.getY() * 100) + (ct.getY() * 50)) / (50 + 100);
          g.setColor(Color.blue);
          g.fillOval(collisionPointX, collisionPointY, 25, 25);   
       }
    }
}


Forgive me if I am wrong, but I believe if you track the x and y coordinates of each shape there is an easy way to check if the coordinates of each object overlap, some googling will probably help because this is something I previously implemented but was a long time ago.

Alternatively, you could implement a bounding box around each shape and then detect the collision of these bounding boxes and this can be quite a bit easier.

Sorry for the ambiguous answer, I have not programmed in Java for a long time but if you have any questions feel free to PM me and I can try and help
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.