Ejemplo n.º 1
0
    private void HandleThrow(Vector3 dir)
    {
        dummyPlayerPuck.SetActive(false);
        IPhysicalObject physicalObject = physicalObjectFactory.Create(PhysicalObjectFactory.ObjectType.BlackPuck);

        physicalObject.ApplyForce(dir * ThrowForce);
    }
Ejemplo n.º 2
0
 // using Newton's Law of Universal Gravitation
 // http://en.wikipedia.org/wiki/Newton%27s_law_of_universal_gravitation
 public static Newton GravityBetween(IPhysicalObject physicalObject, IPhysicalObject otherObject)
 {
     Meter distanceBetween = physicalObject.GetCoordinates<Meter>().DistanceFrom<Meter>(otherObject.GetCoordinates<Meter>());
     var distanceSquared = new Meter(System.Math.Pow(distanceBetween.Value, 2));
     var productOfMasses = (physicalObject.GetMass<Kilogram>()*otherObject.GetMass<Kilogram>());
     return new Newton(GravitationalConstant.Multiply((productOfMasses).Divide(distanceSquared)).Value);
 }
        public virtual CollisionResult TestCollision(IPhysicalObject that, Vector2 thatNewPosition, float scrollRows)
        {
            var collisionResult = new CollisionResult();

            var thisScreenRectangle =
                new Rectangle((int)Position.X
                              , (int)Position.Y
                              , (int)this.Size.X
                              , (int)this.Size.Y);

            var thatScreenRectangle =
                new Rectangle((int)thatNewPosition.X
                              , (int)(thatNewPosition.Y)
                              , (int)that.Size.X
                              , (int)that.Size.Y);

            Rectangle intersectArea = Rectangle.Intersect(thisScreenRectangle, thatScreenRectangle);

            if (intersectArea.X * intersectArea.Y != 0)
            {
                collisionResult.CollisionType = CollisionType.Blocked;
            }

            return(collisionResult);
        }
Ejemplo n.º 4
0
 public static Newton ForceBetween(IPhysicalObject physicalObject, IPhysicalObject otherObject)
 {
     Meter distanceBetween = physicalObject.GetCoordinates<Meter>().DistanceFrom<Meter>(otherObject.GetCoordinates<Meter>());
     var distanceSquared = new Meter(System.Math.Pow(distanceBetween.Value, 2));
     var chargeProduct = physicalObject.GetCharge().Multiply(otherObject.GetCharge());
     return new Newton((CoulombsConstant.Multiply(chargeProduct).Divide(distanceSquared)).Value);
 }
Ejemplo n.º 5
0
 public Force(GraphComponent component, IPhysicalObject target, double strength)
     {
     component.Forces.Add(this);
     this.component = component;
     this.target    = target;
     this.strength  = strength;
     this.time      = 0;
     }
Ejemplo n.º 6
0
        // using Newton's Law of Universal Gravitation
        // http://en.wikipedia.org/wiki/Newton%27s_law_of_universal_gravitation

        public static Newton GravityBetween(IPhysicalObject physicalObject, IPhysicalObject otherObject)
        {
            Meter distanceBetween = physicalObject.GetCoordinates <Meter>().DistanceFrom <Meter>(otherObject.GetCoordinates <Meter>());
            var   distanceSquared = new Meter(System.Math.Pow(distanceBetween.Value, 2));
            var   productOfMasses = (physicalObject.GetMass <Kilogram>() * otherObject.GetMass <Kilogram>());

            return(new Newton(GravitationalConstant.Multiply((productOfMasses).Divide(distanceSquared)).Value));
        }
Ejemplo n.º 7
0
        public static Newton ForceBetween(IPhysicalObject physicalObject, IPhysicalObject otherObject)
        {
            Meter distanceBetween = physicalObject.GetCoordinates <Meter>().DistanceFrom <Meter>(otherObject.GetCoordinates <Meter>());
            var   distanceSquared = new Meter(System.Math.Pow(distanceBetween.Value, 2));
            var   chargeProduct   = physicalObject.GetCharge().Multiply(otherObject.GetCharge());

            return(new Newton((CoulombsConstant.Multiply(chargeProduct).Divide(distanceSquared)).Value));
        }
Ejemplo n.º 8
0
        public override CollisionResult TestCollision(IPhysicalObject that, Vector2 thatNewPosition, float scrollRows)
        {
            var collisionResult = new CollisionResult();

            if (thatNewPosition.X < 0 ||
                thatNewPosition.X + that.Size.X > GameSettings.Instance.WindowTilesSize.X ||
                thatNewPosition.Y < 0 ||
                thatNewPosition.Y + that.Size.Y > GameSettings.Instance.WindowTilesSize.Y)
            {
                collisionResult.CollisionType = CollisionType.OffRoad;
                var x = Math.Min(thatNewPosition.X, GameSettings.Instance.WindowTilesSize.X - that.Size.X);
                x = Math.Max(x, 0);
                var y = Math.Min(thatNewPosition.Y, GameSettings.Instance.WindowTilesSize.Y - that.Size.Y);
                y = Math.Max(y, 0);
            }
            else
            {
                for (var deltaX = 0; deltaX < that.Size.X; deltaX++)
                {
                    for (var deltaY = 0; deltaY < that.Size.Y; deltaY++)
                    {
                        var f = (thatNewPosition.X + deltaX) / 2f;
                        var x = (int)Math.Round(f * 2, MidpointRounding.AwayFromZero) / 2;
                        f = (scrollRows / 2) + (thatNewPosition.Y + deltaY) / 2;
                        var y = (int)Math.Round(f * 2, MidpointRounding.AwayFromZero) / 2;

                        if ((x < 0 ||
                             x >= MAP_COLS) ||
                            y < 0 ||
                            y >= MAP_ROWS)
                        {
                            collisionResult.CollisionType = CollisionType.OffRoad;
                            return(collisionResult);
                        }
                        else
                        {
                            var tileInfo = jsonMap.layers[0].tileIndexes[(int)(x + y * MAP_COLS)];

                            if (tileInfo.ti != 0)
                            {
                                collisionResult.CollisionType = CollisionType.Blocked;
                                return(collisionResult);
                            }
                        }
                    }
                }
            }

            return(collisionResult);
        }
Ejemplo n.º 9
0
        protected Vector3 LookAtMotor(IPhysicalObject obj)
        {
            lock (m_LookAtParamsLock)
            {
                if (!m_EnableLookAt)
                {
                    return(Vector3.Zero);
                }
                Quaternion dirRot = m_LookAtTarget / obj.Rotation;
                Vector3    dir    = dirRot.GetEulerAngles();

                dir /= m_LookAtStrength;

                return(dir - obj.AngularVelocity * m_LookAtDamping);
            }
        }
Ejemplo n.º 10
0
    public IPhysicalObject Create(ObjectType type)
    {
        switch (type)
        {
        case ObjectType.BlackPuck:
            IPhysicalObject physicalObject = diContainer.InstantiatePrefabResource(PrefabBlackPuckPath).GetComponent <PhysicalObject>();
            onBlackPuckCreated?.Invoke(physicalObject);
            return(physicalObject);

        case ObjectType.RedPuck:
            return(diContainer.InstantiatePrefabResource(PrefabRedPuckPath).GetComponent <PhysicalObject>());

        case ObjectType.Goal:
            return(diContainer.InstantiatePrefabResource(PrefabGoalPath).GetComponent <PhysicalObject>());

        default:
            return(null);
        }
    }
        private void ApplyPhysics(int id, Func <IInputElement, Point> getposition)
        {
            if (!_manipulationstack.ContainsKey(id) || _manipulationstack[id].Count == 0)
            {
                return;
            }

            List <IPhysicalObject> stack    = _manipulationstack[id];
            IPhysicalObject        original = stack[stack.Count - 1];

            // Call Apply on the physical object. Pass the point and retrieve the unhandled vector.
            original.SetPoint(id, attachedElement.TransformToDescendant(original.ContainerVisual).Transform(_lastknownpositions[id]));
            Vector delta = original.ApplyManipulation(id, getposition(original.ContainerVisual));

            _lastknownpositions[id] = getposition(attachedElement);

            // while there are parent physical containers...
            for (int i = stack.Count - 2; i >= 0; i--)
            {
                IPhysicalObject parent = stack[i];

                // Find the touch point on the parent
                Point pt = getposition(parent.ContainerVisual);

                Matrix m = ((Transform)original.ContainerVisual.TransformToAncestor(parent.ContainerVisual)).Value;

                m.OffsetX = 0;
                m.OffsetY = 0;

                parent.SetPoint(id, pt + m.Transform(delta));

                // Find the transform from original to the parent.
                // Transform the parent-adjusted point to parent space
                // Call Apply on parent
                delta = parent.ApplyManipulation(id, pt);

                // original = parent
                original = parent;
            }
        }
        public static void PushPhysicalReferenceFrame(InputDevice device, IPhysicalObject obj)
        {
            int id = 0;

            if (device is MouseDevice)
            {
                id = -1;
                _lastknownpositions[id] = ((MouseDevice)device).GetPosition(device.ActiveSource.RootVisual as IInputElement);
            }
            if (device is StylusDevice)
            {
                id = ((StylusDevice)device).Id;
                _lastknownpositions[id] = ((StylusDevice)device).GetPosition(device.ActiveSource.RootVisual as IInputElement);
            }

            if (!_manipulationstack.ContainsKey(id))
            {
                _manipulationstack[id] = new List <IPhysicalObject>();
            }

            _manipulationstack[id].Add(obj);
        }
Ejemplo n.º 13
0
 protected PositionalForce HoverMotor(IPhysicsObject actor, IPhysicalObject obj, Vector3 pos)
 {
     lock (m_HoverParamsLock)
     {
         if (m_HoverEnabled)
         {
             var    v = new Vector3(0, 0, (m_Buoyancy - 1) * GravityConstant(actor, obj));
             double targetHoverHeight;
             SceneInterface.LocationInfo locInfo = LocationInfoProvider.At(obj.GlobalPosition);
             targetHoverHeight = locInfo.GroundHeight;
             if (targetHoverHeight < locInfo.WaterHeight && m_AboveWater)
             {
                 targetHoverHeight = locInfo.WaterHeight;
             }
             v.Z += (targetHoverHeight - obj.Position.Z) * m_HoverTau;
             return(new PositionalForce("HoverMotor", v, pos));
         }
         else
         {
             return(new PositionalForce("HoverMotor", Vector3.Zero, pos));
         }
     }
 }
Ejemplo n.º 14
0
        protected PositionalForce MoveToTargetMotor(IPhysicalObject obj)
        {
            Vector3 force = Vector3.Zero;
            Vector3 dist  = obj.GlobalPosition;

            lock (m_MoveToTargetParamsLock)
            {
                if (m_MoveToTargetTau > 0.01 && m_EnableMoveToTarget)
                {
                    dist -= m_MoveToTargetPos;
                    force = dist / m_MoveToTargetTau;
                    Vector3 abs = new Vector3
                    {
                        X = Math.Abs(dist.X),
                        Y = Math.Abs(dist.Y),
                        Z = Math.Abs(dist.Z)
                    };
                    force.X = force.X.Clamp(-abs.X, abs.X);
                    force.Y = force.Y.Clamp(-abs.Y, abs.Y);
                    force.Z = force.Z.Clamp(-abs.Z, abs.Z);
                }
            }
            return(new PositionalForce("MoveToTargetMotor", force, Vector3.Zero));
        }
Ejemplo n.º 15
0
        public override CollisionResult TestCollision(IPhysicalObject that, Vector2 thatNewPosition, float scrollRows)
        {
            var collisionResult = new CollisionResult();

            if (thatNewPosition.X < 0 ||
                thatNewPosition.X + that.Size.X > GameSettings.Instance.WindowTilesSize.X ||
                thatNewPosition.Y < 0 ||
                thatNewPosition.Y + that.Size.Y > GameSettings.Instance.WindowTilesSize.Y)
            {
                collisionResult.CollisionType = CollisionType.OffRoad;
                var x = Math.Min(thatNewPosition.X, GameSettings.Instance.WindowTilesSize.X - that.Size.X);
                x = Math.Max(x, 0);
                var y = Math.Min(thatNewPosition.Y, GameSettings.Instance.WindowTilesSize.Y - that.Size.Y);
                y = Math.Max(y, 0);
            }
            else
            {
                for (var deltaX = 0; deltaX < that.Size.X; deltaX++)
                {
                    for (var deltaY = 0; deltaY < that.Size.Y; deltaY++)
                    {
                        var f = (thatNewPosition.X + deltaX) / 2f;
                        var x = (int)Math.Round(f * 2, MidpointRounding.AwayFromZero) / 2;
                        f = (scrollRows / 2) + (thatNewPosition.Y + deltaY) / 2;
                        var y = (int)Math.Round(f * 2, MidpointRounding.AwayFromZero) / 2;

                        if ((x < 0 ||
                             x >= (GameSettings.Instance.WindowTilesSize.X / 2)) ||
                            y < 0 ||
                            y >= (mapLines.Count() * 2))
                        {
                            collisionResult.CollisionType = CollisionType.OffRoad;
                            return(collisionResult);
                        }
                        else
                        {
                            var mapChar = mapLines[y][x];

                            if ("XD".Contains(mapChar))
                            {
                                collisionResult.CollisionType = CollisionType.Blocked;
                                return(collisionResult);
                            }
                        }
                    }
                }

                //var colorArray = new Color[(int)(that.Size.X * that.Size.Y)];
                //var extractRegion = new Rectangle(
                //    (int)Math.Round(thatNewPosition.X),
                //    (int)Math.Round(scrollRows + thatNewPosition.Y),
                //    (int)that.Size.X,
                //    (int)that.Size.Y);
                //path01.GetData<Color>(0, extractRegion, colorArray, 0, (int)(that.Size.X * that.Size.Y));
                //foreach (var color in colorArray)
                //{
                //    if (color != Color.White)
                //    {
                //        collisionResult.CollisionType = CollisionType.Blocked;
                //        break;
                //    }
                //}
            }

            return(collisionResult);
        }
Ejemplo n.º 16
0
 public bool Contains(IPhysicalObject physicalObject)
 {
     return(_physicalObjects.Contains(physicalObject));
 }
Ejemplo n.º 17
0
 private void UpdateObjectParameters(IPhysicalObject o, float dt)
 {
     switch (o.Type)
     {
         case ObjectType.PointMass:
             UpdatePointMassParameters((PointMass)o, dt);
             break;
         case ObjectType.RigidBody:
             UpdateRigidBodyParameters((RigidBody)o, dt);
             break;
     }
     if (o.CollisionDataType == CollisionDataType.CollisionMesh)
     {
         ((CollisionMesh)o.CollisionData).MeshRotation = o.AngularVelocity;
     }
 }
Ejemplo n.º 18
0
        public void RemoveObject(IPhysicalObject obj)
        {
            int i = objects.IndexOf(obj);
            if (i >= 0)
            {

                walkingEnabled.Remove(objects[i]);
                walkData.Remove(objects[i]);
                objects.RemoveAt(i);
            }
            if (obj.CollisionDataType != CollisionDataType.None)
                CollisionDetector.RemoveObject(obj);
        }
Ejemplo n.º 19
0
 public FrictionalForce(GraphComponent component, IPhysicalObject target, double k, double degree = 0) : base(component, target, k)
     {
     this.degree = degree;
     }
Ejemplo n.º 20
0
 protected PositionalForce BuoyancyMotor(IPhysicsObject actor, IPhysicalObject obj, Vector3 pos)
 {
     return(new PositionalForce("BuoyancyMotor", new Vector3(0, 0, m_Buoyancy * GravityConstant(actor, obj)), pos));
 }
Ejemplo n.º 21
0
 protected double GravityConstant(IPhysicsObject actor, IPhysicalObject obj) =>
 CombinedGravityAccelerationConstant *actor.Mass *obj.PhysicsGravityMultiplier;
Ejemplo n.º 22
0
 public IntersectionForce(GraphComponent component, IPhysicalObject target, double strength, GraphEdge a, GraphEdge b, IPhysicalObject reference) : base(component,target,strength)
     {
     this.a = a;
     this.b = b;
     this.reference = reference;
     }
Ejemplo n.º 23
0
 public UserDraggingForce(GraphComponent component, IPhysicalObject target, double strength) : base(component,target,strength)
     {
     }
Ejemplo n.º 24
0
 private bool isObjectOnPlatform(IPhysicalObject lf)
 {
     return((lf.Y <= y && lf.Y >= y - 2) && (lf.X >= x) && (lf.X < x + length));
 }
Ejemplo n.º 25
0
 public void addObject(IPhysicalObject obj)
 {
     plObjects.Add(obj);
 }
Ejemplo n.º 26
0
 private void HandleBlackPuckCreated(IPhysicalObject physicalObject)
 {
     blackPuckPhysicalObject = physicalObject;
     SubscribeToBlackPuck();
 }
Ejemplo n.º 27
0
 private void SpawnGoal()
 {
     goal = physicalObjectFactory.Create(PhysicalObjectFactory.ObjectType.Goal);
     goal.SetPosition(GetRandomSpawnPos());
 }
Ejemplo n.º 28
0
 public SpringForce(GraphComponent component, IPhysicalObject target, double k, double l, IPhysicalObject reference) : base(component, target, k)
     {
     this.equilibriumLength = l;
     this.reference = reference;
     }
Ejemplo n.º 29
0
 protected PositionalForce TargetVelocityMotor(IPhysicalObject obj, Vector3 targetvel, double factor, Vector3 pos) =>
 new PositionalForce("TargetVelocityMotor", (targetvel - obj.Velocity) * factor, pos);
Ejemplo n.º 30
0
 public CoolingThermalNoise(GraphComponent component, IPhysicalObject target, double strength, double decayDuration) : base(component,target,strength)
     {
     this.decayDuration = decayDuration;
     }
Ejemplo n.º 31
0
 protected PositionalForce GravityMotor(IPhysicsObject actor, IPhysicalObject obj, Vector3 pos) =>
 new PositionalForce("GravityMotor", new Vector3(0, 0, -GravityConstant(actor, obj)), pos);
Ejemplo n.º 32
0
 public void RemovePhysicalObject(IPhysicalObject physicalObject)
 {
     _physicalObjects.Remove(physicalObject);
 }
Ejemplo n.º 33
0
 public void DisableWalking(IPhysicalObject obj)
 {
     for (int i = 0; i < walkingEnabled.Count; i++)
     {
         if (objects[i] == obj)
         {
             walkingEnabled[objects[i]] = false;
             //walkData.Remove(objects[i]);
         }
     }
 }
Ejemplo n.º 34
0
 public static void CreateSymmetricalSpringForces(GraphComponent component, IPhysicalObject a, IPhysicalObject b, double k, double l)
 // Create the pair of forces that act on both ends of a conceptual spring
     {
     new SpringForce(component, a, k, l, b);
     new SpringForce(component, b, k, l, a);
     }
Ejemplo n.º 35
0
 public void AddObject(IPhysicalObject obj)
 {
     objects.Add(obj);
     walkingEnabled[obj] = false;
     if (obj.CollisionDataType != CollisionDataType.None)
         CollisionDetector.AddObject(obj);
 }
Ejemplo n.º 36
0
 internal static PhysicalVector GetAirResistanceForce(this IPhysicalObject obj)
 {
     return(new PhysicalVector(obj.Speed.Angle + Math.PI).VectorMultiply(obj.Speed.Value * PhysicalConstants.AirResistanceKoefficient));
 }
Ejemplo n.º 37
0
 public void EnableWalking(IPhysicalObject obj)
 {
     for (int i = 0; i < walkingEnabled.Count; i++)
     {
         if (objects[i] == obj)
             walkingEnabled[objects[i]] = true;
     }
 }
Ejemplo n.º 38
0
 public ReplusiveForce(GraphComponent component, IPhysicalObject target, IPhysicalObject reference, double strength, double degree = -2) : base(component,target,strength)
     {
     this.reference = reference;
     this.degree    = degree;
     }
Ejemplo n.º 39
0
 private void CalculateObjectForces(IPhysicalObject o)
 {
     switch (o.Type)
     {
         case ObjectType.PointMass:
             CalculatePointMassForces((PointMass)o);
             break;
         case ObjectType.RigidBody:
             CalculateRigidBodyForces((RigidBody)o);
             break;
     }
 }
 ISelectable SelectableFromPhysical(IPhysicalObject p)
     {
     foreach (GraphComponentCanvas c in this.canvases)
         {
         foreach (ISelectable s in c.Component.Selectables)
             {
             VisualGraphVertex d = s as VisualGraphVertex;
             if (null != d)
                 {
                 if (d.PhysicalObject == p)
                     return s;
                 }
             }
         }
     return null;
     }
Ejemplo n.º 41
0
 public override CollisionResult TestCollision(IPhysicalObject that, Vector2 thatNewPosition, float scrollRows)
 {
     return(base.TestCollision(that, thatNewPosition, scrollRows));
 }
Ejemplo n.º 42
0
 public RightAngleForce(GraphComponent component, IPhysicalObject a, IPhysicalObject target, IPhysicalObject b, double strength) : base(component, target, strength)
     {
     this.a = a;
     this.b = b;
     }
Ejemplo n.º 43
0
 public void AddPhysicalObject(IPhysicalObject physicalObject)
 {
     _physicalObjects.Add(physicalObject);
 }
Ejemplo n.º 44
0
 protected PositionalForce LinearRestitutionMotor(IPhysicalObject obj, double factor, Vector3 pos) =>
 new PositionalForce("LinearRestitutionMotor", -obj.Velocity * factor, pos);
Ejemplo n.º 45
0
 internal static PhysicalVector GetGravityForce(this IPhysicalObject obj)
 {
     return(new PhysicalVector(0, obj.Mass * PhysicalConstants.GravityAcceleration));
 }
Ejemplo n.º 46
0
 protected bool IsSelected(IPhysicalObject o)
     {
     ISelectable s = o.Visualization as ISelectable;
     return s.IsSelected();
     }
Ejemplo n.º 47
0
 public void RegisterObject(IPhysicalObject physObject)
 {
     PhysicalObjects.Add(physObject);                    // you may have a problem with deep copy v. shallow copy with this; you want a shallow copy so the physics engine will manipulate the original class
 }
Ejemplo n.º 48
0
 public override void Initialize(Object initializationData)
 {
     this._resource = (IPhysicalObject)initializationData;
 }