Пример #1
0
        //this should not run on next frame, or balls will return.
        //calculate the ball angle and velocity, and execute.
        public void calculateBallCollision(Ball otherBall)
        {
            double collisionTangent = Math.Atan2((double)yDist, (double)xDist); //returns the angle of the tangent of the vector which is the collision x and y distance.
            double sin = Math.Sin(collisionTangent);
            double cos = Math.Cos(collisionTangent);
            //rotate ball 0 pos
            double B0x = 0;//relative x & y pos set
            double B0y = 0;
            //set ball 1 pos relative to ball 0, aka distance.
            double B1x = xDist * cos + yDist * sin;//RELATIVE TO BALL 0!!!
            double B1y = yDist * cos - xDist * sin;
            //rotate ball 0 velocity
            double V0x = xSpeed * cos + ySpeed * sin;
            double V0y = ySpeed * cos - xSpeed * sin;
            //rotate ball 1 velocity
            double V1x = otherBall.xSpeed * cos + otherBall.ySpeed * sin;
            double V1y = otherBall.ySpeed * cos - otherBall.xSpeed * sin;

            //collision reaction ELASTISK LIGNING
            double vxtotal = V0x - V1x;
            V0x = ((mass - otherBall.getMass()) * V0x + 2 * otherBall.getMass() * V1x) / (mass + otherBall.getMass());//new velocity x ball 1
            V1x = vxtotal + V0x; //new velocity x ball 2
            //update position, THIS ONE IS RELATIVE TO MID BALL 0 and BALL 1
            B0x += V0x;
            B1x += V1x;
            //rot pos back? SET NEW POSITION. BALLS SHOULD OVERLAP AFTER THIS.
            double B0newPosx = B0x * cos - B0y * sin;
            double B0newPosy = B0y * cos + B0x * sin;

            double B1newPosx = B1x * cos - B1y * sin;
            double B1newPosy = B1y * cos + B1x * sin;

            //rot vel back
            double B0newVelx = V0x * cos - V0y * sin;
            double B0newVely = V0y * cos + V0x * sin;

            double B1newVelx = V1x * cos - V1y * sin;
            double B1newVely = V1y * cos + V1x * sin;

            //update position
            otherBall.xPos = xPos + (float)B1newPosx;//is this just to set it out of the other balls's radius
            otherBall.yPos = yPos + (float)B1newPosy;
            xPos = xPos + (float)B0newPosx;//these 4 new positions will be a little "bigger" than when they entered. this is so that they wont stick. also, they point slightly away from each other.
            yPos = yPos + (float)B0newPosy;

            //update speed
            xSpeed = (float)B0newVelx;
            ySpeed = (float)B0newVely;

            otherBall.setXspeed((float)B1newVelx);
            otherBall.setYspeed((float)B1newVely);
        }
Пример #2
0
 public void InitializeBallz()
 {
     Ballz[0] = new Ball(pad.getX(), padPosy-10, startBallspeedX, forceVal, radius, false, 0);//xPos, yPos, xSpeed,  ySpeed, radius, inPlay, gravity value
     Ballz[1] = new Ball(1000, botWall - radius, 0, 0, radius, false,gravity);//all other balls start off to the bottom right corner.
     Ballz[2] = new Ball(1000, botWall - radius, 0, 0, radius, false,gravity);
     Ballz[3] = new Ball(1000, botWall - radius, 0, 0, radius, false,gravity);
     Ballz[4] = new Ball(1000, botWall - radius, 0, 0, radius, false,gravity);
 }
Пример #3
0
 //drop a new ball from CheckCollision()
 public void DropNewBall(int b, int f)
 {
     int count = 0;//used count and while loop since the "count" must be changed when a ball is being activated, so that we don't put out more than one ball.
        while(count < Ballz.Length)
     {
         if(!Ballz[count].inPlay)//if the ball is not yet in play, we can deploy it.
         {
             Ballz[count] = new Ball(Brickz[b].getX(),Brickz[b].getY(), Ballz[f].getXspeed() , Ballz[f].getYspeed(), radius, true, gravity);//sets new balls' position(the brick), and speeds(colliding ball's speed)
             numBallsActive += 1;//keep track for update purposes
             count = Ballz.Length;//exits loop
         }
         count++;
     }
 }
Пример #4
0
        public void InitializeBallz()
        {
            Ballz[0] = new Ball(pad.getX(), padPosy-10, 0, 0, radius, true);//xPos, yPos, xSpeed,  ySpeed, radius, inPlay
            Ballz[1] = new Ball(1000, botWall - radius, 0, 0, radius, false);

            Ballz[2] = new Ball(1000, botWall - radius, 0, 0, radius, false);
            Ballz[3] = new Ball(1000, botWall - radius, 0, 0, radius, false);
            Ballz[4] = new Ball(1000, botWall - radius, 0, 0, radius, false);
        }
Пример #5
0
 //drop a new ball from CheckCollision()
 public void DropNewBall(int b, int f)
 {
     int count = 0;
        while(count < Ballz.Length)
     {
         if(!Ballz[count].inPlay)
         {
             Ballz[count] = new Ball(Brickz[b].getX(),Brickz[b].getY(), Ballz[f].getXspeed() , Ballz[f].getYspeed(), radius, true);
             numBallsActive += 1;
             count = Ballz.Length;
         }
         count++;
     }
 }
Пример #6
0
        //this should not run on next frame, or balls will return.
        //calculate the ball angle and velocity, and execute.
        public void calculateBallCollision(Ball otherBall)
        {
            double collisionTangent = Math.Atan2((double)yDist, (double)xDist); //returns the angle of the tangent of the vector which is the collision x and y distance.
            double sin = Math.Sin(collisionTangent);
            double cos = Math.Cos(collisionTangent);
            //rotate ball 0 pos
            double B0x = 0;//relative x & y pos set
            double B0y = 0;
            //set ball 1 pos relative to ball 0, aka distance.
            double B1x = xDist * cos + yDist * sin;//RELATIVE TO BALL 0!!!
            double B1y = yDist * cos - xDist * sin;
            //rotate ball 0 velocity
            double V0x = xSpeed * cos + ySpeed * sin;
            double V0y = ySpeed * cos - xSpeed * sin;
            //rotate ball 1 velocity
            double V1x = otherBall.xSpeed * cos + otherBall.ySpeed * sin;
            double V1y = otherBall.ySpeed * cos - otherBall.xSpeed * sin;

            //collision reaction ELASTISK LIGNING I BOKA?, tror denne gjør at de ikke setter seg fast, må plusse på noe ekstra? eller ikke siden det er vel.
            double vxtotal = V0x - V1x;
            V0x = ((mass - otherBall.getMass()) * V0x + 2 * otherBall.getMass() * V1x) / (mass + otherBall.getMass());//new velocity x ball 1
            V1x = vxtotal + V0x; //new velocity x ball 2
            //update position, THIS ONE IS RELATIVE TO MID BALL 0 and BALL 1
            B0x += V0x;
            B1x += V1x;
            //rot pos back? SET NEW POSITION. BALLS SHOULD OVERLAP AFTER THIS.
            double B0newPosx = B0x * cos - B0y * sin;
            double B0newPosy = B0y * cos + B0x * sin;

            double B1newPosx = B1x * cos - B1y * sin;
            double B1newPosy = B1y * cos + B1x * sin;

            //rot vel back?
            double B0newVelx = V0x * cos - V0y * sin;
            double B0newVely = V0y * cos + V0x * sin;

            double B1newVelx = V1x * cos - V1y * sin;
            double B1newVely = V1y * cos + V1x * sin;

            //update pos
            otherBall.xPos = xPos + (float)B1newPosx;//is this just to set it out of the other balls radius?
            otherBall.yPos = yPos + (float)B1newPosy;
            xPos = xPos + (float)B0newPosx;//these 4 new positions will be a little "bigger" than when they entered. this is so that they wont stick. also, they point slightly away from each other.
            yPos = yPos + (float)B0newPosy;

            //update vel - I WANT THEM TO HAVE PERMANENT SPEEDS if not, I can rearrange the code again.
            xSpeed = (float)B0newVelx > 0 ? 2 : -2;
            //xSpeed = (float)B0newVelx;
            ySpeed = (float)B0newVely > 0 ? 2 : -2;
            //ySpeed = (float)B0newVely;
            otherBall.setXspeed((float)B1newVelx > 0 ? 2 : -2);
            //otherBall.setXspeed((float)B1newVelx);
            otherBall.setYspeed((float)B1newVely > 0 ? 2 : -2);
            //otherBall.setYspeed((float)B1newVely);

               /* //old velocity
            double V0Ball1x = xSpeed * cos;
            double V0Ball1y = ySpeed * sin;
            double V0Ball2x = otherBall.getXspeed() * cos;
            double V0Ball2y = otherBall.getYspeed() * sin;

            //new velocity
            double V1Ball1x = ((mass - otherBall.getMass()) / (mass + otherBall.getMass())) * V0Ball1x + (2 * otherBall.getMass() / (mass + otherBall.getMass())) * V0Ball2x;
            double V1Ball1y = (2 * mass / (mass + otherBall.getMass())) * V0Ball1x + ((otherBall.getMass() - mass) / (otherBall.getMass() + mass)) * V0Ball2x;
            double V1Ball2x = V0Ball1y;
            double V1Ball2y = V0Ball2y;

            //set new velocity
            xSpeed = (float)V1Ball1x;
            ySpeed = (float)V1Ball1y;
            xPos += xSpeed *3;
            yPos += ySpeed *3;
            otherBall.setXspeed((float)V1Ball2x);
            otherBall.setYspeed((float)V1Ball2y);
            otherBall.xPos += otherBall.xSpeed * 5;
            otherBall.yPos += otherBall.ySpeed *5;*/
        }
Пример #7
0
        //check if balls collide
        public bool checkBallCollision(Ball otherBall)
        {
            xDist = otherBall.getX()-xPos;//dx
            yDist = otherBall.getY()-yPos;//dy

            float radDist = radius + otherBall.getRadius();

            if (radDist * radDist >= (xDist * xDist + yDist * yDist) && inPlay == true && otherBall.inPlay == true) //check if distance is bigger than the balls touching range
            {
                return true;
            }
            else
            {
                return false;
            }
        }