Unmoving, collidable triangle mesh.
The acceleration structure for the mesh is created individually for each StaticMesh; if you want to create many meshes of the same model, consider using the InstancedMesh.
Inheritance: StaticCollidable
        ///<summary>
        /// Cleans up the pair handler.
        ///</summary>
        public override void CleanUp()
        {
            base.CleanUp();

            mesh = null;
            convex = null;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructs a new demo.
        /// </summary>
        /// <param name="game">Game owning this demo.</param>
        public FishInABarrelDemo(DemosGame game)
            : base(game)
        {
            game.Camera.Position = new Vector3(0, 7, 30);

            var detector = new Box(new Vector3(0, 0, 0), 1.5f, 1.5f, 1.5f);
            detector.CollisionInformation.CollisionRules.Personal = CollisionRule.NoSolver;
            var acceptedTriggerEntity = new Box(new Vector3(5, 0, 0), 1.6f, .7f, .4f, 1);
            acceptedTrigger = acceptedTriggerEntity.CollisionInformation;

            detector.Tag = "noDisplayObject";
            acceptedTriggerEntity.Tag = "noDisplayObject";
            Space.Add(detector);
            Space.Add(acceptedTriggerEntity);

            var fish = game.Content.Load<Model>("fish");
            game.ModelDrawer.Add(new DisplayEntityModel(acceptedTriggerEntity, fish, game.ModelDrawer));

            var barrelAndPlatform = game.Content.Load<Model>("barrelAndPlatform");
            Vector3[] staticTriangleVertices;
            int[] staticTriangleIndices;
            ModelDataExtractor.GetVerticesAndIndicesFromModel(barrelAndPlatform, out staticTriangleVertices, out staticTriangleIndices);

            //Note that the final 'margin' parameter is optional, but can be used to specify a collision margin on triangles in the static triangle group.
            var fishDepositoryGroup = new StaticMesh(staticTriangleVertices, staticTriangleIndices);
            CollisionRules.AddRule(fishDepositoryGroup, detector, CollisionRule.NoBroadPhase);
            Space.Add(fishDepositoryGroup);
            game.ModelDrawer.Add(fishDepositoryGroup);

            movedBox = new Box(new Vector3(-4, 5, 0), 1, 1, 1, 1);
            detector.Space.Add(movedBox);
            detector.CollisionInformation.Events.InitialCollisionDetected += InitialCollisionDetected;
            detector.CollisionInformation.Events.CollisionEnded += CollisionEnded;
        }
        ///<summary>
        /// Initializes the pair handler.
        ///</summary>
        ///<param name="entryA">First entry in the pair.</param>
        ///<param name="entryB">Second entry in the pair.</param>
        public override void Initialize(BroadPhaseEntry entryA, BroadPhaseEntry entryB)
        {

            mesh = entryA as StaticMesh;
            convex = entryB as ConvexCollidable;

            if (mesh == null || convex == null)
            {
                mesh = entryB as StaticMesh;
                convex = entryA as ConvexCollidable;

                if (mesh == null || convex == null)
                    throw new ArgumentException("Inappropriate types used to initialize pair.");
            }

            //Contact normal goes from A to B.
            broadPhaseOverlap.entryA = convex;
            broadPhaseOverlap.entryB = mesh;

            UpdateMaterialProperties(convex.entity != null ? convex.entity.material : null, mesh.material);

            base.Initialize(entryA, entryB);




        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a new instance
        /// </summary>
        /// <param name="collisionObject">The ParentObject this instance will be associated with</param>
        /// <param name="vertices">The Model vertices used to compute the BEPUPgysics StaticMesh</param>
        /// <param name="indices">The Model indices used to compute the BEPUPgysics StaticMesh</param>
        public StaticMeshCollisionMove(ICollisionObject collisionObject, Vector3[] vertices, int[] indices)
            : base(collisionObject)
        {
            ParentObject.World.Decompose(out _collisionObjectScale, out _collisionObjectRotation, out _collisionObjectTranslation);

            SpaceObject = new StaticMesh(vertices, indices, new AffineTransform(_collisionObjectScale, _collisionObjectRotation, _collisionObjectTranslation));
        }
Exemplo n.º 5
0
    // Use this for initialization
    public override void XStart()
    {
        if (!isInit)
        {
            meshfilter = transform.GetComponent <MeshFilter>();
            Collider   = transform.GetComponent <MeshCollider>();
            position   = Collider.bounds.center;
            rotation   = meshfilter.transform.rotation;
            scale      = meshfilter.transform.localScale;

            mesh = meshfilter.sharedMesh;

            if (transform.GetComponent <Rigidbody>() != null)
            {
                rigid = transform.GetComponent <Rigidbody>();
                mass  = rigid.mass;
            }

            List <BEPUutilities.Vector3> vertices = new List <BEPUutilities.Vector3>();
            int[] indices = new int[mesh.GetIndexCount(0)];
            indices = mesh.GetIndices(0);

            for (int i = 0; i < mesh.vertices.Length; i++)
            {
                vertices.Add(mesh.vertices[i].ToBEPU());
            }

            entity           = new BEPUphysics.BroadPhaseEntries.StaticMesh(vertices.ToArray(), indices, new AffineTransform(scale.ToBEPU(), rotation.ToBEPU(), position.ToBEPU()));
            entity.Sidedness = TriangleSidedness.DoubleSided;
            PhysicsManagerBehavior.Space.Add(entity);

            this.enabled = false;
            isInit       = true;
        }
    }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a new instance
        /// </summary>
        /// <param name="collisionObject">The ParentObject this instance will be associated with</param>
        /// <param name="model">The Model used to compute the BEPUPgysics StaticMesh</param>
        public StaticMeshCollisionMove(ICollisionObject collisionObject, Model model) : base(collisionObject)
        {
            Vector3[] vertices;
            int[] indices;

            TriangleMesh.GetVerticesAndIndicesFromModel(model, out vertices, out indices);

            ParentObject.World.Decompose(out _collisionObjectScale, out _collisionObjectRotation, out _collisionObjectTranslation);

            SpaceObject = new StaticMesh(vertices, indices, new AffineTransform(_collisionObjectScale, _collisionObjectRotation, _collisionObjectTranslation));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Adds static mesh to phys space.
        /// </summary>
        /// <param name="mesh"></param>
        /// <param name="transform"></param>
        void AddStaticCollisionMesh( Mesh mesh, Matrix transform )
        {
            var indices		=	mesh.GetIndices();
            var vertices	=	mesh.Vertices
                                .Select( v1 => Vector3.TransformCoordinate( v1.Position, transform ) )
                                .Select( v2 => MathConverter.Convert( v2 ) )
                                .ToArray();

            var staticMesh = new StaticMesh( vertices, indices );
            staticMesh.Sidedness = BEPUutilities.TriangleSidedness.Clockwise;
            physSpace.Add( staticMesh );
        }
Exemplo n.º 8
0
        /// <summary>
        /// Constructs a new demo.
        /// </summary>
        /// <param name="game">Game owning this demo.</param>
        public AddRemoveStressDemo(DemosGame game)
            : base(game)
        {
            Space.Remove(vehicle.Vehicle);

            var compoundShape = new CompoundShape(new List<CompoundShapeEntry>
                {
                    new CompoundShapeEntry(new BoxShape(1, 1, 1), new Vector3(0, 1, 0), 1),
                    new CompoundShapeEntry(new BoxShape(2, 1, 2), new Vector3(), 1),
                    new CompoundShapeEntry(new BoxShape(1, 1, 1), new Vector3(0, -1, 0), 1)
                });
            for (int i = 0; i < 300; ++i)
            {
                var toAdd = new Entity(compoundShape, 10);
                addedEntities.Add(toAdd);
            }

            var boxShape = new BoxShape(1, 1, 1);
            for (int i = 0; i < 300; ++i)
            {
                var toAdd = new Entity(boxShape, 10);
                addedEntities.Add(toAdd);
            }

            Vector3[] vertices;
            int[] indices;
            ModelDataExtractor.GetVerticesAndIndicesFromModel(game.Content.Load<Model>("cube"), out vertices, out indices);
            var mobileMeshShape = new MobileMeshShape(vertices, indices, new AffineTransform(Matrix3x3.CreateScale(1, 2, 1), new Vector3()), MobileMeshSolidity.Counterclockwise);
            for (int i = 0; i < 300; ++i)
            {
                var toAdd = new Entity(mobileMeshShape, 10);
                addedEntities.Add(toAdd);
            }

            for (int i = 0; i < addedEntities.Count; ++i)
            {
                var entity = addedEntities[i];
                entity.Gravity = new Vector3();
                entity.Position = GetRandomPosition(random);
                entity.LinearVelocity = 3 * Vector3.Normalize(entity.Position);
                Space.Add(entity);
            }

            var playgroundModel = game.Content.Load<Model>("playground");
            ModelDataExtractor.GetVerticesAndIndicesFromModel(playgroundModel, out vertices, out indices);
            var staticMesh = new StaticMesh(vertices, indices, new AffineTransform(Matrix3x3.CreateFromAxisAngle(Vector3.Up, MathHelper.Pi), new Vector3(0, -30, 0)));
            staticMesh.Sidedness = TriangleSidedness.Counterclockwise;

            Space.Add(staticMesh);
            game.ModelDrawer.Add(staticMesh);

            game.Camera.Position = new Vector3(0, 6, 15);
        }
        ///<summary>
        /// Initializes the manifold.
        ///</summary>
        ///<param name="newCollidableA">First collidable.</param>
        ///<param name="newCollidableB">Second collidable.</param>
        public override void Initialize(Collidable newCollidableA, Collidable newCollidableB)
        {
            convex = newCollidableA as ConvexCollidable;
            mesh = newCollidableB as StaticMesh;

            if (convex == null || mesh == null)
            {
                convex = newCollidableB as ConvexCollidable;
                mesh = newCollidableA as StaticMesh;
                if (convex == null || mesh == null)
                    throw new ArgumentException("Inappropriate types used to initialize contact manifold.");
            }
        }
        ///<summary>
        /// Initializes the pair handler.
        ///</summary>
        ///<param name="entryA">First entry in the pair.</param>
        ///<param name="entryB">Second entry in the pair.</param>
        public override void Initialize(BroadPhaseEntry entryA, BroadPhaseEntry entryB)
        {
            mesh = entryA as StaticMesh;
            if (mesh == null)
            {
                mesh = entryB as StaticMesh;
                if (mesh == null)
                {
                    throw new ArgumentException("Inappropriate types used to initialize pair.");
                }
            }

            base.Initialize(entryA, entryB);
        }
        /// <summary>
        /// Constructs a new demo.
        /// </summary>
        /// <param name="game">Game owning this demo.</param>
        public StaticMeshDemo(DemosGame game)
            : base(game)
        {
            //Load in mesh data and create the collision mesh.
            Vector3[] staticTriangleVertices;
            int[] staticTriangleIndices;

            var playgroundModel = game.Content.Load<Model>("playground");
            //This is a little convenience method used to extract vertices and indices from a model.
            //It doesn't do anything special; any approach that gets valid vertices and indices will work.
            ModelDataExtractor.GetVerticesAndIndicesFromModel(playgroundModel, out staticTriangleVertices, out staticTriangleIndices);
            var staticMesh = new StaticMesh(staticTriangleVertices, staticTriangleIndices, new AffineTransform(Matrix3x3.CreateFromAxisAngle(Vector3.Up, MathHelper.Pi), new Vector3(0, -10, 0)));
            staticMesh.Sidedness = TriangleSidedness.Counterclockwise;

            Space.Add(staticMesh);
            game.ModelDrawer.Add(staticMesh);


            //Dump some boxes on top of it for fun.
            int numColumns = 8;
            int numRows = 8;
            int numHigh = 1;
            float separation = 8;
            for (int i = 0; i < numRows; i++)
                for (int j = 0; j < numColumns; j++)
                    for (int k = 0; k < numHigh; k++)
                    {
                        var toAdd = new Box(
                            new Vector3(
                            separation * i - numRows * separation / 2,
                            30f + k * separation,
                            separation * j - numColumns * separation / 2),
                            2, 2, 2, 15);
                        Space.Add(toAdd);
                    }



            game.Camera.Position = new Vector3(0, 10, 40);


        }
        /// <summary>
        /// Constructs a new demo.
        /// </summary>
        /// <param name="game">Game owning this demo.</param>
        public CharacterPlaygroundDemo(DemosGame game)
            : base(game)
        {

            game.Camera.Position = new Vector3(-10, 7, 5);
            game.Camera.ViewDirection = new Vector3(0, 0, 1);
            //Since this is the character playground, turn on the character by default.
            character.Activate();
            //Having the character body visible would be a bit distracting.
            character.CharacterController.Body.Tag = "noDisplayObject";

            //Load in mesh data for the environment.
            Vector3[] staticTriangleVertices;
            int[] staticTriangleIndices;


            var playgroundModel = game.Content.Load<Model>("CharacterControllerTestTerrain");
            //This is a little convenience method used to extract vertices and indices from a model.
            //It doesn't do anything special; any approach that gets valid vertices and indices will work.
            ModelDataExtractor.GetVerticesAndIndicesFromModel(playgroundModel, out staticTriangleVertices, out staticTriangleIndices);
            var staticMesh = new StaticMesh(staticTriangleVertices, staticTriangleIndices, new AffineTransform(new Vector3(0.01f, 0.01f, 0.01f), Quaternion.Identity, new Vector3(0, 0, 0)));
            staticMesh.Sidedness = TriangleSidedness.Counterclockwise;

            Space.Add(staticMesh);
            game.ModelDrawer.Add(staticMesh);



            //Add a spinning blade for the character to ram itself into.
            var fanBase = new Cylinder(new Vector3(-13, .5f, 50), 1.1f, 1);
            var fanBlade = new Box(fanBase.Position + new Vector3(0, .8f, 0), 5, .1f, 1f, 5);
            var fanJoint = new RevoluteJoint(fanBase, fanBlade, (fanBase.Position + fanBlade.Position) * .5f, Vector3.Up);
            fanJoint.Motor.IsActive = true;
            fanJoint.Motor.Settings.VelocityMotor.GoalVelocity = 30;
            fanJoint.Motor.Settings.MaximumForce = 300;
            Space.Add(fanBase);
            Space.Add(fanBlade);
            Space.Add(fanJoint);

            //Add a bridge connecting the two towers.
            Vector3 startPosition = new Vector3(-19.3f, 10.5f - .25f, 23 - .85f);
            var startPlatform = new Box(startPosition - new Vector3(0, 0, 2.2f), 4, .5f, 6);
            Space.Add(startPlatform);
            Vector3 offset = new Vector3(0, 0, 1.7f);
            Box previousLink = startPlatform;
            Vector3 position = new Vector3();
            for (int i = 1; i <= 7; i++)
            {
                position = startPosition + offset * i;
                Box link = new Box(position, 3, .3f, 1.5f, 50);
                link.LinearDamping = .1f;
                link.AngularDamping = .1f;
                Space.Add(link);
                Space.Add(new RevoluteJoint(previousLink, link, position - offset * .5f, Vector3.Right));

                previousLink = link;
            }
            var endPlatform = new Box(position - new Vector3(0, 0, -3.8f), 4, .5f, 6);
            Space.Add(endPlatform);

            Space.Add(new RevoluteJoint(previousLink, endPlatform, position + offset * .5f, Vector3.Right));


            //Add in a floating platform controlled by a curve to serve as an elevator.
            Entity movingEntity = new Box(new Vector3(-10, 0, -10), 3, 1, 3);

            var positionCurve = new CardinalSpline3D();

            positionCurve.PreLoop = CurveEndpointBehavior.Mirror;
            positionCurve.PostLoop = CurveEndpointBehavior.Mirror;

            positionCurve.ControlPoints.Add(-1, new Vector3(-19.3f, 0, 43));
            positionCurve.ControlPoints.Add(0, new Vector3(-19.3f, 0, 43));
            positionCurve.ControlPoints.Add(2, new Vector3(-19.3f, 0, 43));
            positionCurve.ControlPoints.Add(3, new Vector3(-19.3f, 0, 43));
            positionCurve.ControlPoints.Add(4, new Vector3(-19.3f, 5, 43));
            positionCurve.ControlPoints.Add(5f, new Vector3(-19.3f, 10, 43));
            positionCurve.ControlPoints.Add(6f, new Vector3(-19.3f, 10, 43));
            positionCurve.ControlPoints.Add(8f, new Vector3(-19.3f, 10, 43));
            positionCurve.ControlPoints.Add(9f, new Vector3(-19.3f, 10, 43));

            elevatorMover = new EntityMover(movingEntity);
            Space.Add(elevatorMover);
            Space.Add(movingEntity);

            elevatorPath = positionCurve;

            //Add in another floating platform controlled by a curve for horizontal transport.
            movingEntity = new Box(new Vector3(-10, 0, -10), 2.5f, .5f, 2.5f);

            var platformCurve = new LinearInterpolationCurve3D();

            platformCurve.PreLoop = CurveEndpointBehavior.Mirror;
            platformCurve.PostLoop = CurveEndpointBehavior.Mirror;

            platformCurve.ControlPoints.Add(0, new Vector3(-1.75f, 10, 21.5f));
            platformCurve.ControlPoints.Add(2, new Vector3(-1.75f, 10, 21.5f));
            platformCurve.ControlPoints.Add(5, new Vector3(-1.75f, 10, 15.5f));
            platformCurve.ControlPoints.Add(10, new Vector3(-19.3f, 10, 15.5f));
            platformCurve.ControlPoints.Add(12, new Vector3(-19.3f, 10, 15.5f));
            platformCurve.ControlPoints.Add(15, new Vector3(-25, 10, 15.5f));
            platformCurve.ControlPoints.Add(22, new Vector3(-25, 10, 38));
            platformCurve.ControlPoints.Add(23, new Vector3(-22.75f, 10, 38));
            platformCurve.ControlPoints.Add(25, new Vector3(-22.75f, 10, 38));

            //Make it spin too.  That'll be fun.  Or something.
            var platformRotationCurve = new QuaternionSlerpCurve();
            platformRotationCurve.PreLoop = CurveEndpointBehavior.Mirror;
            platformRotationCurve.PostLoop = CurveEndpointBehavior.Mirror;
            platformRotationCurve.ControlPoints.Add(0, Quaternion.Identity);
            platformRotationCurve.ControlPoints.Add(15, Quaternion.Identity);
            platformRotationCurve.ControlPoints.Add(22, Quaternion.CreateFromAxisAngle(Vector3.Up, MathHelper.PiOver2));
            platformRotationCurve.ControlPoints.Add(25, Quaternion.CreateFromAxisAngle(Vector3.Up, MathHelper.PiOver2));

            platformMover = new EntityMover(movingEntity);
            platformRotator = new EntityRotator(movingEntity);
            Space.Add(platformMover);
            Space.Add(platformRotator);
            Space.Add(movingEntity);

            platformPath = platformCurve;
            platformOrientationPath = platformRotationCurve;

            //Add in a diving board.

            Box divingBoardBase = new Box(new Vector3(-9, 10, 39.3f), 5, 1, 3);
            Box divingBoard = new Box(divingBoardBase.Position + new Vector3(-2, 0, 3.5f), 1, .3f, 3, 5);
            var divingBoardJoint = new RevoluteJoint(divingBoardBase, divingBoard, divingBoard.Position + new Vector3(0, 0, -1.5f), Vector3.Right);
            divingBoardJoint.Motor.IsActive = true;
            divingBoardJoint.Motor.Settings.Mode = MotorMode.Servomechanism;
            divingBoardJoint.Motor.Settings.Servo.Goal = 0;
            divingBoardJoint.Motor.Settings.Servo.SpringSettings.Stiffness = 5000;
            divingBoardJoint.Motor.Settings.Servo.SpringSettings.Damping = 0;

            Space.Add(divingBoardBase);
            Space.Add(divingBoard);
            Space.Add(divingBoardJoint);


            //Add a second diving board for comparison.

            Box divingBoard2 = new Box(divingBoardBase.Position + new Vector3(2, 0, 5f), 1, .3f, 6, 5);
            var divingBoardJoint2 = new RevoluteJoint(divingBoardBase, divingBoard2, divingBoard2.Position + new Vector3(0, 0, -3), Vector3.Right);
            divingBoardJoint2.Motor.IsActive = true;
            divingBoardJoint2.Motor.Settings.Mode = MotorMode.Servomechanism;
            divingBoardJoint2.Motor.Settings.Servo.Goal = 0;
            divingBoardJoint2.Motor.Settings.Servo.SpringSettings.Stiffness = 10000;
            divingBoardJoint2.Motor.Settings.Servo.SpringSettings.Damping = 0;

            Space.Add(divingBoard2);
            Space.Add(divingBoardJoint2);

            //Add a seesaw for people to jump on.
            Box seesawBase = new Box(new Vector3(-7, .45f, 52), 1, .9f, .3f);
            Box seesawPlank = new Box(seesawBase.Position + new Vector3(0, .65f, 0), 1.2f, .2f, 6, 3);
            RevoluteJoint seesawJoint = new RevoluteJoint(seesawBase, seesawPlank, seesawPlank.Position, Vector3.Right);
            Space.Add(seesawJoint);
            Space.Add(seesawBase);
            Space.Add(seesawPlank);

            Space.Add(new Box(seesawPlank.Position + new Vector3(0, 1.3f, 2), 1, 1, 1, 5));


            //Add in some boxes to bump and jump on.
            int numColumns = 3;
            int numRows = 3;
            int numHigh = 3;
            float xSpacing = 1.01f;
            float ySpacing = 1.01f;
            float zSpacing = 1.01f;
            for (int i = 0; i < numRows; i++)
                for (int j = 0; j < numColumns; j++)
                    for (int k = 0; k < numHigh; k++)
                    {
                        Space.Add(new Box(new Vector3(
                                                 5 + xSpacing * i - (numRows - 1) * xSpacing / 2f,
                                                 1.58f + k * (ySpacing),
                                                 45 + zSpacing * j - (numColumns - 1) * zSpacing / 2f),
                                             .5f, .5f, .5f, 5));
                    }



            //Add a log to roll!
            //Make it a compound so some boxes can be added to let the player know it's actually spinning.
            CompoundBody log = new CompoundBody(new List<CompoundShapeEntry>()
            {
                new CompoundShapeEntry(new CylinderShape(4, 1.8f), Quaternion.CreateFromAxisAngle(Vector3.Forward, MathHelper.PiOver2), 20),
                new CompoundShapeEntry(new BoxShape(.5f, .5f, 3.7f),  new Vector3(1.75f, 0,0), 0),
                new CompoundShapeEntry(new BoxShape(.5f, 3.7f, .5f), new Vector3(1.75f, 0,0), 0),
                new CompoundShapeEntry(new BoxShape(.5f, .5f, 3.7f),  new Vector3(-1.75f, 0,0), 0),
                new CompoundShapeEntry(new BoxShape(.5f, 3.7f, .5f), new Vector3(-1.75f, 0,0), 0)
            }, 50);
            log.Position = new Vector3(-14.5f, 10, 41);
            log.AngularDamping = 0;


            RevoluteJoint logJointA = new RevoluteJoint(divingBoardBase, log, log.Position + new Vector3(2.5f, 0, 0), Vector3.Right);
            RevoluteJoint logJointB = new RevoluteJoint(endPlatform, log, log.Position + new Vector3(-2.5f, 0, 0), Vector3.Right);
            Space.Add(logJointA);
            Space.Add(logJointB);

            Space.Add(log);


            //Put some planks to stand on that show various slopes.
            int numPads = 10;
            for (int i = 0; i < numPads; i++)
            {
                offset = new Vector3(0, 0, 4);
                Box a = new Box(new Vector3(i * 1.5f + 3.5f, 10, 24), 1.5f, 1, 4);
                Box b = new Box(new Vector3(i * 1.5f + 3.5f, 10, 24), 1.5f, 1, 4);
                float angle = -i * MathHelper.PiOver2 / numPads;
                b.Orientation = Quaternion.CreateFromAxisAngle(Vector3.Right, angle);
                b.Position += offset * .5f + Quaternion.Transform(offset * .5f, b.Orientation);

                Space.Add(a);
                Space.Add(b);
            }

        }
Exemplo n.º 13
0
    // Start is called before the first frame update
    void Start()
    {
        world.ForceUpdater.Gravity = new BEPUutilities.Vector3(0, -9.81f, 0);

        // floor
        world.Add(new Box(new BEPUutilities.Vector3(0, -1.0f, 0), 30, 1, 30));


        GameObject prefab = Resources.Load("elsa") as GameObject;
        GameObject actor  = MonoBehaviour.Instantiate(prefab, new Vector3(0, 3, 0), Quaternion.Euler(new Vector3(0, 0, 0))) as GameObject;
        var        ac     = actor.AddComponent <testActor>();

        ac.cc = new BEPUphysics.Character.CharacterController(new BEPUutilities.Vector3(0, 3, 0), 1.0f, 1.0f * 0.7f, 1.0f * 0.3f, 0.5f, 0.001f, 10f, 0.8f, 1.3f, 8f, 3f, 1.5f, 1000f, 0, 0, 0, 0, 0, 0);
        world.Add(ac.cc);


        var vertices = new BEPUutilities.Vector3[]
        {
            new BEPUutilities.Vector3(-0.454f, 0.087f, -0.413f),
            new BEPUutilities.Vector3(-0.454f, 0.087f, 0.29f),
            new BEPUutilities.Vector3(-0.454f, 0.79f, -0.413f),
            new BEPUutilities.Vector3(0.454f, 0.79f, -0.413f),
            new BEPUutilities.Vector3(0.454f, 0.087f, 0.29f),
            new BEPUutilities.Vector3(0.454f, 0.087f, -0.413f),
            new BEPUutilities.Vector3(-0.333f, 1f, -0.5f),
            new BEPUutilities.Vector3(-0.333f, 0f, 0.5f),
            new BEPUutilities.Vector3(0.333f, 1f, -0.5f),
            new BEPUutilities.Vector3(0.333f, 0f, 0.5f),
            new BEPUutilities.Vector3(-0.333f, 1f, -0.5f),
            new BEPUutilities.Vector3(-0.5f, 0f, -0.5f),
            new BEPUutilities.Vector3(-0.5f, 1f, -0.5f),
            new BEPUutilities.Vector3(0.5f, 0f, -0.5f),
            new BEPUutilities.Vector3(0.333f, 1f, -0.5f),
            new BEPUutilities.Vector3(0.5f, 1f, -0.5f),
            new BEPUutilities.Vector3(-0.5f, 1f, -0.5f),
            new BEPUutilities.Vector3(-0.474f, 1.058f, -0.5f),
            new BEPUutilities.Vector3(-0.355f, 1.058f, -0.5f),
            new BEPUutilities.Vector3(-0.5f, 0f, -0.5f),
            new BEPUutilities.Vector3(-0.333f, 0f, 0.5f),
            new BEPUutilities.Vector3(-0.5f, 0f, 0.5f),
            new BEPUutilities.Vector3(0.333f, 0f, 0.5f),
            new BEPUutilities.Vector3(0.5f, 0f, -0.5f),
            new BEPUutilities.Vector3(0.5f, 0f, 0.5f),
            new BEPUutilities.Vector3(0.355f, 0.058f, 0.5f),
            new BEPUutilities.Vector3(0.474f, 1.058f, -0.5f),
            new BEPUutilities.Vector3(0.355f, 1.058f, -0.5f),
            new BEPUutilities.Vector3(0.474f, 0.058f, 0.5f),
            new BEPUutilities.Vector3(-0.355f, 1.058f, -0.5f),
            new BEPUutilities.Vector3(-0.474f, 1.058f, -0.5f),
            new BEPUutilities.Vector3(-0.474f, 0.058f, 0.5f),
            new BEPUutilities.Vector3(-0.355f, 0.058f, 0.5f),
            new BEPUutilities.Vector3(0.333f, 1f, -0.5f),
            new BEPUutilities.Vector3(0.474f, 1.058f, -0.5f),
            new BEPUutilities.Vector3(0.5f, 1f, -0.5f),
            new BEPUutilities.Vector3(0.355f, 1.058f, -0.5f),
            new BEPUutilities.Vector3(0.474f, 0.058f, 0.5f),
            new BEPUutilities.Vector3(0.5f, 0f, 0.5f),
            new BEPUutilities.Vector3(0.5f, 1f, -0.5f),
            new BEPUutilities.Vector3(0.474f, 1.058f, -0.5f),
            new BEPUutilities.Vector3(0.333f, 0f, 0.5f),
            new BEPUutilities.Vector3(0.5f, 0f, 0.5f),
            new BEPUutilities.Vector3(0.474f, 0.058f, 0.5f),
            new BEPUutilities.Vector3(0.355f, 0.058f, 0.5f),
            new BEPUutilities.Vector3(0.355f, 0.058f, 0.5f),
            new BEPUutilities.Vector3(0.355f, 1.058f, -0.5f),
            new BEPUutilities.Vector3(0.333f, 1f, -0.5f),
            new BEPUutilities.Vector3(0.333f, 0f, 0.5f),
            new BEPUutilities.Vector3(-0.333f, 0f, 0.5f),
            new BEPUutilities.Vector3(-0.474f, 0.058f, 0.5f),
            new BEPUutilities.Vector3(-0.5f, 0f, 0.5f),
            new BEPUutilities.Vector3(-0.355f, 0.058f, 0.5f),
            new BEPUutilities.Vector3(-0.474f, 0.058f, 0.5f),
            new BEPUutilities.Vector3(-0.474f, 1.058f, -0.5f),
            new BEPUutilities.Vector3(-0.5f, 1f, -0.5f),
            new BEPUutilities.Vector3(-0.5f, 0f, 0.5f),
            new BEPUutilities.Vector3(-0.355f, 0.058f, 0.5f),
            new BEPUutilities.Vector3(-0.333f, 0f, 0.5f),
            new BEPUutilities.Vector3(-0.333f, 1f, -0.5f),
            new BEPUutilities.Vector3(-0.355f, 1.058f, -0.5f),
            new BEPUutilities.Vector3(-0.5f, 0.049f, -0.451f),
            new BEPUutilities.Vector3(-0.454f, 0.79f, -0.413f),
            new BEPUutilities.Vector3(-0.5f, 0.882f, -0.451f),
            new BEPUutilities.Vector3(-0.454f, 0.087f, -0.413f),
            new BEPUutilities.Vector3(-0.5f, 0.049f, 0.382f),
            new BEPUutilities.Vector3(-0.5f, 0.882f, -0.451f),
            new BEPUutilities.Vector3(-0.454f, 0.79f, -0.413f),
            new BEPUutilities.Vector3(-0.454f, 0.087f, 0.29f),
            new BEPUutilities.Vector3(-0.5f, 0.049f, -0.451f),
            new BEPUutilities.Vector3(-0.5f, 0.049f, 0.382f),
            new BEPUutilities.Vector3(-0.454f, 0.087f, 0.29f),
            new BEPUutilities.Vector3(-0.454f, 0.087f, -0.413f),
            new BEPUutilities.Vector3(0.5f, 0.049f, -0.451f),
            new BEPUutilities.Vector3(0.5f, 0.882f, -0.451f),
            new BEPUutilities.Vector3(0.454f, 0.79f, -0.413f),
            new BEPUutilities.Vector3(0.454f, 0.087f, -0.413f),
            new BEPUutilities.Vector3(0.454f, 0.087f, 0.29f),
            new BEPUutilities.Vector3(0.5f, 0.049f, -0.451f),
            new BEPUutilities.Vector3(0.454f, 0.087f, -0.413f),
            new BEPUutilities.Vector3(0.5f, 0.049f, 0.382f),
            new BEPUutilities.Vector3(0.5f, 0.049f, 0.382f),
            new BEPUutilities.Vector3(0.454f, 0.79f, -0.413f),
            new BEPUutilities.Vector3(0.5f, 0.882f, -0.451f),
            new BEPUutilities.Vector3(0.454f, 0.087f, 0.29f),
            new BEPUutilities.Vector3(-0.5f, 0f, -0.5f),
            new BEPUutilities.Vector3(-0.5f, 0.049f, -0.451f),
            new BEPUutilities.Vector3(-0.5f, 0.882f, -0.451f),
            new BEPUutilities.Vector3(-0.5f, 0.049f, 0.382f),
            new BEPUutilities.Vector3(-0.5f, 1f, -0.5f),
            new BEPUutilities.Vector3(-0.5f, 0f, 0.5f),
            new BEPUutilities.Vector3(0.5f, 0.882f, -0.451f),
            new BEPUutilities.Vector3(0.5f, 0.049f, -0.451f),
            new BEPUutilities.Vector3(0.5f, 0f, -0.5f),
            new BEPUutilities.Vector3(0.5f, 0.049f, 0.382f),
            new BEPUutilities.Vector3(0.5f, 1f, -0.5f),
            new BEPUutilities.Vector3(0.5f, 0f, 0.5f),
        };

        int[] indices = new int[]
        {
            0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 9, 10, 11, 12, 10, 13, 11, 10, 14, 13, 13, 14, 15, 10, 16, 17, 17, 18, 10, 19, 20, 21, 22, 20, 19, 19, 23, 22, 22, 23, 24, 25, 26, 27, 26, 25, 28, 29, 30, 31, 31, 32, 29, 33, 34, 35, 33, 36, 34, 37, 38, 39, 37, 39, 40, 41, 42, 43, 43, 44, 41, 45, 46, 47, 47, 48, 45, 49, 50, 51, 49, 52, 50, 53, 54, 55, 55, 56, 53, 57, 58, 59, 57, 59, 60, 61, 62, 63, 61, 64, 62, 65, 66, 67, 67, 68, 65, 69, 70, 71, 71, 72, 69, 73, 74, 75, 75, 76, 73, 77, 78, 79, 78, 77, 80, 81, 82, 83, 81, 84, 82, 85, 86, 87, 88, 86, 85, 85, 87, 89, 89, 87, 88, 85, 90, 88, 89, 88, 90, 91, 92, 93, 93, 92, 94, 93, 95, 91, 94, 91, 95, 93, 94, 96, 95, 96, 94
        };

        var stair = new BEPUphysics.BroadPhaseEntries.StaticMesh(vertices, indices, new BEPUutilities.AffineTransform(new BEPUutilities.Vector3(0.5f, 0.5f, 0.5f), BEPUutilities.Quaternion.Identity, new BEPUutilities.Vector3(0, 0, 0)));

        world.Add(stair);
    }
Exemplo n.º 14
0
        /// <summary>
        /// Constructs a new demo.
        /// </summary>
        /// <param name="game">Game owning this demo.</param>
        public ScaleDemo(DemosGame game)
            : base(game)
        {
            //Pick a scale!
            //Beware: If you go too far (particularly 0.01 and lower) issues could start to crop up.
            float scale = 1;

            //Load in mesh data and create the collision mesh.
            //The 'mesh' will be a supergiant triangle.
            //Triangles in meshes have a collision detection system which bypasses most numerical issues for collisions on the face of the triangle.
            //Edge collisions fall back to the general case collision detection system which is susceptible to numerical issues at extreme scales.
            //For our simulation, the edges will be too far away to worry about!
            Vector3[] vertices;
            int[] indices;
            vertices = new Vector3[] { new Vector3(-10000, 0, -10000), new Vector3(-10000, 0, 20000), new Vector3(20000, 0, -10000) };
            indices = new int[] { 2, 1, 0 };
            var staticMesh = new StaticMesh(vertices, indices, new AffineTransform(Matrix3x3.CreateFromAxisAngle(Vector3.Up, MathHelper.Pi), new Vector3(0, 0, 0)));
            staticMesh.Sidedness = TriangleSidedness.Counterclockwise;

            Space.Add(staticMesh);
            game.ModelDrawer.Add(staticMesh);

            //Since everything's pretty large, increase the gravity a whole bunch to make things fall fast.
            Space.ForceUpdater.Gravity *= scale;

            //Change the various engine tuning factors so that collision detection and collision response handle the changed scale better.
            ConfigurationHelper.ApplyScale(Space, scale);

            //When dealing with objects that generally have high velocities and accelerations relative to their size, having a shorter time step duration can boost quality
            //a whole lot.  Once the configuration is set properly, most of any remaining 'unsmoothness' in the simulation is due to a lack of temporal resolution;
            //one discrete step can take an object from a valid state to an unpleasing state due to the high rates of motion.
            //To simulate the same amount of time with a smaller time step duration requires taking more time steps.
            //This is a quality-performance tradeoff.  If you want to do this, set the time step duration like so:

            //Space.TimeStepSettings.TimeStepDuration = 1 / 120f;

            //And then, in the update, either call the Space.Update() method proportionally more often or use the Space.Update(dt) version, which takes as many timesteps are necessary to simulate dt time.
            //Watch out: when using the internal timestepping method, you may notice slight motion jitter since the number of updates per frame isn't fixed.  Interpolation buffers can be used
            //to address this; check the Asynchronous Update documentation for more information on using internal time stepping.
            //[Asynchronously updating isn't required to use internal timestepping, but it is a common use case.]

            //Dump some boxes on top of it for fun.
            int numColumns = 8;
            int numRows = 8;
            int numHigh = 1;
            float separation = 2 * scale;
            float baseWidth = 0.5f;
            float baseHeight = 1;
            float baseLength = 1.5f;
            Entity toAdd;
            for (int i = 0; i < numRows; i++)
                for (int j = 0; j < numColumns; j++)
                    for (int k = 0; k < numHigh; k++)
                    {
                        toAdd = new Box(
                            new Vector3(
                            separation * i - numRows * separation / 2,
                            2 * scale + k * separation,
                            separation * j - numColumns * separation / 2),
                            baseWidth * scale, baseHeight * scale, baseLength * scale, 15);

                        Space.Add(toAdd);
                    }

            //Dump some stuff on top of it that use general case collision detection when they collide with boxes.
            numColumns = 3;
            numRows = 3;
            numHigh = 4;
            separation = 2 * scale;
            baseWidth = 1f;
            baseHeight = 1;
            for (int i = 0; i < numRows; i++)
                for (int j = 0; j < numColumns; j++)
                    for (int k = 0; k < numHigh; k++)
                    {
                        toAdd = new Cylinder(
                            new Vector3(
                            separation * i - numRows * separation / 2,
                            8 * scale + k * separation,
                            separation * j - numColumns * separation / 2),
                            baseHeight * scale, 0.5f * baseWidth * scale,  15);

                        Space.Add(toAdd);
                    }

            game.Camera.Position = scale * new Vector3(0, 4, 10);
            originalCameraSpeed = freeCameraControlScheme.Speed;
            freeCameraControlScheme.Speed *= scale;
        }
        static void Build14(int gridSize, out VertexModifier vertexModifier, out Dictionary<string, MeshRebuilder> rebuilders)
        {
            List<Vector3B> vertices = new List<Vector3B>();

            for (int y = 0; y < gridSize; ++y)
                for (int x = 0; x < gridSize; ++x)
                    vertices.Add(new Vector3B(2 * x, 0, 2 * y));

            List<int> triangles = new List<int>();

            for (int y = 0; y < gridSize - 1; ++y)
                for (int x = 0; x < gridSize - 1; ++x)
                {
                    triangles.Add(gridSize * (y) + (x));
                    triangles.Add(gridSize * (y) + (x + 1));
                    triangles.Add(gridSize * (y + 1) + (x + 1));

                    triangles.Add(gridSize * (y) + (x));
                    triangles.Add(gridSize * (y + 1) + (x + 1));
                    triangles.Add(gridSize * (y + 1) + (x));
                }

            var physicsMesh = new BEPUphysics.BroadPhaseEntries.StaticMesh(vertices.ToArray(), triangles.ToArray(), BEPUutilities.AffineTransform.Identity);

            vertexModifier = (Random random) =>
            {
                for (int vertexIndex = 0; vertexIndex < physicsMesh.Mesh.Data.Vertices.Length; ++vertexIndex)
                    physicsMesh.Mesh.Data.Vertices[vertexIndex].Y = (float)random.NextDouble() * 2;
            };

            rebuilders = new Dictionary<string, MeshRebuilder>();
            rebuilders.Add("v1.4.0 Reconstruct", (i) =>
            {
                physicsMesh.Mesh.Tree.Reconstruct();
                physicsMesh.UpdateBoundingBox();
            });
            rebuilders.Add("v1.4.0 Refit", (i) =>
            {
                physicsMesh.Mesh.Tree.Refit();
                physicsMesh.UpdateBoundingBox();
            });
        }
Exemplo n.º 16
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"/>
 private void OnStaticMeshCollisionDetected(StaticMesh sender, Collidable other, CollidablePairHandler pair)
 {
     OnCollisionDetected(sender, other, pair);
 }