开发者

Circle collision response

I'm working on an Android game and I need to bounce 2 circles of each other (like 2 pool balls bouncing off each other). The collision is an elastic collision, and I need to calculate only 1 circles (called a Particle in my code)开发者_开发技巧 new velocity after the collision (the other circle, called a Barrier in my code will remain stationary and will not move because of a collision).

I am using a formula that I found on Wikipedia (http://en.wikipedia.org/wiki/Elastic_collision), but my end result for the new velocity of the particle after the collision is exactly the same as the velocity before the collision?

This is def wrong but I cant see where I am going wrong. Can anyone spot where I am going wrong?

I have just used a Java program to simulate my velocities and locations for the 2 circles as I dont wanna try it in my main Android game at the moment for fear of "breaking something"

Here is what I have so far (like I mentioned, this is just a simulation in NetBeans for the moment and I will use a menthod in my Android game to keep things a bit tidier):

double randomNextDouble = (new Random()).nextDouble();
    System.out.println("Random.nextDouble: " + randomNextDouble);

    double mathPI = Math.PI * 2;
    System.out.println("Math PI: " + mathPI);

    // get a random direction for the Particle to move towards
double direction = (new Random()).nextDouble() * Math.PI * 2;
    System.out.println("Direction: " + direction);

// Then set the Particle's velocity - Increase to make Particles move faster
int velocity = 10;
System.out.println("Velocity: " + velocity);

// Then calculate the xv and the yv
// Velocity value on the x and y axis
double xv = (velocity * Math.cos(direction));
    double yv = (velocity * Math.sin(direction));
    System.out.println("\nXV: " + xv + "\nYV: " + yv);


    // Genareting a random number for the Particle and Barrier's positions on screen
    double Xmin = 0;
    double Xmax = 300;

    double Ymin = 0;
    double Ymax = 300;

    double randomNumber1 = Xmin + (int)(Math.random() * ((Xmax - Xmin) + 1));
    double randomNumber2 = Ymin + (int)(Math.random() * ((Ymax - Ymin) + 1));
    double randomNumber3 = Xmin + (int)(Math.random() * ((Xmax - Xmin) + 1));
    double randomNumber4 = Ymin + (int)(Math.random() * ((Ymax - Ymin) + 1));

    // Setting the Particle and Barrier's radius
    double particleRadius = 8;
    double barrierRadius = 16;

    // Setting up the Particle and Barrier's mass
    double particleMass = 100;
    double barrierMass = 200;

    // Assigning a random number to the Particle to simulate its position on screen
    double particleX = randomNumber1;
    double particleY = randomNumber2;
    System.out.println("\nParticle X: " + particleX + " Particle Y: " + particleY);

    // Assigning a random number to the Barrier to simulate its position on screen
    double barrierX = randomNumber3;
    double barrierY = randomNumber4;
    System.out.println("Barrier  X: " + barrierX + " Barrier  Y: " + barrierY);



    double distanceXToBarrier = barrierX - particleX;
    System.out.println("\nBarrier X - Particle X: " + distanceXToBarrier);
    double distanceYToBarrier = barrierY - particleY;
    System.out.println("Barrier Y - Particle Y: " + distanceYToBarrier);

    // Get the distance between the Particle and the Barrier
    // Used for collision detection
    double distance = Math.sqrt((distanceXToBarrier * distanceXToBarrier) + (distanceYToBarrier * distanceYToBarrier));
    System.out.println("\nDistance: " + distance);

    // Check to see if the Particle and Barrier has collided
    if (distance <= particleRadius  + barrierRadius)
    {
        System.out.println("Distance is less than 2 Radii");
    }
    else
        System.out.println("Distance is NOT less than 2 Radii");

    // Velx = (v1.u) * u + (v1 - (v1.u) * u)
    // Vely = (v1.u) * u + (v1 - (v1.u) * u)
    // Where v1 = xv and yv respectively
    // Break it into 2 equations
    // (v1.u) * u AND
    // (v1 - (v1.u) * u)
    //
    // u = normalised Vector
    // To normalize you just devide the x, y, z coords by the length of the vector. 
    // This then gives you the Unit Vector.
    //
    //Normalize the vector
    double particleXNormalized = particleX * (1.0 / distance);
    double particleYNormalized = particleY * (1.0 / distance);
    System.out.println("\nParticle X Normalised: " + particleXNormalized +
                       "\nParticle Y Normalised: " + particleYNormalized);

    // Calculating the first part of the eqaution
    // (v1.u)
    double v1DotUForX = xv * particleXNormalized;
    double v1DotUForY = yv * particleYNormalized;
    System.out.println("\nv1.u for X: " + v1DotUForX +
                       "\nv1.u for Y: " + v1DotUForY);

    // The first part of the equation
    // (v1.u) * u
    double part1X = v1DotUForX * particleXNormalized;
    double part1Y = v1DotUForY * particleYNormalized;
    System.out.println("\nPart 1 for X: " + part1X +
                       "\nPart 1 for Y: " + part1Y);

    // The second part of the equation
    // (v1 - (v1.u) * u)
    double part2X = (xv - (v1DotUForX) * particleXNormalized);
    double part2Y = (yv - (v1DotUForY) * particleYNormalized);
    System.out.println("\nPart 2 for X: " + part2X +
                       "\nPart 2 for Y: " + part2Y);




    // Solving for:
    // (((mass 1 - mass2) / (mass1 + mass2) * (v1.u) * u + ((2mass2) / (mass1 + mass2) * ((v1.u) * u))) +
    //           (v1 - (v1.u) * u))
    double newXV = ((((particleMass - barrierMass) / (particleMass + barrierMass)) * part1X) + (((2 * barrierMass) / (particleMass + barrierMass)) * part1X) + part2X);
    double newYV = ((((particleMass - barrierMass) / (particleMass + barrierMass)) * part1Y) + (((2 * barrierMass) / (particleMass + barrierMass)) * part1Y) + part2Y);
    System.out.println("\nNew XV: " + newXV + "\nNew YV: " + newYV);


Looking at your algorithm, you appear to have made errors in the implementation. Why are you normalizing the coordinates of the particle? Shouldn't you be doing that to the velocity? In the usual equations, u is velocity, not position.

And why do you give the particle a random velocity (xv, yv) that has nothing to do with the two random coordinates you set up for the particle and barrier? (Surely the velocity should be some multiple of (barrier - particle) vector?)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜