void AddCollisionPoint(double x, double y, double sideLength)
        {
            CollisionPoint tempcp = new CollisionPoint();

            tempcp.Load(x, y, sideLength);
            cplist.Add(tempcp);
        }
Esempio n. 2
0
 public EPAOutput(
     double compenetrationDistance,
     CollisionPoint collisionPoint)
 {
     CompenetrationDistance = compenetrationDistance;
     CollisionPoint         = collisionPoint;
 }
Esempio n. 3
0
    public Player(float x, float y, MainGame tempGame, Hub _tempHub) : base("Sprites/Packy.png", 6, 18)
    {
        SetXY(x, y);
        _position = new Vec2(x, y);

        SetMoveSpeed(5);

        AnimationDrawsBetweenFrames = 3;
        Step        = 0;
        _pointUP    = new CollisionPoint("PointHorizontal.png", this.width / 2, SPEED);
        _pointDown  = new CollisionPoint("PointHorizontal.png", this.width / 2, this.height + SPEED);
        _pointLeft  = new CollisionPoint("PointVertical.png", SPEED, this.height / 2 + SPEED);
        _pointRight = new CollisionPoint("PointVertical.png", this.width - SPEED, this.height / 2 + SPEED);
        AddChild(_pointUP);
        AddChild(_pointDown);
        AddChild(_pointRight);
        AddChild(_pointLeft);

        SetFrame(0);

        _game = tempGame;

        _activityIsLoading = false;

        _hub = _tempHub;

        _dance = false;
    }
Esempio n. 4
0
        private void SubStep(int step, CustomPoint3D startPoint)
        {
            var gamma1 = Rnd(double.Epsilon, 1);

            FreePathLength.Add(-Math.Log(gamma1) / _sigmaS);
            var gamma2x = Rnd(0, 1);
            var gamma2y = Rnd(0, 1);
            //var gamma2z = Rnd(0, 1);
            //var wz = 1 - 2 * gamma2z;
            //var tmp2 = Math.Sqrt(1 - Math.Pow(cosZ, 2));
            //var wx = tmp2 * Math.Cos(TwoPi * gamma2x);
            //var vy = tmp2 * Math.Sin(TwoPi * gamma2y);
            var wx = Math.Cos(TwoPi * gamma2x);
            var wy = Math.Sin(TwoPi * gamma2y);
            var wz = 0;

            GuidedCos.Add(new Vector3D(wx, wy, wz));
            CollisionPoint.Add(new CustomPoint3D(
                                   startPoint.X + GuidedCos[step].X * FreePathLength[step],
                                   startPoint.Y + GuidedCos[step].Y * FreePathLength[step],
                                   startPoint.Z + GuidedCos[step].Z * FreePathLength[step]));
            PathLength += FreePathLength[step];
            var gamma3 = Rnd(0, 1);

            if (gamma3 <= _sigmaA / _sigmaT)
            {
                IsAbsorbed = true;
            }

            //Console.WriteLine(@"FreePathLength: {0};	wx: {1};	wy: {2};wz: {3};	gamma3: {4};	SigmaA: {5};	SigmaTr: {6};	{7}", FreePathLength[step], wx, wy, wz, gamma3, _sigmaA, _sigmaTr, _sigmaA/_sigmaTr);
        }
Esempio n. 5
0
		public EPAOutput (
			double compenetrationDistance,
			CollisionPoint collisionPoint)
		{
			CompenetrationDistance = compenetrationDistance;
			CollisionPoint = collisionPoint;
		}
Esempio n. 6
0
 public CollisionLine()
 {
     cp1 = new CollisionPoint();
     cp2 = new CollisionPoint();
     p1  = new PointD();
     p2  = new PointD();
     SetColor(255, 230, 175, 50);    // set line color
 }
        CollisionPoint CreateCollisionPoint(double x, double y, double boxSize, double radius, double rotation, Color c)
        {
            PointD cppos = new PointD();

            cppos.X = (x + radius * Math.Cos(rotation));
            cppos.Y = (y + radius * Math.Sin(rotation));
            CollisionPoint cp = new CollisionPoint(x, y, boxSize, boxSize, c);

            return(cp);
        }
Esempio n. 8
0
 // Use this for initialization
 void Start()
 {
     // Generate X coordinate random
     this.transform.parent = null;
     _direction = new Vector3(0.0f, -0.1f, -0f);
     _ball = this.gameObject;
     _ballTransform = this.gameObject.transform;
     _collisionPoint = this.GetComponent("CollisionPoint") as CollisionPoint;
     
 }
Esempio n. 9
0
    public CollisionPoint AddCollidingObject(GameObject[] objects, Vector2 point, bool isBorder)
    {
        CollisionPoint collisionPoint = new CollisionPoint();

        collisionPoint.objects  = objects;
        collisionPoint.point    = point;
        collisionPoint.isBorder = isBorder;
        collisionPoints.Add(collisionPoint);
        return(collisionPoint);
    }
Esempio n. 10
0
        public void Deflect(CollisionPoint c, Direction d)
        {
            const int max = 5;

            ChangeDirection = new TimeSpan(0, 0, Arkanoid.Random.Next(1, max));
            var nd = GetRandomDirection();

            while (nd == Direction)
            {
                nd = GetRandomDirection();
            }
            Direction = nd;
        }
Esempio n. 11
0
        public double?GetTimeOfImpact(
            IShape shapeA,
            IShape shapeB,
            double timeStep)
        {
            SaveBaseData(shapeA, shapeB);

            // relative linear velocity
            Vector3d rLinearVelocity    = shapeB.LinearVelocity - shapeA.LinearVelocity;
            double   maxAngularVelocity = shapeA.AngularVelocity.Length() * shapeA.FarthestPoint.Length() +
                                          shapeB.AngularVelocity.Length() * shapeB.FarthestPoint.Length();

            double         radius         = 1E-4;
            double         t              = 0.0;
            CollisionPoint collisionPoint = GetDistance(shapeA, shapeB);

            if (collisionPoint.Intersection)
            {
                return(0.0);
            }

            while (collisionPoint.Distance > radius &&
                   !collisionPoint.Intersection)
            {
                double nLinear = rLinearVelocity.Dot(-1.0 * collisionPoint.CollisionNormal);
                double relDist = nLinear + maxAngularVelocity;

                t += collisionPoint.Distance / relDist;

                if (t < 0.0 || t > timeStep)
                {
                    // never hit during this timestep
                    RestoreBaseData(shapeA, shapeB);
                    return(null);
                }

                integratePosition.IntegrateObjectPosition(shapeA, t);
                integratePosition.IntegrateObjectPosition(shapeB, t);

                collisionPoint = GetDistance(shapeA, shapeB);

                if (collisionPoint == null)
                {
                    break;
                }
            }

            RestoreBaseData(shapeA, shapeB);

            return(t);
        }
Esempio n. 12
0
        private static Fraction Best(List <CollisionPoint> potentials, Car[] cars, int considering)
        {
            if (considering >= potentials.Count)
            {
                return(potentials[potentials.Count - 1].Time + (Fraction)1);
            }
            CollisionPoint point = potentials[considering];

            if (point.FastCar.RightLane != point.SlowCar.RightLane)
            {
                Fraction first = Best(potentials, cars, considering + 1);
                if (CanSwap(cars, point.FastCar, point.Time) && CanSwap(cars, point.SlowCar, point.Time))
                {
                    point.FastCar.RightLane = !point.FastCar.RightLane;
                    point.SlowCar.RightLane = !point.SlowCar.RightLane;
                    Fraction second = Best(potentials, cars, considering + 1);
                    point.FastCar.RightLane = !point.FastCar.RightLane;
                    point.SlowCar.RightLane = !point.SlowCar.RightLane;
                    if (first < second)
                    {
                        return(second);
                    }
                }
                return(first);
            }
            else
            {
                Fraction best = point.Time;
                if (CanSwap(cars, point.FastCar, point.Time))
                {
                    point.FastCar.RightLane = !point.FastCar.RightLane;
                    Fraction second = Best(potentials, cars, considering + 1);
                    point.FastCar.RightLane = !point.FastCar.RightLane;
                    if (second > best)
                    {
                        best = second;
                    }
                }
                if (CanSwap(cars, point.SlowCar, point.Time))
                {
                    point.SlowCar.RightLane = !point.SlowCar.RightLane;
                    Fraction second = Best(potentials, cars, considering + 1);
                    point.SlowCar.RightLane = !point.SlowCar.RightLane;
                    if (second > best)
                    {
                        best = second;
                    }
                }
                return(best);
            }
        }
Esempio n. 13
0
 protected void UpdateListBoxGearPoints()
 {
     listBoxGearPoints.Items.Clear();
     if (gearPoints != null)
     {
         for (int i = 0; i < gearPoints.Count; i++)
         {
             CollisionPoint colPt = new CollisionPoint();
             colPt.Index  = i;
             colPt.Vector = gearPoints[i];
             listBoxGearPoints.Items.Add(colPt);
         }
     }
 }
Esempio n. 14
0
 private void buttonDeleteFloatPoint_Click(object sender, EventArgs e)
 {
     if (listBoxFloatPoints.SelectedIndex > -1)
     {
         CollisionPoint point = listBoxFloatPoints.Items[listBoxFloatPoints.SelectedIndex] as CollisionPoint;
         if (point != null)
         {
             floatPoints.RemoveAt(point.Index);
             ModelControl.AirplaneModel.AirplaneControl.AircraftParameters.FloatPoints.RemoveAt(point.Index);
             ModelControl.AirplaneModel.AirplaneControl.AircraftParameters.UnscaledFloatPoints.RemoveAt(point.Index);
             Program.Instance.CollisionPointsUpdated();
             UpdateListBoxFloatPoints();
         }
     }
 }
Esempio n. 15
0
        public void DeflectBallFromBrick(Brick brick, CollisionPoint cx, Direction dx)
        {
            Direction      d;
            CollisionPoint c;

            while (Collisions.IsCollision(this, brick, out d, out c))
            {
                switch (d)
                {
                case Direction.Up:
                    Location += new Vector2(Motion.X * BallSpeed, Math.Abs(Motion.Y * BallSpeed));
                    break;

                case Direction.Down:
                    Location += new Vector2(Motion.X * BallSpeed, -1 * Math.Abs(Motion.Y * BallSpeed));
                    break;

                case Direction.Left:
                    Location += new Vector2(Math.Abs(Motion.X * BallSpeed), Motion.Y * BallSpeed);
                    break;

                case Direction.Right:
                    Location += new Vector2(-1 * Math.Abs(Motion.X * BallSpeed), Motion.Y * BallSpeed);
                    break;

                case Direction.DownLeft:
                    Location += new Vector2(Math.Abs(Motion.X * BallSpeed), -1 * Math.Abs(Motion.Y * BallSpeed));
                    break;

                case Direction.DownRight:
                    Location += new Vector2(-1 * Math.Abs(Motion.X * BallSpeed), -1 * Math.Abs(Motion.Y * BallSpeed));
                    break;

                case Direction.UpLeft:
                    Location += new Vector2(Math.Abs(Motion.X * BallSpeed), Math.Abs(Motion.Y * BallSpeed));
                    break;

                case Direction.UpRight:
                    Location += new Vector2(-1 * Math.Abs(Motion.X * BallSpeed), Math.Abs(Motion.Y * BallSpeed));
                    break;

                case Direction.Stop:
                    break;
                }
            }

            DeflectBrick(cx, dx, brick);
        }
Esempio n. 16
0
        public void CrearColisiones()
        {
            Collision.AddCollisionSegment(new Point3D(-3.02f, -1.02f, 0), new Point3D(2.21f, -1.30f, 0), 0.5f);//torno

            CollisionPoint c = new CollisionPoint();

            c.ColitionDistance = 2;
            c.enabled          = true;
            c.point            = new Point3D(-14.59f, -0.68f, 0);

            Collision.AddCollisionPoint(c);

            Collision.AddCollisionSegment(new Point3D(-21.98f, 34.08f, 0), new Point3D(-22.27f, -14.35f, 0), 1); //pared lateral

            Collision.AddCollisionSegment(new Point3D(-22.27f, -14.35f, 0), new Point3D(14.28f, -14.85f, 0), 1); // pared del ventilador
        }
		public CollisionPointStructure (
			int objectA,
			int objectB,
			bool intersection,
			double objectDistance,
			CollisionPoint collisionPoint,
			CollisionPoint[] collisionPoints)
		{
			ObjectA = objectA;
			ObjectB = objectB;
			ObjectDistance = objectDistance;
			Intersection = intersection;
			CollisionPoint = collisionPoint;
			CollisionPoints = collisionPoints;
            FrameCount = 0;
		}
Esempio n. 18
0
 private void vectorControlCollisionPoint_VectorChanged(object sender, EventArgs e)
 {
     if (listBoxPoints.SelectedIndex > -1)
     {
         collisionPoints[listBoxPoints.SelectedIndex] = vectorControlCollisionPoint.Vector;
         CollisionPoint colPt = listBoxPoints.Items[listBoxPoints.SelectedIndex] as CollisionPoint;
         if (colPt != null)
         {
             colPt.Vector = vectorControlCollisionPoint.Vector;
             listBoxPoints.Items[listBoxPoints.SelectedIndex] = colPt;
         }
         ModelControl.AirplaneModel.AirplaneControl.AircraftParameters.CollisionPoints[listBoxPoints.SelectedIndex] =
             FlightModelWind.ToModel(vectorControlCollisionPoint.Vector);
         ModelControl.AirplaneModel.AirplaneControl.AircraftParameters.UnscaledCollisionPoints[listBoxPoints.SelectedIndex] =
             FlightModelWind.ToModel(vectorControlCollisionPoint.Vector) * (1f / ModelControl.AirplaneModel.AirplaneControl.AircraftParameters.Scale);
         Program.Instance.CollisionPointsMoved();
     }
 }
Esempio n. 19
0
 private void buttonCloneFloatPoint_Click(object sender, EventArgs e)
 {
     if (listBoxFloatPoints.SelectedIndex > -1)
     {
         CollisionPoint point = listBoxFloatPoints.Items[listBoxFloatPoints.SelectedIndex] as CollisionPoint;
         if (point != null)
         {
             floatPoints.Add(new Vector3(-point.Vector.X, point.Vector.Y, point.Vector.Z));
             ModelControl.AirplaneModel.AirplaneControl.AircraftParameters.FloatPoints.Add(
                 FlightModelWind.ToModel(new Vector3(-point.Vector.X, point.Vector.Y, point.Vector.Z)));
             ModelControl.AirplaneModel.AirplaneControl.AircraftParameters.UnscaledFloatPoints.Add(
                 FlightModelWind.ToModel(new Vector3(-point.Vector.X, point.Vector.Y, point.Vector.Z) *
                                         (1f / ModelControl.AirplaneModel.AirplaneControl.AircraftParameters.Scale)));
             Program.Instance.CollisionPointsUpdated();
             UpdateListBoxFloatPoints();
         }
     }
 }
Esempio n. 20
0
    CollisionPoint findCPSphereOnLine(Vector3 spherePosition, float sphereRadius, Vector3 lineStart, Vector3 lineEnd)
    {
        Vector3 relativeSpherePos = spherePosition - lineStart;
        Vector3 line = lineEnd - lineStart;

        float closestPointOnLine = Vector3.Dot(relativeSpherePos, line.normalized);

        closestPointOnLine = Mathf.Clamp(closestPointOnLine, 0, line.magnitude);

        CollisionPoint collision = new CollisionPoint(false, lineStart + (closestPointOnLine * line.normalized));

        collision.normal = (spherePosition - collision.position).normalized;

        if ((spherePosition - collision.position).magnitude <= sphereRadius)
        {
            collision.occurred = true;
        }

        return(collision);
    }
Esempio n. 21
0
    void sphereOnPlane(Transform sphereTransform, Transform planeTransform, Physics spherePhysics, Physics planePhysics)
    {
        Matrix4x4 rotation = Matrix4x4.Rotate(Quaternion.Euler(planeTransform.eulerAngles));

        Vector3 planeNormal = rotation.MultiplyVector(new Vector3(0, 1, 0)).normalized;
        Vector3 planeX      = rotation.MultiplyVector(new Vector3(1, 0, 0)).normalized *planeTransform.localScale.x * 5;
        Vector3 planeZ      = rotation.MultiplyVector(new Vector3(0, 0, 1)).normalized *planeTransform.localScale.z * 5;

        Plane          plane     = new Plane(planeTransform.position, planeNormal, planeX, planeZ);
        CollisionPoint collision = findCPSphereOnPlane(sphereTransform.position, spherePhysics.m_sphereRadius, plane);

        if (!collision.occurred)
        {
            return;
        }

        Vector3 pointToCentre = sphereTransform.position - collision.position;

        float vSphere     = Vector3.Dot(spherePhysics.m_velocity, pointToCentre.normalized);
        float vPlane      = Vector3.Dot(planePhysics.m_velocity, pointToCentre.normalized);
        float netMovement = vPlane - vSphere;

        if (netMovement > m_contactSpeed) //Moving towards so collision should occur
        {
            float speedSphere = speedAfterCollision(spherePhysics.m_restitution * planePhysics.m_restitution, vSphere, vPlane, spherePhysics.m_mass, planePhysics.m_mass);
            spherePhysics.m_velocity += (speedSphere - vSphere) * pointToCentre.normalized;

            float speedPlane = speedAfterCollision(spherePhysics.m_restitution * planePhysics.m_restitution, vPlane, vSphere, planePhysics.m_mass, spherePhysics.m_mass);
            planePhysics.m_velocity += (speedPlane - vPlane) * pointToCentre.normalized;
        }
        else if (netMovement > -m_contactSpeed / 2) //In contact
        {
            if (spherePhysics.m_mass != 0)
            {
                spherePhysics.m_velocity += netMovement * pointToCentre.normalized;
                sphereTransform.position  = pointToCentre.normalized * spherePhysics.m_sphereRadius + collision.position;
            }
        }
        //Else they're moving away from each other
    }
Esempio n. 22
0
        /// <summary>
        /// Raises the appropriate ParentObject collision events depending on its CollisionType value
        /// </summary>
        /// <param name="sender">The current ISpaceObject instance</param>
        /// <param name="other">The ISpaceObject instance which collided</param>
        /// <param name="pair"/>
        protected void OnCollisionDetected(Collidable sender, Collidable other, CollidablePairHandler pair)
        {
            if (sender.Tag == null || other.Tag == null)
            {
                return;
            }

            ICollisionObject senderCollisionObject = (ICollisionObject)sender.Tag;
            ICollisionObject otherCollisionObject  = (ICollisionObject)other.Tag;

            switch (ParentObject.CollisionType)
            {
            case CollisionType.Trigger:
            {
                ParentObject.OnCollisionTrigger(senderCollisionObject, otherCollisionObject);
                break;
            }

            case CollisionType.Collide:
            {
                var collisionPoint = new CollisionPoint
                {
                    ContactObject = otherCollisionObject,
                    ContactPoint  = pair.Contacts[0].Contact.Position,
                    ContactTime   = pair.TimeOfImpact,
                    Material      = null,
                    SurfaceNormal = pair.Contacts[0].Contact.Normal
                };

                ParentObject.OnCollisionReact(senderCollisionObject, otherCollisionObject, collisionPoint, ref _collisionHandled);
                break;
            }

            default:
            case CollisionType.None:
            {
                break;
            }
            }
        }
Esempio n. 23
0
        public void Deflect(CollisionPoint c, Direction d)
        {
            switch (c)
            {
            case CollisionPoint.Right:
            case CollisionPoint.Left:
                Motion = new Vector2(Motion.X * -1, Motion.Y);
                break;

            case CollisionPoint.Top:
            case CollisionPoint.Bottom:
                Motion = new Vector2(Motion.X, Motion.Y * -1);
                break;

            case CollisionPoint.BottomRight:
            case CollisionPoint.BottomLeft:
            case CollisionPoint.TopRight:
            case CollisionPoint.TopLeft:
                Motion = new Vector2(Motion.X * -1, Motion.Y * -1);
                break;
            }
        }
Esempio n. 24
0
    void CheckCollisions(Vector3 startPos, Vector3 offset, float dt)
    {
        // STEP 1: Define our variables, what is endpoint?
        Vector3        endPos  = startPos + offset;
        CollisionPoint closest = new CollisionPoint();


        // STEP 2:  Find the closest collision

        // case 1: wall (top and bottom)
        if (endPos.z >= FIELD_TOP)
        {
            // evaluate collision with top
            float uz = (FIELD_TOP - startPos.z) / (offset.z);
            if (uz <= closest.progress)
            {
                closest = new CollisionPoint((startPos + (offset * uz)), "top", uz);
            }
        }
        else if (endPos.z <= FIELD_BOTTOM)
        {
        }



        // STEP 3:  Did we find a collision?

        // IF YES:

        //     STEP 4:  PROJECT THE BOUNCE

        //     STEP 5:  RUN CHECK COLLISIONS AGAIN


        // IF NO:

        //     Move to final position
    }
Esempio n. 25
0
        public static bool IsCollision(ILocatable me, Rectangle target, out Direction from, out CollisionPoint where, bool circle = false)
        {
            var prjMe = new Rectangle((int)(me.Bounds.X + me.Motion.X), (int)(me.Bounds.Y + me.Motion.Y),
                                      me.Bounds.Width,
                                      me.Bounds.Height);
            var prjTarget = target;

            from = Direction.Stop;
            if (Math.Abs(me.Motion.X) < 0.00001 && Math.Abs(me.Motion.Y) < 0.00001)
            {
                from = Direction.Stop;
            }
            else if (me.Motion.X < 0)  //Left
            {
                if (Math.Abs(me.Motion.Y) < 0.00001)
                {
                    from = Direction.Left;
                }
                else if (me.Motion.Y < 0)
                {
                    from = Direction.UpLeft;
                }
                else
                {
                    from = Direction.DownLeft;
                }
            }
            else
            {
                if (Math.Abs(me.Motion.Y) < 0.00001)
                {
                    from = Direction.Right;
                }
                else if (me.Motion.Y < 0)
                {
                    from = Direction.UpRight;
                }
                else
                {
                    from = Direction.DownRight;
                }
            }

            where = CollisionPoint.None;
            if (circle && !IsCollisionCircle(prjMe, prjTarget))
            {
                return(false);
            }
            if (prjMe.Intersects(prjTarget))
            {
                if (
                    prjMe.Intersects(new Rectangle(prjTarget.Left, prjTarget.Top,
                                                   prjTarget.Width, 1)))
                {
                    where = CollisionPoint.Top;
                }
                else if (
                    prjMe.Intersects(new Rectangle(prjTarget.Left, prjTarget.Bottom,
                                                   prjTarget.Width, 1)))
                {
                    where = CollisionPoint.Bottom;
                }
                if (prjMe.Intersects(new Rectangle(prjTarget.Left, prjTarget.Top, 1,
                                                   prjTarget.Height)))
                {
                    where = where == CollisionPoint.None
                        ? CollisionPoint.Left
                        : where == CollisionPoint.Top ? CollisionPoint.TopLeft : CollisionPoint.BottomLeft;
                }
                if (prjMe.Intersects(new Rectangle(prjTarget.Right, prjTarget.Top, 1,
                                                   prjTarget.Height)))
                {
                    where = where == CollisionPoint.None
                        ? CollisionPoint.Right
                        : where == CollisionPoint.Top ? CollisionPoint.TopRight : CollisionPoint.BottomRight;
                }

                return(true);
            }
            return(false);
        }
 public Velocity Hit(Velocity vel, CollisionPoint point)
 {
     throw new NotImplementedException();
 }
Esempio n. 27
0
        private void UpdateCollisions(float elapsedTime)
        {
            Vector3 planeVelocity      = new Vector3(Velocity.X, Velocity.Y, Velocity.Z);
            Vector3 rotationalVelocity = new Vector3(Wx, Wy, Wz);

            planeVelocity.TransformCoordinate(Matrix.RotationQuaternion(Quaternion.Invert(OrientationQuat)));
            //Vx = planeVelocity.X;
            //Vy = planeVelocity.Y;
            //Vz = planeVelocity.Z;
            Vector3 position = new Vector3(X, Y, Z);

            List <CollisionPoint> contactList = new List <CollisionPoint>();

            foreach (Vector3 gearPoint in AircraftParameters.GearPoints)
            {
                // Check for collisions
                Vector3 gearPointW = position + Vector3.TransformCoordinate(gearPoint, Matrix.RotationQuaternion(OrientationQuat));
                Vector3 normalW;
                float   depth = 0f;
                if (IsColliding(gearPointW, out normalW, out depth))
                {
                    Vector3        normal = Vector3.TransformCoordinate(ToModel(normalW), Matrix.RotationQuaternion(Quaternion.Invert(OrientationQuat)));
                    CollisionPoint point  = new CollisionPoint();
                    point.ContactPoint = gearPoint;
                    point.Normal       = normal;
                    point.NormalW      = ToModel(normalW);
                    point.Depth        = depth;
                    contactList.Add(point);
                }
            }
            if (contactList.Count > 0)
            {
                Vector3 contactPoint;
                Vector3 normal;
                Vector3 normalW;
                float   depth;
                if (contactList.Count == 1)
                {
                    // vertex
                    contactPoint = contactList[0].ContactPoint;
                    normal       = contactList[0].Normal;
                    normalW      = contactList[0].NormalW;
                    depth        = contactList[0].Depth;
                }
                else if (contactList.Count == 2)
                {
                    // segment
                    contactPoint = (0.5f * (contactList[0].ContactPoint + contactList[1].ContactPoint));
                    normal       = (0.5f * (contactList[0].Normal + contactList[1].Normal));
                    normalW      = (0.5f * (contactList[0].NormalW + contactList[1].NormalW));
                    depth        = (contactList[0].Depth + contactList[1].Depth) / 2;
                }
                else
                {
                    // plane

                    //contactPoint = (0.5f * (contactList[0].ContactPoint + contactList[1].ContactPoint));
                    normal       = (0.5f * (contactList[0].Normal + contactList[1].Normal));
                    normalW      = (0.5f * (contactList[0].NormalW + contactList[1].NormalW));
                    depth        = (contactList[0].Depth + contactList[1].Depth) / 2;
                    contactPoint = -depth * normal;
                }
                Vector3 pointVel = planeVelocity + Vector3.Cross(rotationalVelocity, contactPoint);
                float   epsilon  = 0.7f; // restitution
                float   j        = ((-1 - epsilon) * Vector3.Dot(planeVelocity, normal)) /
                                   ((Vector3.Dot(normal, normal) / (float)AircraftParameters.Mass) +
                                    (Vector3.Dot(MultiplyInertiaInverse(Vector3.Cross(Vector3.Cross(contactPoint, normal), contactPoint)), normal)));
                Z -= depth;
                if (Vector3.Dot(normal, pointVel) > 0)
                {
                }
                else
                {
                    //Z -= depth;
#if DEBUG
                    Framework.Instance.DebugString += j + "\n";
                    Framework.Instance.DebugString += normalW + "\n";
                    Framework.Instance.DebugString += contactList.Count;
#endif
                    Velocity += (j / (float)AircraftParameters.Mass) * normalW;
                    //Velocity += (-Velocity * 1.0f* elapsedTime);
                    Vector3 rotationVelDiff = MultiplyInertiaInverse(Vector3.Cross(contactPoint, j * normal));
                    Wx += rotationVelDiff.X;
                    Wy += rotationVelDiff.Y;
                    Wz += rotationVelDiff.Z;
                }
            }
            //DebugPosition = ToDirectX(gearPointW);
            //pointVel = Velocity + Vector3.Cross(rotationalVelocity, gearPoint);
        }
Esempio n. 28
0
 /// <summary>
 /// Calculates and applies the reaction force between the
 ///             object and the collision surface contained in the CollisionPoint.
 /// </summary>
 /// <param name="worldcollisionpoint">Contains information about the closest collision point to the collider.</param>
 public virtual void React(CollisionPoint worldcollisionpoint)
 {
 }
Esempio n. 29
0
        private List <JacobianConstraint> BuildRigidBodyCollisionConstraints(
            CollisionPointStructure collisionPointStr,
            IShape objectA,
            IShape objectB)
        {
            List <JacobianConstraint> contactConstraints = new List <JacobianConstraint>();

            double restitutionCoefficient =
                (objectA.RestitutionCoeff +
                 objectB.RestitutionCoeff) * 0.5;

            double baumgarteStabilizationValue =
                (objectA.RestoreCoeff +
                 objectB.RestoreCoeff) * 0.5;

            for (int h = 0; h < collisionPointStr.CollisionPointBase.Length; h++)
            {
                var collisionPointBase = collisionPointStr.CollisionPointBase[h];

                for (int k = 0; k < collisionPointBase.CollisionPoints.Length; k++)
                {
                    CollisionPoint collisionPoint = collisionPointBase.CollisionPoints[k];

                    Vector3d ra = collisionPoint.CollisionPointA.Vertex - objectA.Position;
                    Vector3d rb = collisionPoint.CollisionPointB.Vertex - objectB.Position;

                    Vector3d linearComponentA = (-1.0 * collisionPoint.CollisionNormal).Normalize();
                    Vector3d linearComponentB = -1.0 * linearComponentA;

                    Vector3d angularComponentA = ra.Cross(linearComponentA);
                    Vector3d angularComponentB = -1.0 * rb.Cross(linearComponentA);

                    Vector3d velocityA = objectA.LinearVelocity +
                                         objectA.AngularVelocity.Cross(ra);

                    Vector3d velocityB = objectB.LinearVelocity +
                                         objectB.AngularVelocity.Cross(rb);

                    Vector3d relativeVelocity = velocityB - velocityA;

                    if (relativeVelocity.Length() < 1E-12 &&
                        collisionPointBase.CollisionPoint.Intersection &&
                        collisionPointBase.CollisionPoint.Distance < 1E-10)
                    {
                        continue;
                    }

                    #region Normal direction contact

                    double linearComponent = linearComponentA.Dot(relativeVelocity);

                    double uCollision = restitutionCoefficient * Math.Max(0.0, linearComponent);

                    if (uCollision <= 0.0 &&
                        !collisionPointBase.CollisionPoint.Intersection)
                    {
                        continue;
                    }

                    double correctionParameter = 0.0;

                    if (collisionPointBase.CollisionPoint.Intersection)
                    {
                        //Limit the Baum stabilization jitter effect
                        correctionParameter = Math.Max(Math.Max(collisionPointBase.CollisionPoint.Distance - simulationParameters.CompenetrationTolerance, 0.0) *
                                                       baumgarteStabilizationValue, 0.0);
                    }

                    double correctedBounce = uCollision;

                    JacobianConstraint normalContact = JacobianCommon.GetDOF(
                        linearComponentA,
                        linearComponentB,
                        angularComponentA,
                        angularComponentB,
                        objectA,
                        objectB,
                        correctedBounce,
                        correctionParameter,
                        simulationParameters.NormalCFM,
                        0.0,
                        ConstraintType.Collision,
                        null);

                    #endregion

                    contactConstraints.Add(normalContact);

                    #region Friction Contact

                    JacobianConstraint[] frictionContact =
                        AddFrictionConstraints(
                            objectA,
                            objectB,
                            simulationParameters,
                            linearComponentA,
                            relativeVelocity,
                            ra,
                            rb);

                    #endregion

                    int normalIndex = contactConstraints.Count - 1;
                    foreach (JacobianConstraint fjc in frictionContact)
                    {
                        fjc.SetContactReference(normalIndex);
                        contactConstraints.Add(fjc);
                    }
                }
            }

            return(contactConstraints);
        }
Esempio n. 30
0
        /// <summary>
        /// Raises the appropriate ParentObject collision events depending on its CollisionType value
        /// </summary>
        /// <param name="sender">The current ISpaceObject instance</param>
        /// <param name="other">The ISpaceObject instance which collided</param>
        /// <param name="pair"/>
        protected void OnCollisionDetected(Collidable sender, Collidable other, CollidablePairHandler pair)
        {
            if (sender.Tag == null || other.Tag == null)
                return;

            ICollisionObject senderCollisionObject = (ICollisionObject) sender.Tag;
            ICollisionObject otherCollisionObject = (ICollisionObject) other.Tag;

            switch (ParentObject.CollisionType)
            {
                case CollisionType.Trigger:
                    {
                        ParentObject.OnCollisionTrigger(senderCollisionObject, otherCollisionObject);
                        break;
                    }
                case CollisionType.Collide:
                    {
                        var collisionPoint = new CollisionPoint
                                                 {
                                                     ContactObject = otherCollisionObject,
                                                     ContactPoint = pair.Contacts[0].Contact.Position,
                                                     ContactTime = pair.TimeOfImpact,
                                                     Material = null,
                                                     SurfaceNormal = pair.Contacts[0].Contact.Normal
                                                 };

                        ParentObject.OnCollisionReact(senderCollisionObject, otherCollisionObject, collisionPoint, ref _collisionHandled);
                        break;
                    }
                default:
                case CollisionType.None:
                    {
                        break;
                    }
            }
        }
Esempio n. 31
0
 public void AddCollisionPoint(CollisionPoint point)
 {
     colitionPoints.Add(point);
 }
 public void Write(TProtocol oprot)
 {
     oprot.IncrementRecursionDepth();
     try
     {
         TStruct struc = new TStruct("FireBallResponse");
         oprot.WriteStructBegin(struc);
         TField field = new TField();
         if (__isset.type)
         {
             field.Name = "type";
             field.Type = TType.I32;
             field.ID   = 10;
             oprot.WriteFieldBegin(field);
             oprot.WriteI32(Type);
             oprot.WriteFieldEnd();
         }
         if (Dir != null && __isset.dir)
         {
             field.Name = "dir";
             field.Type = TType.Struct;
             field.ID   = 20;
             oprot.WriteFieldBegin(field);
             Dir.Write(oprot);
             oprot.WriteFieldEnd();
         }
         if (CollisionPoint != null && __isset.collisionPoint)
         {
             field.Name = "collisionPoint";
             field.Type = TType.Struct;
             field.ID   = 30;
             oprot.WriteFieldBegin(field);
             CollisionPoint.Write(oprot);
             oprot.WriteFieldEnd();
         }
         if (DestGrid != null && __isset.destGrid)
         {
             field.Name = "destGrid";
             field.Type = TType.Struct;
             field.ID   = 40;
             oprot.WriteFieldBegin(field);
             DestGrid.Write(oprot);
             oprot.WriteFieldEnd();
         }
         if (__isset.ballId)
         {
             field.Name = "ballId";
             field.Type = TType.I32;
             field.ID   = 50;
             oprot.WriteFieldBegin(field);
             oprot.WriteI32(BallId);
             oprot.WriteFieldEnd();
         }
         if (__isset.speed)
         {
             field.Name = "speed";
             field.Type = TType.Double;
             field.ID   = 60;
             oprot.WriteFieldBegin(field);
             oprot.WriteDouble(Speed);
             oprot.WriteFieldEnd();
         }
         if (__isset.result)
         {
             field.Name = "result";
             field.Type = TType.Byte;
             field.ID   = 70;
             oprot.WriteFieldBegin(field);
             oprot.WriteByte(Result);
             oprot.WriteFieldEnd();
         }
         if (StartPosition != null && __isset.startPosition)
         {
             field.Name = "startPosition";
             field.Type = TType.Struct;
             field.ID   = 80;
             oprot.WriteFieldBegin(field);
             StartPosition.Write(oprot);
             oprot.WriteFieldEnd();
         }
         oprot.WriteFieldStop();
         oprot.WriteStructEnd();
     }
     finally
     {
         oprot.DecrementRecursionDepth();
     }
 }
Esempio n. 33
0
    void sphereOnCube(Transform sphereTransform, Transform cubeTransform, Physics spherePhysics, Physics cubePhysics)
    {
        //Rotate cube vectors into worldspace
        Matrix4x4 rotation = Matrix4x4.Rotate(Quaternion.Euler(cubeTransform.eulerAngles));

        Vector3 cubeX = rotation.MultiplyVector(new Vector3(1, 0, 0)).normalized *cubeTransform.localScale.x * 0.5f;
        Vector3 cubeY = rotation.MultiplyVector(new Vector3(0, 1, 0)).normalized *cubeTransform.localScale.y * 0.5f;
        Vector3 cubeZ = rotation.MultiplyVector(new Vector3(0, 0, 1)).normalized *cubeTransform.localScale.z * 0.5f;

        //Check if there's a collision against the closest side, and get the collision point
        Vector3 sphereRelativePosition = sphereTransform.position - cubeTransform.position;

        float distInX = Vector3.Dot(sphereRelativePosition, cubeX.normalized) / cubeX.magnitude;
        float distInY = Vector3.Dot(sphereRelativePosition, cubeY.normalized) / cubeY.magnitude;
        float distInZ = Vector3.Dot(sphereRelativePosition, cubeZ.normalized) / cubeZ.magnitude;

        CollisionPoint collision = new CollisionPoint(false);

        if (Mathf.Abs(distInX) > Mathf.Abs(distInY) && Mathf.Abs(distInX) > Mathf.Abs(distInZ)) //Closest to X sides
        {
            if (distInX > 0)
            {
                Plane plane = new Plane(cubeTransform.position + cubeX, cubeX, cubeY, cubeZ);
                collision = findCPSphereOnPlane(sphereTransform.position, spherePhysics.m_sphereRadius, plane);
            }
            else
            {
                Plane plane = new Plane(cubeTransform.position - cubeX, -cubeX, cubeY, cubeZ);
                collision = findCPSphereOnPlane(sphereTransform.position, spherePhysics.m_sphereRadius, plane);
            }
        }
        else if (Mathf.Abs(distInY) > Mathf.Abs(distInZ)) //Closest to Y sides
        {
            if (distInY > 0)
            {
                Plane plane = new Plane(cubeTransform.position + cubeY, cubeY, cubeX, cubeZ);
                collision = findCPSphereOnPlane(sphereTransform.position, spherePhysics.m_sphereRadius, plane);
            }
            else
            {
                Plane plane = new Plane(cubeTransform.position - cubeY, -cubeY, cubeX, cubeZ);
                collision = findCPSphereOnPlane(sphereTransform.position, spherePhysics.m_sphereRadius, plane);
            }
        }
        else //Closest to Z sides
        {
            if (distInZ > 0)
            {
                Plane plane = new Plane(cubeTransform.position + cubeZ, cubeZ, cubeY, cubeX);
                collision = findCPSphereOnPlane(sphereTransform.position, spherePhysics.m_sphereRadius, plane);
            }
            else
            {
                Plane plane = new Plane(cubeTransform.position - cubeZ, -cubeZ, cubeY, cubeX);
                collision = findCPSphereOnPlane(sphereTransform.position, spherePhysics.m_sphereRadius, plane);
            }
        }

        if (!collision.occurred)
        {
            return;
        }

        Vector3 pointToCentre = sphereTransform.position - collision.position;

        float vSphere     = Vector3.Dot(spherePhysics.m_velocity, pointToCentre.normalized);
        float vCube       = Vector3.Dot(cubePhysics.m_velocity, pointToCentre.normalized);
        float netMovement = vCube - vSphere;

        if (netMovement > m_contactSpeed) //Moving towards so collision should occur
        {
            float speedSphere = speedAfterCollision(spherePhysics.m_restitution * cubePhysics.m_restitution, vSphere, vCube, spherePhysics.m_mass, cubePhysics.m_mass);
            spherePhysics.m_velocity += (speedSphere - vSphere) * pointToCentre.normalized;

            float speedPlane = speedAfterCollision(spherePhysics.m_restitution * cubePhysics.m_restitution, vCube, vSphere, cubePhysics.m_mass, spherePhysics.m_mass);
            cubePhysics.m_velocity += (speedPlane - vCube) * pointToCentre.normalized;
        }
        else if (netMovement > -m_contactSpeed / 2) //In contact
        {
            sphereTransform.position = collision.position + spherePhysics.m_sphereRadius * pointToCentre.normalized;

            if (spherePhysics.m_mass != 0 && cubePhysics.m_mass != 0)
            {
                spherePhysics.m_velocity += netMovement * pointToCentre.normalized / 2;
                cubePhysics.m_velocity   -= netMovement * pointToCentre.normalized / 2;
            }
            else if (spherePhysics.m_mass != 0)
            {
                spherePhysics.m_velocity += netMovement * pointToCentre.normalized;
            }
            else if (cubePhysics.m_mass != 0)
            {
                cubePhysics.m_velocity += netMovement * pointToCentre.normalized;
            }
        }
        //Else they're moving away from each other
    }
        public override string ToString()
        {
            StringBuilder __sb    = new StringBuilder("FireBallResponse(");
            bool          __first = true;

            if (__isset.type)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("Type: ");
                __sb.Append(Type);
            }
            if (Dir != null && __isset.dir)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("Dir: ");
                __sb.Append(Dir == null ? "<null>" : Dir.ToString());
            }
            if (CollisionPoint != null && __isset.collisionPoint)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("CollisionPoint: ");
                __sb.Append(CollisionPoint == null ? "<null>" : CollisionPoint.ToString());
            }
            if (DestGrid != null && __isset.destGrid)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("DestGrid: ");
                __sb.Append(DestGrid == null ? "<null>" : DestGrid.ToString());
            }
            if (__isset.ballId)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("BallId: ");
                __sb.Append(BallId);
            }
            if (__isset.speed)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("Speed: ");
                __sb.Append(Speed);
            }
            if (__isset.result)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("Result: ");
                __sb.Append(Result);
            }
            if (StartPosition != null && __isset.startPosition)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("StartPosition: ");
                __sb.Append(StartPosition == null ? "<null>" : StartPosition.ToString());
            }
            __sb.Append(")");
            return(__sb.ToString());
        }
Esempio n. 35
0
 /// <summary>
 /// Calculates and applies the reaction force between the
 ///             object and the collision surface contained in the CollisionPoint.
 /// </summary>
 /// <param name="worldcollisionpoint">Contains information about the closest collision point to the collider.</param>
 public virtual void React(CollisionPoint worldcollisionpoint)
 {
 }