Beispiel #1
0
        public Form1()
        {
            InitializeComponent();
            this.ClientSize             = new Size(1400, 750);
            width                       = this.ClientSize.Width;
            height                      = this.ClientSize.Height;
            masterChannel               = new ANT();
            masterChannel.UcChannelType = 0;
            ThreadStart antRefMaster    = new ThreadStart(masterChannel.startANT);
            Thread      antThreadMaster = new Thread(antRefMaster);

            antThreadMaster.Start();


            //ground = new Rectangle2D(new Point2D(600, 425), 1100, 50);
            topLine    = new Line2D(new Point2D(0, 0), new Point2D(width, 0));
            groundLine = new Line2D(new Point2D(0, height), new Point2D(width, height));

            /*
             * Obstacle1B = new Rectangle2D(new Point2D(1150, 400-25), 20, 50);
             * Obstacle1B.Velocity = new Point2D(-10, 0);
             */
            Obstacle1 = new Barrier(width + 10, 30);
            Obstacle2 = new Barrier(width + width / 3 + 10, 30);
            Obstacle3 = new Barrier(width + width * 2 / 3 + 10, 30);
            player    = new Sphere2D(new Point2D(200, 350), 25);

            updatePlayerLifes(playerLives);
        }
        public void PerformCollision(Sphere2D otherBall)
        {
            Point2D difference = this.center - otherBall.center;
            float   distance   = difference.Magnitude;

            if (distance == 0)
            {
                return;                // what were you thinking?
            }
            // minimum translation distance mtd will be used to seperate the balls
            Point2D mtd = difference * (this.radius + otherBall.Radius - distance) / distance * 1.1f;


            // get the inverse of masses
            float myMassInverse    = 1 / mass;
            float otherMassInverse = 1 / otherBall.Mass;


            // push the balls apart the minimum translation distance based on their relative mass
            Point2D center = mtd * (myMassInverse / (myMassInverse + otherMassInverse));

            this.center.X += center.X;
            this.center.Y += center.Y;
            Point2D centerotherBall = mtd * (otherMassInverse / (myMassInverse + otherMassInverse));

            otherBall.center.X -= centerotherBall.X;
            otherBall.center.Y -= centerotherBall.Y;


            // now normalize the mtd to get a unit vector in the mtd direction
            mtd /= mtd.Magnitude;;


            // impact speed
            Point2D v         = this.velocity - otherBall.Velocity;
            float   v_dot_mtd = v * mtd;

            if (float.IsNaN(v_dot_mtd))
            {
                return;                         // vn was Not A Number
            }
            // if the balls are intersecting but already moving apart - do nothing
            if (v_dot_mtd > 0)
            {
                return;
            }


            // work out the collision effect
            float i = -(1.0f + elasticity) * v_dot_mtd / (myMassInverse + otherMassInverse);


            Point2D impulse = mtd * i;


            // change the ball's velocities
            this.velocity      += impulse * myMassInverse;
            otherBall.Velocity -= impulse * otherMassInverse;
        }
Beispiel #3
0
 public int playerHitBarrier(Sphere2D player)
 {
     if (player.Center.X >= center.X - width / 2 && player.Center.X <= center.X + width / 2 &&
         player.Center.Y <= center.Y + height / 2 && player.Center.Y >= center.Y - height)
     {
         return(1);
     }
     return(0);
 }
 public int playerHitBarrier(Sphere2D player)
 {
     if ((player.Center.X + player.Radius >= obstacleA.Center.X - obstacleA.Width / 2 && player.Center.X - player.Radius <= obstacleA.Center.X + obstacleA.Width / 2 &&
          player.Center.Y - player.Radius <= obstacleA.Center.Y + obstacleA.Height / 2) || (
             player.Center.X + player.Radius >= obstacleB.Center.X - obstacleB.Width / 2 && player.Center.X - player.Radius <= obstacleB.Center.X + obstacleB.Width / 2 &&
             player.Center.Y + player.Radius >= obstacleB.Center.Y - obstacleB.Height / 2))
     {
         return(1);
     }
     return(0);
 }
Beispiel #5
0
 public Line2D(Point2D p1, Point2D p2)
 {
     this.p1     = p1;
     this.p2     = p2;
     b[0]        = new Sphere2D(new Point2D(), 0.01);
     b[1]        = new Sphere2D(new Point2D(), 0.01);
     b[0].Center = p1;
     b[1].Center = p2;
     b[0].Mass   = float.MaxValue;
     b[1].Mass   = float.MaxValue;
 }
Beispiel #6
0
        public Point2D Normal2Ball(Sphere2D ball)
        {
            Point2D v          = p2 - p1;
            Point2D ballToLine = ball.Center - p1;
            Point2D normal     = v.Normal;

            normal.Normalize();
            if (normal * ballToLine < 0)
            {
                normal *= -1;
            }
            return(normal);
        }
Beispiel #7
0
        /// <summary>
        /// return the center of the ball if it is reflected
        /// </summary>
        /// <param name="ball"></param>
        /// <returns></returns>

        public bool BallBounce(Sphere2D ball)
        {
            // determine the normal vector ball the reflection line towards the ball
            Point2D normal = Normal2Ball(ball);
            // this next line is just to be able to see the normal line
            Line2D normalLine = new Line2D(P1, P2);

            normalLine.P2 = P1 + Normal2Ball(ball) * ball.Radius;
            // we will reflect from the center of the ball so we "move" the line towards the ball by the normal a distance of radius
            Line2D aLineTemp = new Line2D(P1 + normal * ball.Radius, P2 + normal * ball.Radius);
            // where will the ball be in one step?
            Point2D ballNextStep = ball.Center + ball.Velocity;
            // calculate the path of the ball
            Line2D ballPath = new Line2D(ball.Center, ballNextStep);
            // find the point of intersection between the ball and the reflection line
            Point2D intersectionPt = aLineTemp.LineIntersectionPoint(ballPath);

            float dot = normal * ball.Velocity;

            if (b[0].IsColliding(ball))
            {
                b[0].PerformCollision(ball);
                return(true);
            }
            else if (b[1].IsColliding(ball))
            {
                b[1].PerformCollision(ball);
                return(true);
            }// if the intersection point is on the reflection line and the ball is moving towards the reflection line
            // if we don't travel as far as the intersection point, no reflection and moving towards the reflection line
            else if (ball.Velocity.Magnitude < (intersectionPt - ball.Center).Magnitude && normal * ball.Velocity < 0)
            {
                return(false);
            }
            else if (aLineTemp.Contains(intersectionPt) && normal * ball.Velocity < 0)
            {
                Line2D  reflectionLine = new Line2D(ball.Center + ball.Velocity, intersectionPt);
                Point2D velDirection   = -1 * reflectionLine.Reflection(Normal2Ball(ball));
                velDirection.Normalize();
                ball.Velocity = velDirection * ball.Velocity.Magnitude;
                ball.Center   = intersectionPt - reflectionLine.Reflection(Normal2Ball(ball)) * ball.Elasticity;
                return(true);
            }
            // else there is no reflection
            else
            {
                return(false);
            }
        }
        // Ball to Ball collision code
        public bool IsColliding(Sphere2D otherBall)
        {
            // measure the distance between the centers of the balls
            Point2D difference = this.center - otherBall.center;
            float   distance   = difference.Magnitude;

            // if distance is less than sum of radii, we collide
            if (distance < this.radius + otherBall.Radius)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }