Пример #1
0
        private void handleCollision(CollisionInfo colInfo, float timestep)
        {
            GameObject obj1 = colInfo.Entity1;
            GameObject obj2 = colInfo.Entity2;
            Vector2 normal = colInfo.Normal;
            float depth = colInfo.Depth;

            if (!obj1.HasComponent(ComponentType.MotionProperties)) return;
            if (!obj1.HasComponent(ComponentType.Transform2D)) return;
            if (!obj1.HasComponent(ComponentType.BoundingBox)) return;
            if (!obj2.HasComponent(ComponentType.Transform2D)) return;
            if (!obj2.HasComponent(ComponentType.BoundingBox)) return;

            MotionPropertiesComponent obj1MotionComponent =
                (MotionPropertiesComponent)obj1.GetComponent(ComponentType.MotionProperties);
            Transform2DComponent obj1TransformComponent =
                (Transform2DComponent)obj1.GetComponent(ComponentType.Transform2D);
            BoundingBoxComponent obj1BoundingBoxComponent =
                (BoundingBoxComponent)obj1.GetComponent(ComponentType.BoundingBox);
            CurrentActionComponent obj1CurActionComponent =
                (CurrentActionComponent)obj1.GetComponent(ComponentType.Action);
            Transform2DComponent obj2TransformComponent =
                (Transform2DComponent)obj2.GetComponent(ComponentType.Transform2D);
            BoundingBoxComponent obj2BoundingBoxComponent =
                (BoundingBoxComponent)obj2.GetComponent(ComponentType.BoundingBox);

            //velocity correction
            Vector2 obj2obj1_vector = obj2TransformComponent.GetTranslation() - obj1TransformComponent.GetTranslation();
            obj2obj1_vector.Normalize();

            if (Vector2.Dot(normal, obj2obj1_vector) > 0)
            {
                normal = normal * -1;
            }

            Vector2 velocityCorrection = normal * depth * (float)(1.0f / timestep);
            Vector2 positionCorrection = normal * depth;

            if (obj2.HasComponent(ComponentType.MotionProperties))
            {
                MotionPropertiesComponent obj2MotionComponent =
                    (MotionPropertiesComponent)obj2.GetComponent(ComponentType.MotionProperties);

                //obj1MotionComponent.AddVelocity(velocityCorrection);

            }
            else
            {
                obj1TransformComponent.AddTranslation(positionCorrection);

                if (positionCorrection.Y != 0)
                {
                    obj1MotionComponent.SetVelocity(new Vector2(obj1MotionComponent.GetVelocity().X, 0.0f));
                }
                else if(positionCorrection.X != 0)
                {
                    obj1MotionComponent.SetVelocity(new Vector2(0.0f, obj1MotionComponent.GetVelocity().Y));
                }
                //obj1MotionComponent.AddVelocity(velocityCorrection);

            }

            if (velocityCorrection.Y != 0.0f)
            {
                obj1MotionComponent.State = MotionState.Ground;
                if (obj1CurActionComponent != null &&
                    obj1CurActionComponent.curAction.secondaryAction != SecondaryAction.Smash &&
                    obj1CurActionComponent.curAction.secondaryAction != SecondaryAction.Shield &&
                    obj1CurActionComponent.curAction.secondaryAction != SecondaryAction.Walk &&
                    obj1CurActionComponent.curAction.secondaryAction != SecondaryAction.Throw &&
                    obj1CurActionComponent.curAction.secondaryAction != SecondaryAction.Grabbed &&
                    obj1CurActionComponent.curAction.secondaryAction != SecondaryAction.Grab)
                {
                    obj1CurActionComponent.curAction.primaryAction = PrimaryAction.None;
                    obj1CurActionComponent.curAction.secondaryAction = SecondaryAction.Stand;
                }
            }
            else
            {
                obj1MotionComponent.State = MotionState.Air;
            }

            //friction
            if(obj2.HasComponent(ComponentType.PhysicalProperties))
            {
                PhysicalPropertiesComponent obj2PhysicalPropertiesComponent =
                    (PhysicalPropertiesComponent)obj2.GetComponent(ComponentType.PhysicalProperties);

                Vector2 velocity = obj1MotionComponent.GetVelocity();
                float velocityX = velocity.X / Math.Abs(velocity.X);
                if (float.IsNaN(velocityX)) velocityX = 0;
                float frictionCoefficient = obj2PhysicalPropertiesComponent.Friction * velocityX * -1.0f;

                if (velocityX != 0)
                {
                    float velocityXBuffer = obj1MotionComponent.GetVelocity().X;
                    obj1MotionComponent.AddVelocity(new Vector2(frictionCoefficient * timestep, 0.0f));

                    if (velocityXBuffer * obj1MotionComponent.GetVelocity().X < 0)
                    {
                        obj1MotionComponent.SetVelocity(
                            new Vector2(0.0f, obj1MotionComponent.GetVelocity().Y));
                    }
                }
            }
        }
Пример #2
0
 protected void triggerActionCollision(CollisionInfo colInfo, float timestep)
 {
     ActionCollision(colInfo, timestep);
 }
Пример #3
0
        protected void triggerCollisionEvent(CollisionInfo colInfo, float timestep)
        {
            GameObject obj1 = colInfo.Entity1;
            GameObject obj2 = colInfo.Entity2;

            if (obj1.HasComponent(ComponentType.IsPhysical) && obj2.HasComponent(ComponentType.IsPhysical))
            {
                this.triggerPhysicalCollision(colInfo, timestep);
            }

            if (obj1.HasComponent(ComponentType.IsAction) && obj2.HasComponent(ComponentType.IsCharacter))
            {
                this.triggerActionCollision(colInfo, timestep);
            }
        }
Пример #4
0
 protected void triggerPhysicalCollision(CollisionInfo colInfo, float timestep)
 {
     PhysicalCollision(colInfo, timestep);
 }
Пример #5
0
        private void actionHitHandler(CollisionInfo colInfo, float timestep)
        {
            GameObject obj1 = colInfo.Entity1;
            GameObject obj2 = colInfo.Entity2;
            GameObject obj1Parent = obj1.GetParent();

            if (!obj1.HasComponent(ComponentType.IsAction)) return;
            if (!obj2.HasComponent(ComponentType.IsCharacter)) return;
            if (!obj2.HasComponent(ComponentType.Health)) return;
            if (!obj2.HasComponent(ComponentType.MotionProperties)) return;

            IsActionComponent obj1ActionComponent = (IsActionComponent)obj1.GetComponent(ComponentType.IsAction);
            LifetimeComponent obj1LifetimeComponent = (LifetimeComponent)obj1.GetComponent(ComponentType.Lifetime);
            HealthComponent obj2HealthComponent = (HealthComponent)obj2.GetComponent(ComponentType.Health);
            MotionPropertiesComponent obj2MotionComponent = (MotionPropertiesComponent)obj2.GetComponent(ComponentType.MotionProperties);
            ActionInfoProjectile actionInfo = (ActionInfoProjectile)obj1ActionComponent.ActionInfo;

            obj2HealthComponent.AddDmg(actionInfo.OnHitDamage);
            Vector2 forceFinal = actionInfo.OnHitForce * obj2HealthComponent.getCurrDmg() / obj2MotionComponent.Mass;
            obj2MotionComponent.AddVelocity(forceFinal);
            obj1LifetimeComponent.Lifetime = 0;

            if (obj1Parent != null)
            {
                CurrentActionComponent obj1ParentCurAction =
                    (CurrentActionComponent)obj1Parent.GetComponent(ComponentType.Action);
            }
        }