Ejemplo n.º 1
0
        private Entity CreateChildEntity(ColliderShape shape, PhysicsElementBase.Types type, bool addOffset = false)
        {
            if (shape == null)
            {
                return(null);
            }

            switch (shape.Type)
            {
            case ColliderShapeTypes.StaticPlane:
            {
                //Hmm TODO maybe can draw an infinite plane??
                return(null);
            }

            case ColliderShapeTypes.Compound:
            {
                var entity = new Entity();

                //We got to recurse
                var compound = (CompoundColliderShape)shape;
                for (var i = 0; i < compound.Count; i++)
                {
                    var subShape  = compound[i];
                    var subEntity = CreateChildEntity(subShape, type, true);

                    subEntity.Transform.UseTRS = false;
                    entity.AddChild(subEntity);
                }

                entity.Transform.LocalMatrix = Matrix.Identity;
                entity.Transform.UseTRS      = false;

                return(entity);
            }

            default:
            {
                Material mat;
                switch (type)
                {
                case PhysicsElementBase.Types.PhantomCollider:
                    mat = triggerMaterial;
                    break;

                case PhysicsElementBase.Types.StaticCollider:
                case PhysicsElementBase.Types.StaticRigidBody:
                    mat = staticMaterial;
                    break;

                case PhysicsElementBase.Types.DynamicRigidBody:
                    mat = dynamicMaterial;
                    break;

                case PhysicsElementBase.Types.KinematicRigidBody:
                    mat = kinematicMaterial;
                    break;

                case PhysicsElementBase.Types.CharacterController:
                    mat = characterMaterial;
                    break;

                default:
                    throw new ArgumentOutOfRangeException("type", type, null);
                }

                var entity = new Entity
                {
                    new ModelComponent
                    {
                        Model = new Model
                        {
                            mat,
                            new Mesh
                            {
                                Draw = shape.CreateDebugPrimitive(graphicsDevice)
                            }
                        }
                    }
                };

                var offset = addOffset ? Matrix.RotationQuaternion(shape.LocalRotation) * Matrix.Translation(shape.LocalOffset) : Matrix.Identity;

                if (shape.Type == ColliderShapeTypes.ConvexHull)
                {
                    var hullDesc = (ConvexHullColliderShape)shape;

                    entity.Transform.LocalMatrix = shape.DebugPrimitiveMatrix * Matrix.Scaling(hullDesc.Scaling) * offset;
                }
                else
                {
                    entity.Transform.LocalMatrix = shape.DebugPrimitiveMatrix * offset;
                }
                entity.Transform.UseTRS = false;

                return(entity);
            }
            }
        }
        private Entity CreateChildEntity(ColliderShape shape, PhysicsElementBase.Types type, bool addOffset = false)
        {
            if (shape == null)
                return null;

            switch (shape.Type)
            {
                case ColliderShapeTypes.StaticPlane:
                    {
                        //Hmm TODO maybe can draw an infinite plane??
                        return null;
                    }
                case ColliderShapeTypes.Compound:
                    {
                        var entity = new Entity();

                        //We got to recurse
                        var compound = (CompoundColliderShape)shape;
                        for (var i = 0; i < compound.Count; i++)
                        {
                            var subShape = compound[i];
                            var subEntity = CreateChildEntity(subShape, type, true);

                            subEntity.Transform.UseTRS = false;
                            entity.AddChild(subEntity);
                        }

                        entity.Transform.LocalMatrix = Matrix.Identity;
                        entity.Transform.UseTRS = false;

                        return entity;
                    }
                default:
                    {
                        Material mat;
                        switch (type)
                        {
                            case PhysicsElementBase.Types.PhantomCollider:
                                mat = triggerMaterial;
                                break;

                            case PhysicsElementBase.Types.StaticCollider:
                            case PhysicsElementBase.Types.StaticRigidBody:
                                mat = staticMaterial;
                                break;

                            case PhysicsElementBase.Types.DynamicRigidBody:
                                mat = dynamicMaterial;
                                break;

                            case PhysicsElementBase.Types.KinematicRigidBody:
                                mat = kinematicMaterial;
                                break;

                            case PhysicsElementBase.Types.CharacterController:
                                mat = characterMaterial;
                                break;

                            default:
                                throw new ArgumentOutOfRangeException("type", type, null);
                        }

                        var entity = new Entity
                        {
                            new ModelComponent
                            {
                                Model = new Model
                                {
                                    mat,
                                    new Mesh
                                    {
                                        Draw = shape.CreateDebugPrimitive(graphicsDevice)
                                    }
                                }
                            }
                        };

                        var offset = addOffset ? Matrix.RotationQuaternion(shape.LocalRotation)*Matrix.Translation(shape.LocalOffset) : Matrix.Identity;

                        if (shape.Type == ColliderShapeTypes.ConvexHull)
                        {
                            var hullDesc = (ConvexHullColliderShape)shape;

                            entity.Transform.LocalMatrix = shape.DebugPrimitiveMatrix * Matrix.Scaling(hullDesc.Scaling) * offset;
                        }
                        else
                        {
                            entity.Transform.LocalMatrix = shape.DebugPrimitiveMatrix * offset;
                        }
                        entity.Transform.UseTRS = false;

                        return entity;
                    }
            }
        }
Ejemplo n.º 3
0
        private Entity CreateChildEntity(PhysicsComponent component, ColliderShape shape, bool addOffset = false)
        {
            if (shape  == null)
                return null;

            switch (shape.Type)
            {
                case ColliderShapeTypes.StaticPlane:
                    {
                        //Hmm TODO maybe can draw an infinite plane??
                        return null;
                    }
                case ColliderShapeTypes.Compound:
                    {
                        var entity = new Entity();

                        //We got to recurse
                        var compound = (CompoundColliderShape)shape;
                        for (var i = 0; i < compound.Count; i++)
                        {
                            var subShape = compound[i];
                            var subEntity = CreateChildEntity(component, subShape, true);

                            subEntity.Transform.UseTRS = false;
                            entity.AddChild(subEntity);
                        }

                        entity.Transform.LocalMatrix = Matrix.Identity;
                        entity.Transform.UseTRS = false;

                        return entity;
                    }
                default:
                    {
                        var mat = triggerMaterial;

                        if (component is RigidbodyComponent)
                        {
                            mat = ((RigidbodyComponent)component).IsKinematic ? kinematicMaterial : dynamicMaterial;

                        }
                        else if (component is CharacterComponent)
                        {
                            mat = characterMaterial;
                        }
                        else if (component is StaticColliderComponent)
                        {
                            mat = staticMaterial;
                        }

                        var entity = new Entity
                        {
                            new ModelComponent
                            {
                                Model = new Model
                                {
                                    mat,
                                    new Mesh
                                    {
                                        Draw = shape.CreateDebugPrimitive(graphicsDevice)
                                    }
                                }
                            }
                        };

                        var offset = addOffset ? Matrix.RotationQuaternion(shape.LocalRotation)*Matrix.Translation(shape.LocalOffset) : Matrix.Identity;

                        if (shape.Type == ColliderShapeTypes.ConvexHull)
                        {
                            var hullDesc = (ConvexHullColliderShape)shape;

                            entity.Transform.LocalMatrix = shape.DebugPrimitiveMatrix * Matrix.Scaling(hullDesc.Scaling) * offset;
                        }
                        else
                        {
                            entity.Transform.LocalMatrix = shape.DebugPrimitiveMatrix * offset;
                        }
                        entity.Transform.UseTRS = false;

                        return entity;
                    }
            }
        }
Ejemplo n.º 4
0
        private Entity CreateChildEntity(PhysicsComponent component, ColliderShape shape, bool addOffset = false)
        {
            if (shape == null)
            {
                return(null);
            }

            switch (shape.Type)
            {
            case ColliderShapeTypes.StaticPlane:
            {
                //Hmm TODO maybe can draw an infinite plane??
                return(null);
            }

            case ColliderShapeTypes.Compound:
            {
                var entity = new Entity();

                //We got to recurse
                var compound = (CompoundColliderShape)shape;
                for (var i = 0; i < compound.Count; i++)
                {
                    var subShape  = compound[i];
                    var subEntity = CreateChildEntity(component, subShape, true);

                    subEntity.Transform.UseTRS = false;
                    entity.AddChild(subEntity);
                }

                entity.Transform.LocalMatrix = Matrix.Identity;
                entity.Transform.UseTRS      = false;

                return(entity);
            }

            default:
            {
                var mat = triggerMaterial;

                if (component is RigidbodyComponent)
                {
                    mat = ((RigidbodyComponent)component).IsKinematic ? kinematicMaterial : dynamicMaterial;
                }
                else if (component is CharacterComponent)
                {
                    mat = characterMaterial;
                }
                else if (component is StaticColliderComponent)
                {
                    mat = staticMaterial;
                }

                var entity = new Entity
                {
                    new ModelComponent
                    {
                        Model = new Model
                        {
                            mat,
                            new Mesh
                            {
                                Draw = shape.CreateDebugPrimitive(graphicsDevice)
                            }
                        }
                    }
                };

                var offset = addOffset ? Matrix.RotationQuaternion(shape.LocalRotation) * Matrix.Translation(shape.LocalOffset) : Matrix.Identity;

                if (shape.Type == ColliderShapeTypes.ConvexHull)
                {
                    var hullDesc = (ConvexHullColliderShape)shape;

                    entity.Transform.LocalMatrix = shape.DebugPrimitiveMatrix * Matrix.Scaling(hullDesc.Scaling) * offset;
                }
                else
                {
                    entity.Transform.LocalMatrix = shape.DebugPrimitiveMatrix * offset;
                }
                entity.Transform.UseTRS = false;

                return(entity);
            }
            }
        }