public void Activate(bool active)
 {
     if (!isInit())
     {
         var timer = new PhyInterval(1, Player.simulator);
         timer.Completed += OnTick;
         golfBallRef      = Player.golfball.GetReference();
         vehicle          = new Vehicle(Player.golfball);
         Player.simulator.Simulation.Awakener.AwakenBody(golfBallRef.Handle);
     }
     if (isInit())
     {
         //golfBallRef.
         QuixConsole.Log("Gauntlet", active);
         vehicle.isActive = active;
         Player.simulator.Simulation.Awakener.AwakenBody(golfBallRef.Handle);
     }
     if (!hasStopped)
     {
         // Player.golfball.Stop();
         hasStopped = true;
     }
     if (!active)
     {
         hasStopped = false;
     }
 }
Exemple #2
0
        public override void Update(Window window, Camera camera, Input input, float dt)
        {
            if (input.WasPushed(ToggleCar))
            {
                playerControlActive = !playerControlActive;
            }
            //For control purposes, we'll match the fixed update rate of the simulation. Could decouple it- this dt isn't
            //vulnerable to the same instabilities as the simulation itself with variable durations.
            const float controlDt = 1 / 60f;

            if (playerControlActive)
            {
                float steeringSum = 0;
                if (input.IsDown(Left))
                {
                    steeringSum += 1;
                }
                if (input.IsDown(Right))
                {
                    steeringSum -= 1;
                }
                var targetSpeedFraction = input.IsDown(Forward) ? 1f : input.IsDown(Backward) ? -1f : 0;
                var zoom = input.IsDown(Zoom);
                playerController.Update(Simulation, controlDt, steeringSum, targetSpeedFraction, zoom, input.IsDown(Brake) || input.IsDown(BrakeAlternate));
            }

            for (int i = 0; i < aiControllers.Length; ++i)
            {
                ref var ai   = ref aiControllers[i];
                var     body = new BodyReference(ai.Controller.Car.Body, Simulation.Bodies);
                ref var pose = ref body.Pose;
        override internal void Update(double elapsedTime, GameTime gameTime)
        {
            BodyReference body = Body();

            UpdateOrientation(body);

            Brakement((float)elapsedTime, body);
            Movement((float)elapsedTime, body);
            Aligment((float)elapsedTime);
            Rotation((float)elapsedTime);

            emitter.Position = Position();
            emitter.Forward  = forward;
            emitter.Up       = upDirection;
            emitter.Velocity = body.Velocity.Linear.ToVector3();

            if (Input.Accelerate())
            {
                lastTurbo = gameTime.TotalGameTime.TotalMilliseconds;
            }
            if (gameTime.TotalGameTime.TotalMilliseconds > lastTurbo + turboRegenerationTime)
            {
                turbo = Math.Min(turbo + turboRegeneration * (float)elapsedTime, maxTurbo);
            }
        }
        private void UpdateOrientation(BodyReference body)
        {
            Quaternion rotation = body.Pose.Orientation.ToQuaternion();

            forward        = -PhysicUtils.Forward(rotation);
            rightDirection = PhysicUtils.Left(rotation);
            upDirection    = PhysicUtils.Up(rotation);
        }
Exemple #5
0
        public override void Initialize(ContentArchive content, Camera camera)
        {
            camera.Position = new Vector3(-30, 8, -60);
            camera.Yaw      = MathHelper.Pi * 3f / 4;
            camera.Pitch    = 0;

            Simulation = Simulation.Create(BufferPool, new DefaultNarrowPhaseCallbacks(), new DefaultPoseIntegratorCallbacks(BufferPool));
            var boxShape  = new Box(1, 1, 1);
            var baseShape = new Box(10, 1, 10);


            boxShape.ComputeInertia(1, out var boxInertia);

            var boxShapeIndex  = Simulation.Shapes.Add(boxShape);
            var baseShapeIndex = Simulation.Shapes.Add(baseShape);


            var boxDescription = new BodyDescription
            {
                //Make the uppermost block kinematic to hold up the rest of the chain.
                LocalInertia = boxInertia,
                Pose         = new RigidPose
                {
                    Position    = new Vector3(0, 2, 0),
                    Orientation = Quaternion.Identity
                },
                Activity = new BodyActivityDescription {
                    MinimumTimestepCountUnderThreshold = 32, SleepThreshold = .01f
                },
                Collidable = new CollidableDescription {
                    Shape = boxShapeIndex, SpeculativeMargin = .1f
                },
            };


            var baseDescription = new BodyDescription
            {
                //Make the uppermost block kinematic to hold up the rest of the chain.
                LocalInertia = new BodyInertia(),
                Pose         = new RigidPose
                {
                    Position    = new Vector3(0, 0, 0),
                    Orientation = Quaternion.Identity
                },
                Activity = new BodyActivityDescription {
                    MinimumTimestepCountUnderThreshold = 32, SleepThreshold = .01f
                },
                Collidable = new CollidableDescription {
                    Shape = baseShapeIndex, SpeculativeMargin = .1f
                },
            };

            var boxIndex  = Simulation.Bodies.Add(boxDescription);
            var baseIndex = Simulation.Bodies.Add(baseDescription);

            _boxReference  = new BodyReference(boxIndex, Simulation.Bodies);
            _baseReference = new BodyReference(baseIndex, Simulation.Bodies);
        }
        private void AddLinearVelocity(BodyReference body, Vector3 velocity)
        {
            var newVelocity = body.Velocity.Linear.ToVector3() + velocity;

            float   forwardSpeed    = Vector3.Dot(newVelocity, forward) / (forward.Length() * forward.Length());
            Vector3 forwardVelocity = Math.Clamp(forwardSpeed, minSpeed, maxSpeed) * forward;
            Vector3 limitedVelocity = forwardVelocity;

            body.Velocity.Linear = limitedVelocity.ToBEPU();
        }
        private float DistanceToXWing(BodyReference body)
        {
            Vector3 TIEPosition   = body.Pose.Position.ToVector3();
            Vector3 XWingPosition = World.xwing.Position();

            Vector3 DistanceVector = TIEPosition - XWingPosition;

            DistanceVector.Y = 0f;

            return(DistanceVector.Length());
        }
Exemple #8
0
        public CharacterControllerRef(CharacterControllers characters, Vector3 initialPosition, Capsule shape,
                                      float speculativeMargin, float mass, float maximumHorizontalForce, float maximumVerticalGlueForce,
                                      float jumpVelocity, float speed, float airControlForceScale, float airControlSpeedScale, float maximumSlope = MathF.PI * 0.25f)
        {
            this.characters = characters;
            var shapeIndex = characters.Simulation.Shapes.Add(shape);

            bodyHandle = characters.Simulation.Bodies.Add(BodyDescription.CreateDynamic(initialPosition, new BodyInertia {
                InverseMass = 1f / mass
            }, new CollidableDescription(shapeIndex, speculativeMargin), new BodyActivityDescription(shape.Radius * 0.02f)));
            Body = new BodyReference(bodyHandle, characters.Simulation.Bodies);
            ref var character = ref characters.AllocateCharacter(bodyHandle);
        private void Reiniciar()
        {
            TGCGame.content.S_Explotion.CreateInstance().Play();
            salud = 100;
            turbo = maxTurbo;
            BodyReference body = Body();

            /*body.Velocity.Linear = System.Numerics.Vector3.Zero;
             * body.Velocity.Angular = System.Numerics.Vector3.Zero;*/
            body.Pose.Position = System.Numerics.Vector3.Zero;
            //body.Pose.Orientation = Quaternion.Normalize(new Quaternion(baseUpDirection * baseRightDirection, 0)).ToBEPU();
        }
        private void Brakement(float elapsedTime, BodyReference body)
        {
            Vector3 velocity      = Velocity();
            float   brakmentForce = -acceleration / 10;

            Vector3 forwardBreakment   = (!Input.Accelerate() || turbo == 0) ? forward : Vector3.Zero;
            float   horizontalSpeed    = Vector3.Dot(velocity, rightDirection) / (rightDirection.Length() * rightDirection.Length());
            Vector3 horizontalBrakment = horizontalSpeed != 0 ? Vector3.Normalize(rightDirection * horizontalSpeed) : Vector3.Zero;
            float   verticalSpeed      = Vector3.Dot(velocity, upDirection) / (upDirection.Length() * upDirection.Length());
            Vector3 verticalBrakment   = verticalSpeed != 0 ? Vector3.Normalize(upDirection * verticalSpeed) : Vector3.Zero;

            AddLinearVelocity(body, (forwardBreakment + horizontalBrakment + verticalBrakment) * brakmentForce * elapsedTime);
        }
        protected override void OnInstantiate()
        {
            BodyReference body = Body();

            System.Numerics.Vector3 velocity = PhysicUtils.Forward(body.Pose.Orientation.ToQuaternion()).ToBEPU() * linearVelocity;
            body.Velocity.Linear = velocity;

            engineSound          = TGCGame.content.S_TIEEngine.CreateInstance();
            engineSound.IsLooped = true;
            engineSound.Volume   = 0.001f;
            emitter.Velocity     = velocity.ToVector3();
            TGCGame.soundManager.PlaySound(engineSound, emitter);
        }
        public virtual void Load(BodyHandle bodyHandle, ConnectionState connectionState, Simulator simulator, ObjectState state)
        {
            this.connectionState = connectionState;
            this.bodyHandle      = bodyHandle;
            this.state           = state;
            this.simulator       = simulator;
            this.reference       = GetReference();

            /* if(state.quaternion == new Quaternion(0,0,0,0)){
             *  this.state.quaternion = Quaternion.Identity;
             * }*/

            SendCreateMessage();
        }
 private void Movement(float elapsedTime, BodyReference body)
 {
     if (Input.Accelerate())
     {
         float speed = acceleration * elapsedTime;
         speed = Math.Min(speed, turbo);
         AddLinearVelocity(body, forward * speed);
         turbo -= speed;
     }
     else
     {
         AddLinearVelocity(body, Vector3.Zero);
     }
 }
        internal override void Update(double elapsedTime, GameTime gameTime)
        {
            BodyReference body = Body();

            if (body.Pose.Position.X > maxX)
            {
                body.Pose.Position.X = minX;
            }
            else if (body.Pose.Position.X < minX)
            {
                body.Pose.Position.X = maxX;
            }
            emitter.Position = body.Pose.Position.ToVector3();
        }
        internal void Fire(double gameTime)
        {
            if (gameTime < lastFire + fireCooldownTime)
            {
                return;
            }

            BodyReference body             = Body();
            Vector3       position         = body.Pose.Position.ToVector3();
            Quaternion    laserOrientation = PhysicUtils.DirectionsToQuaternion(TGCGame.camera.MouseDirection(position), upDirection);

            World.InstantiateLaser(position, -forward, laserOrientation, emitter, laserVolume);
            lastFire = gameTime;
        }
Exemple #16
0
        /// <summary>
        /// Will let you retrieve a reference to a known tracked body part.
        /// </summary>
        /// <param name="bodyRef"></param>
        /// <returns></returns>
        public Transform GetTransformForBone(BodyReference bodyRef)
        {
            switch (bodyRef)
            {
            case BodyReference.Head:
                return(viewCamera.transform);

            case BodyReference.LeftPalm:
                return(leftPalm);

            case BodyReference.LeftWrist:
                return(leftWrist);

            /*case BodyReference.LeftIndexTip:
             *      break;
             *
             * case BodyReference.LeftMiddleTip:
             *      break;
             *
             * case BodyReference.LeftRingTip:
             *      break;
             *
             * case BodyReference.LeftPinkyTip:
             *      break;*/

            case BodyReference.RightPalm:
                return(rightPalm);

            case BodyReference.RightWrist:
                return(rightWrist);

            /*case BodyReference.RightIndexTip:
             *      break;
             *
             * case BodyReference.RightMiddleTip:
             *      break;
             *
             * case BodyReference.RightRingTip:
             *      break;
             *
             * case BodyReference.RightPinkyTip:
             *      break;*/

            default:
                break;
            }

            return(null);
        }
Exemple #17
0
        public override void Update(Window window, Camera camera, Input input, float dt)
        {
            var bigBall = new BodyReference(bigBallHandle, Simulation.Bodies);

            timeAccumulator += 1 / 60f;
            if (timeAccumulator > MathF.PI * 128)
            {
                timeAccumulator -= MathF.PI * 128;
            }
            if (!bigBall.Awake)
            {
                Simulation.Awakener.AwakenBody(bigBallHandle);
            }
            bigBall.Velocity.Linear = new Vector3(0, 3f * MathF.Sin(timeAccumulator * 5), 0);
            base.Update(window, camera, input, dt);
        }
Exemple #18
0
        public void SetCollider(PhysicsType type)
        {
            if (PhysicsBodyInitialized)
            {
                RemoveCollider();

                if (type == PhysicsType.None)
                {
                    PhysicsType = type;
                    if (Physics.PhysicsObjects.Contains(this))
                    {
                        Physics.PhysicsObjects.Remove(this);
                    }

                    return;
                }
            }

            if (type == PhysicsType.Cube)
            {
                Box shape = new Box(2.0f, 2.0f, 2.0f);
                ShapeIndex = Physics.simulator.Shapes.Add(shape);
                Shape      = shape;
            }
            else if (type == PhysicsType.Sphere)
            {
                Sphere shape = new Sphere(2.0f);
                ShapeIndex = Physics.simulator.Shapes.Add(shape);
                Shape      = shape;
            }

            Shape.ComputeInertia(Mass, out BodyInertia inertia);

            PhysicsDescription = BodyDescription.CreateDynamic(transform.Position, inertia, new CollidableDescription(ShapeIndex, 0.1f), new BodyActivityDescription(0.01f));
            BodyHandle         = Physics.simulator.Bodies.Add(PhysicsDescription);
            BodyReference      = Physics.simulator.Bodies.GetBodyReference(BodyHandle);

            if (!Physics.PhysicsObjects.Contains(this))
            {
                Physics.PhysicsObjects.Add(this);
            }

            PhysicsType            = type;
            PhysicsBodyInitialized = true;
            physicsEnabled         = true;
        }
        private void Flee(BodyReference body, GameTime gameTime)
        {
            Quaternion DegreeRotation = new Quaternion(new Vector3(0, 1, 0), 1f);

            DegreeRotation.Normalize();
            Quaternion ActualRotation = body.Pose.Orientation.ToQuaternion();

            ActualRotation.Normalize();

            Quaternion FinalRotation = Quaternion.Slerp(DegreeRotation, ActualRotation, (float)gameTime.ElapsedGameTime.TotalSeconds);

            body.Pose.Orientation = FinalRotation.ToBEPU();

            Quaternion rotation = body.Pose.Orientation.ToQuaternion();
            Vector3    forward  = PhysicUtils.Forward(rotation);

            body.Velocity.Linear = (forward * FastVelocity).ToBEPU();
        }
        private void FaceXWing(BodyReference body, GameTime gameTime)
        {
            Vector3    XWingDirection  = World.xwing.Position() - body.Pose.Position.ToVector3();
            Quaternion RotationToXWing = new Quaternion(XWingDirection, 1f);

            RotationToXWing.Normalize();
            Quaternion ActualRotation = body.Pose.Orientation.ToQuaternion();

            ActualRotation.Normalize();
            Quaternion FinalRotation = Quaternion.Slerp(RotationToXWing, ActualRotation, 8 * (float)gameTime.ElapsedGameTime.TotalSeconds);

            body.Pose.Orientation = FinalRotation.ToBEPU();

            Quaternion rotation = body.Pose.Orientation.ToQuaternion();
            Vector3    forward  = PhysicUtils.Forward(rotation);

            body.Velocity.Linear = (forward * StandarVelocity).ToBEPU();
        }
        protected void Fire(BodyReference body, GameTime gameTime)
        {
            double totalTime = gameTime.TotalGameTime.TotalMilliseconds;

            if (totalTime < LastFire + FireCooldownTime)
            {
                return;
            }

            Vector3    position         = body.Pose.Position.ToVector3();
            Quaternion rotation         = body.Pose.Orientation.ToQuaternion();
            Vector3    forward          = PhysicUtils.Forward(rotation);
            Quaternion laserOrientation = PhysicUtils.DirectionsToQuaternion(forward, Vector3.Up);

            World.InstantiateLaser(position, -forward, laserOrientation, Emitter, LaserVolume);
            LastFire = totalTime;
            FireCounter++;
        }
        internal void SecondaryFire(double gameTime, Vector2 mousePosition)
        {
            if (gameTime < lastFire + fireCooldownTime)
            {
                return;
            }

            BodyReference body = Body();

            Vector3    position         = body.Pose.Position.ToVector3();
            Quaternion laserOrientation = PhysicUtils.DirectionsToQuaternion(TGCGame.camera.MouseDirection(position), upDirection);
            Vector3    up   = PhysicUtils.Up(laserOrientation);
            Vector3    left = PhysicUtils.Left(laserOrientation);

            World.InstantiateLaser(position + up * 1.0f + left * 4.75459f, -forward, laserOrientation, emitter, laserVolume / 4);
            World.InstantiateLaser(position + up * 1.0f - left * 4.75459f, -forward, laserOrientation, emitter, laserVolume / 4);
            World.InstantiateLaser(position - up * 1.7f + left * 4.75459f, -forward, laserOrientation, emitter, laserVolume / 4);
            World.InstantiateLaser(position - up * 1.7f - left * 4.75459f, -forward, laserOrientation, emitter, laserVolume / 4);

            lastFire = gameTime;
        }
Exemple #23
0
        private void CreateFloors()
        {
            var floorShape      = new Box(200, 1, 10);
            var floorShapeIndex = Simulation.Shapes.Add(floorShape);

            var position = new Vector3(0, -0.5f, -NumberOfFloors * floorShape.HalfLength);

            _floorReferences = new List <BodyReference>(NumberOfFloors);
            var offset = new Vector3(0, 0, floorShape.Length);

            for (var i = 0; i < NumberOfFloors; i++)
            {
                var floorIndex = CreateFloor(position, floorShapeIndex);
                position += offset;
                var     floorReference      = new BodyReference(floorIndex, Simulation.Bodies);
                ref var floorBodyProperties = ref _bodyProperties.Allocate(floorIndex);

                var floorCollisionGroup = new CollisionGroup(0b1);
                floorBodyProperties = new BodyProperty {
                    Filter = floorCollisionGroup, Friction = 1f
                };
                _floorReferences.Add(floorReference);
            }
        public override void Update(Window window, Camera camera, Input input, float dt)
        {
            base.Update(window, camera, input, dt);
            #region  KeyInput

            //Delete all boxes;
            if (input.WasPushed(Key.Z))
            {
                for (var i = _createdObjects.Count - 1; i > 0; --i)
                {
                    var bodiIndex = _createdObjects[i];
                    var reference = new BodyReference(bodiIndex, Simulation.Bodies);
                    if (reference.LocalInertia.InverseMass > 0)
                    {
                        Simulation.Bodies.Remove(bodiIndex);
                        _createdObjects.RemoveAt(i);
                    }
                }
            }

            if (input.WasPushed(Key.X))
            {
                for (var j = 0; j < _numberOfLevels; j++)
                {
                    var startPosition = new Vector3(0, _floorHeight * j, _verticalHalfLenght * j);
                    for (int i = 0; i < _numberOfLoops * 2; i++)
                    {
                        CreateBoxes(startPosition);
                        startPosition.Z += _verticalHalfLenght * 2;
                    }
                }
            }
            var random = new Random(Guid.NewGuid().GetHashCode());


            //Randomly destroy
            if (input.WasPushed(Key.C))
            {
                for (var i = _createdObjects.Count - 1; i > 0; --i)
                {
                    var bodiIndex = _createdObjects[i];
                    var reference = new BodyReference(bodiIndex, Simulation.Bodies);
                    if ((reference.LocalInertia.InverseMass > 0) && random.Next(100) > 50)
                    {
                        Simulation.Bodies.Remove(bodiIndex);
                        _createdObjects.RemoveAt(i);
                    }
                }
            }

            _spawnCount += dt;
            if (_spawnCount >= _spawnTime)
            {
                _spawnCount -= _spawnTime;
                var boxPosition = new Vector3(-_horizontalHalfLenght + 1, 2, 0);
                var identity    = Quaternion.Identity;
                Instantiate(_boxDescription, ref boxPosition, ref identity);



                var deleteObject = random.Next(_createdObjects.Count - 1);
                var reference    = new BodyReference(deleteObject, Simulation.Bodies);
                if ((reference.LocalInertia.InverseMass > 0))
                {
                    Simulation.Bodies.Remove(deleteObject);
                    _createdObjects.Remove(deleteObject);
                }
            }


            for (var i = 0; i < _movingObjects.Count; i++)
            {
                ref var movingObject = ref _movingObjects[i];


                if (movingObject.IsMoving)
                {
                    var bodyReference  = new BodyReference(movingObject.BodyIndex, Simulation.Bodies);
                    var targetPosition = movingObject.StartPosition;
                    if (movingObject.IsMovingToOffset)
                    {
                        targetPosition += MovingOffset;
                    }

                    var linearError = targetPosition - bodyReference.Pose.Position;

                    if (linearError.LengthSquared() < .0001f)
                    {
                        bodyReference.Velocity.Linear = Vector3.Zero;
                        movingObject.IsMoving         = false;
                        movingObject.IsMovingToOffset = !movingObject.IsMovingToOffset;
                    }
                    else
                    {
                        var linearVelocity = linearError / dt;
                        bodyReference.Velocity.Linear = linearVelocity;
                        Simulation.Awakener.AwakenBody(bodyReference.Handle);
                    }
                }
                else
                {
                    movingObject.ToggleSleepTimer += dt;
                    if (movingObject.ToggleSleepTimer > SleepTime)
                    {
                        movingObject.ToggleSleepTimer = SleepTime * (float)random.NextDouble();
                        movingObject.IsMoving         = true;
                    }
                }
            }
Exemple #25
0
 public void Update(Simulation simulation, double time)
 {
     var     body       = new BodyReference(BodyHandle, simulation.Bodies);
     ref var pose       = ref body.Pose;
 private void ShootXWing(BodyReference body, GameTime gameTime)
 {
     FaceXWing(body, gameTime);
     Fire(body, gameTime);
 }
Exemple #27
0
 public object GetDynamicContext(BodyReference reference) => GetDynamicContext(reference.Handle);
Exemple #28
0
 public void RemoveDynamic(BodyReference body)
 {
     Simulation.Bodies.Remove(body.Handle);
     _dynamicContexts.Remove(body.Handle);
 }
        protected internal void StateMachine(GameTime gameTime)
        {
            BodyReference body = Body();

            if (Health <= 0)
            {
                TGCGame.content.S_Explotion.CreateInstance().Play();
                Destroy();
            }

            if (TooClose(body))
            {
                CurrentState = State.FLEEING;
                ShortFlee    = true;
                Flee(body, gameTime);
                FireCounter = 0;
            }

            if (CurrentState == State.SEEKING)
            {
                if (XWingInSight(body))
                {
                    GetCloseToXWing(body, gameTime);

                    if (CloseToXWing(body))
                    {
                        CurrentState = State.ATTACKING;
                        ShootXWing(body, gameTime);
                    }

                    if (Health < 40)
                    {
                        CurrentState = State.FLEEING;
                        Flee(body, gameTime);
                        FireCounter = 0;
                    }
                }
                else
                {
                    CurrentState = State.SEEKING;
                    GetCloseToXWing(body, gameTime);
                }
            }

            else if (CurrentState == State.ATTACKING)
            {
                if (FireCounter >= 15)
                {
                    CurrentState = State.FLEEING;
                    ShortFlee    = true;
                    Flee(body, gameTime);
                    FireCounter = 0;
                }

                if (CloseToXWing(body))
                {
                    ShootXWing(body, gameTime);
                }
                else
                {
                    if (Health < 40)
                    {
                        CurrentState = State.FLEEING;
                        Flee(body, gameTime);
                        FireCounter = 0;
                    }
                    else
                    {
                        CurrentState = State.FLEEING;
                        ShortFlee    = true;
                        Flee(body, gameTime);
                        FireCounter = 0;
                    }
                }
            }

            else if (CurrentState == State.FLEEING)
            {
                Flee(body, gameTime);

                if (ShortFlee)
                {
                    if (ShortFleeSuccess(body))
                    {
                        CurrentState = State.SEEKING;
                        ShortFlee    = false;
                    }
                }

                else
                {
                    if (FleeSuccess(body))
                    {
                        CurrentState = State.SEEKING;
                    }
                }
            }
        }
Exemple #30
0
 public Vehicle(PhyObject obj)
 {
     this.obj  = obj;
     reference = obj.GetReference();
 }