Exemplo n.º 1
0
 public static collision_t isColliding(ball_t ball)
 {
     foreach (ball_t registeredBall in registry)
     {
         if (registeredBall != ball && registeredBall.shape.GetGlobalBounds().Intersects(ball.shape.GetGlobalBounds()))
         {
             /*
              * // conservation of momentum: m*v = M, net momentum is conserved through collision
              * // (r.m*r.vx)/b.m == b.vx
              * // (r.m*r.vy)/b.m == b.vy
              * // where r is object we're colliding with and b is us, m is maass and v is velocity
              * Vector2f calcVelocity = new Vector2f();
              * calcVelocity.X = (registeredBall.shape.Radius * registeredBall.velocity.X) / ball.shape.Radius;
              * calcVelocity.Y = (registeredBall.shape.Radius * registeredBall.velocity.Y) / ball.shape.Radius;
              * return new collision_t(true, calcVelocity);
              */
             float forceCalc = 10 / ((ball.shape.Radius * TEMP_vecLen(ball.velocity) > registeredBall.shape.Radius * TEMP_vecLen(registeredBall.velocity)) ? (ball.shape.Radius * TEMP_vecLen(ball.velocity)) : registeredBall.shape.Radius * TEMP_vecLen(registeredBall.velocity));
             return(new collision_t(true, forceCalc));
         }
     }
     return(new collision_t(false, 1f));
 }
Exemplo n.º 2
0
        private void RunPhysics(Vector2f forces)
        {
            //temp place for collision stuff
            Vector2f center           = new Vector2f(this.shape.Position.X + (this.shape.GetLocalBounds().Width / 2), this.shape.Position.Y + (this.shape.GetLocalBounds().Height / 2));
            bool     isValidPositionX = ((center.X > (1280 - (this.shape.GetLocalBounds().Width / 2)) || (center.X < (0 + (this.shape.GetLocalBounds().Width / 2)))));
            bool     isValidPositionY = ((center.Y > (720 - (this.shape.GetLocalBounds().Height / 2))) || center.Y < (0 + (this.shape.GetLocalBounds().Height / 2)));

            if ((isValidPositionX || isValidPositionY) || collisionHandler.isColliding(this).colliding)
            {
                //this.shape.Position = new Vector2f(1280 / 2, 720 / 2);
                this.velocity.X     *= isValidPositionX ? -1f : 1f;
                this.velocity.Y     *= isValidPositionY ? -1f : 1f;
                this.velocity       /= 2f;
                this.shape.Position += new Vector2f(0, -.1F);
                this.shape.Position  = new Vector2f(Math.Clamp(this.shape.Position.X, 0, 1280), Math.Clamp(this.shape.Position.Y, 0, 720));
                //Console.WriteLine("COLLISION: "+this.velocity.Y.ToString());
            }
            Vector2f oldPosition = this.shape.Position;

            this.shape.Position += this.velocity;
            ball_t dummy = this;

            dummy.shape.Position = this.shape.Position;
            collision_t simCollision = collisionHandler.isColliding(dummy);

            if (simCollision.colliding)
            {
                this.shape.Position = oldPosition;
                this.velocity      *= -1;
                this.velocity      /= 2f;
                //this.velocity *= simCollision.force;
            }
            if ((Math.Abs(this.velocity.X) < 1) && (Math.Abs(this.velocity.Y) < 1))
            {
                this.velocity += forces;
            }
        }