Пример #1
0
        public override void Start()
        {
            simulation = this.GetSimulation();
            simulation.Gravity = new Vector3(0, 0, 0);

            if (Input.Gravity.IsSupported) // enables the orientation sensor.
                Input.Gravity.IsEnabled = true;
        }
Пример #2
0
        public override void Start()
        {
            simulation = this.GetSimulation();
            simulation.Gravity = new Vector3(0, -9, 0);

            cubeRigidBody = cube.Get<RigidbodyComponent>();
            cubeRigidBody.CanSleep = false;
            sphereRigidBody = sphere.Get<RigidbodyComponent>();
            sphereRigidBody.CanSleep = false;

            // Create the UI
            constraintNameBlock = new TextBlock
            {
                Font = Font,
                TextSize = 55,
                TextColor = Color.White,
            };
            constraintNameBlock.SetCanvasPinOrigin(new Vector3(0.5f, 0.5f, 0));
            constraintNameBlock.SetCanvasRelativePosition(new Vector3(0.5f, 0.83f, 0));

            Entity.Get<UIComponent>().Page = new UIPage
            {
                RootElement = new Canvas
                {
                    Children = 
                    { 
                        constraintNameBlock, 
                        CreateButton("Next", Font, 1), 
                        CreateButton("Previous", Font, -1) 
                    }
                }
            };

            // Create and initialize constraint
            PhysicsSampleList.Add(CreatePoint2PointConstraint);
            PhysicsSampleList.Add(CreateHingeConstraint);
            PhysicsSampleList.Add(CreateGearConstraint);
            PhysicsSampleList.Add(CreateSliderConstraint);
            PhysicsSampleList.Add(CreateConeTwistConstraint);
            PhysicsSampleList.Add(CreateGeneric6DoFConstraint);

            PhysicsSampleList[constraintIndex]();

            //Add a script for the slider constraint, to apply an impulse on collision
            cubeRigidBody.ProcessCollisions = true;
            Script.AddTask(async () =>
            {
                while (Game.IsRunning)
                {
                    var collision = await cubeRigidBody.NewCollision();
                    if (!(currentConstraint is SliderConstraint)) continue;
                    if (collision.ColliderA != sphereRigidBody && collision.ColliderB != sphereRigidBody) continue;
                    sphereRigidBody.LinearVelocity = Vector3.Zero; //clear any existing velocity
                    sphereRigidBody.ApplyImpulse(new Vector3(-25, 0, 0)); //fire impulse
                }
            });
        }
Пример #3
0
 public override void Start()
 {
     camera = Entity.Get<CameraComponent>();
     simulation = this.GetSimulation();
 }
Пример #4
0
        public static bool ScreenPositionToWorldPositionRaycast(Vector2 screenPos, CameraComponent camera, Simulation simulation, out ClickResult clickResult)
        {
            Matrix invViewProj = Matrix.Invert(camera.ViewProjectionMatrix);

            Vector3 sPos;
            sPos.X = screenPos.X * 2f - 1f;
            sPos.Y = 1f - screenPos.Y * 2f;

            sPos.Z = 0f;
            var vectorNear = Vector3.Transform(sPos, invViewProj);
            vectorNear /= vectorNear.W;

            sPos.Z = 1f;
            var vectorFar = Vector3.Transform(sPos, invViewProj);
            vectorFar /= vectorFar.W;

            clickResult.ClickedEntity = null;
            clickResult.WorldPosition = Vector3.Zero;
            clickResult.Type = ClickType.Empty;
            clickResult.HitResult = new HitResult();

            var minDistance = float.PositiveInfinity;

            var result = simulation.RaycastPenetrating(vectorNear.XYZ(), vectorFar.XYZ());
            foreach (var hitResult in result)
            {
                ClickType type = ClickType.Empty;
                
                var staticBody = hitResult.Collider as StaticColliderComponent;
                if (staticBody != null)
                {
                    if (staticBody.CollisionGroup == CollisionFilterGroups.CustomFilter1)
                        type = ClickType.Ground;

                    if (staticBody.CollisionGroup == CollisionFilterGroups.CustomFilter2)
                        type = ClickType.LootCrate;

                    if (type != ClickType.Empty)
                    {
                        var distance = (vectorNear.XYZ() - hitResult.Point).LengthSquared();
                        if (distance < minDistance)
                        {
                            minDistance = distance;
                            clickResult.Type = type;
                            clickResult.HitResult = hitResult;
                            clickResult.WorldPosition = hitResult.Point;
                            clickResult.ClickedEntity = hitResult.Collider.Entity;
                        }
                    }
                }
            }

            return (clickResult.Type != ClickType.Empty);
        }