Esempio n. 1
0
        public override void LoadContent(ContentManager cm, Scene scene, Layer layer)
        {
            var vertices = new List <Vector2>();

            // Get centroid first
            for (int i = 0; i < WorldPoints.Length; i++)
            {
                vertices.Add(ConvertUnits.ToSimUnits(WorldPoints[i]));
            }
            float area = GeometryFunctions.GetSignedArea(vertices);

            if (area < 0)
            {
                vertices.Reverse();
            }

            Vector2 position = GeometryFunctions.GetCentroid(vertices);

            position = position * layer.ScrollSpeed;

            var gameObject = new GameObject(Name, ConvertUnits.ToDisplayUnits(position), scene, layer);

            var physComponent = gameObject.AddComponent <PhysicsComponent>();

            physComponent.Polygon     = new List <Vector2>(WorldPoints);
            physComponent.Mass        = 10f;
            physComponent.Restitution = 0.5f;
            physComponent.Friction    = 0.8f;
            physComponent.IsStatic    = PhysicsType != "Dynamic";

            //gameObject.BagIndex = layer.GameObjects.Add(gameObject);
        }
Esempio n. 2
0
        public override void Start()
        {
            // Convert Polygon to physic units.

            var vertices = new List <Vector2>();

            // Get centroid first
            Polygon.ForEach((v) => { vertices.Add(ConvertUnits.ToSimUnits(v)); });
            float area = GeometryFunctions.GetSignedArea(vertices);

            if (area < 0)
            {
                vertices.Reverse();
            }

            Vector2 position = GeometryFunctions.GetCentroid(vertices);

            GameObject.Position = ConvertUnits.ToDisplayUnits(position);

            Polygon = vertices;

            if (IsSensor)
            {
                _physics = new Sensor(GameObject.Scene.WorldInfo, ConvertUnits.ToSimUnits(GameObject.Position), Polygon);
            }
            else
            {
                if (IsStatic)
                {
                    _physics = new StaticObject(GameObject.Scene.WorldInfo, ConvertUnits.ToSimUnits(GameObject.Position), Mass, Friction, Restitution, Polygon);
                }
                else
                {
                    var dynamicObject = new DynamicObject(GameObject.Scene.WorldInfo, ConvertUnits.ToSimUnits(GameObject.Position), Mass, Friction, Restitution, Polygon);
                    dynamicObject.IgnoreGravity = IgnoreGravity;

                    _physics = dynamicObject;
                }
            }

            _physics.IsProjectile = IsProjectile;
            _physics.IsEthereal   = IsEthereal;

            // Hook up collision callbacks
            _physics.OnCollision += (A, B, p) =>
            {
                if (A == _physics)
                {
                    if (OnCollision != null)
                    {
                        OnCollision(B.GameObject);
                    }

                    if (B.GameObject != null)
                    {
                        if (OnCollisionEnter != null && !_collidingGameObjects.ContainsKey(B.GameObject))
                        {
                            OnCollisionEnter(B.GameObject);
                        }

                        if (OnCollisionEnter != null || OnCollisionLeave != null)
                        {
                            _collidingGameObjects[B.GameObject] = true;
                        }
                    }
                }
                else if (B == _physics)
                {
                    if (OnCollision != null)
                    {
                        OnCollision(A.GameObject);
                    }

                    if (A.GameObject != null)
                    {
                        if (OnCollisionEnter != null && !_collidingGameObjects.ContainsKey(A.GameObject))
                        {
                            OnCollisionEnter(A.GameObject);
                        }

                        if (OnCollisionEnter != null || OnCollisionLeave != null)
                        {
                            _collidingGameObjects[A.GameObject] = true;
                        }
                    }
                }

                // If we collide with object subscripe to pre update event.
                if (_preUpdateEventRegistered == false && ((_collidingGameObjects.Count != 0 && (OnCollisionEnter != null || OnCollisionLeave != null)) || StorePreviousVelocity))
                {
                    _preUpdateEventRegistered   = true;
                    GameObject.Scene.PreUpdate += preUpdate;
                }
            };

            // Set reset info.
            if (IsStatic == false && IsProjectile == false)
            {
                _startPosition = GameObject.Position;
                SceneInfo.DynamicPhysicObjects.Add(this);
            }

            if (_pendingGameObject != null)
            {
                _physics.GameObject = _pendingGameObject;
            }
        }