Пример #1
0
        //Uses a data reader to read out a quaternion value from the database
        public static Quaternion ReadQuaternionValue(string CommandQuery, string Context = "Unknown error using reader to get quaternion value")
        {
            //Create a variable we will fill with the quaternion values when they are read from the databse
            Quaternion QuaternionValue = Quaternion.Identity;

            //Open a new connection with the base, then a datareader to get information from it
            MySqlConnection Connection = OpenConnection();
            MySqlCommand    Command    = CreateCommand(CommandQuery, Connection, Context);
            MySqlDataReader Reader     = CreateReader(Command);

            //Try using the reader to get the quaternion values that were looking for
            try
            {
                QuaternionValue.X = Convert.ToSingle(Reader["XRotation"]);
                QuaternionValue.Y = Convert.ToSingle(Reader["YRotation"]);
                QuaternionValue.Z = Convert.ToSingle(Reader["ZRotation"]);
                QuaternionValue.W = Convert.ToSingle(Reader["WRotation"]);
            }
            catch (MySqlException Exception) { MessageLog.Error(Exception, Context); }

            //Close the data reader, database connection then return the final quaternion values
            Reader.Close();
            Connection.Close();
            return(QuaternionValue);
        }
Пример #2
0
 //Writes the 4 floating point values of a Quaternion onto the end of the current PacketData
 public void WriteQuaternion(Quaternion QuaternionValue)
 {
     WriteFloat(QuaternionValue.X);
     WriteFloat(QuaternionValue.Y);
     WriteFloat(QuaternionValue.Z);
     WriteFloat(QuaternionValue.W);
 }
Пример #3
0
 public void Write(Quat value)
 {
     Write(value.X);
     Write(value.Y);
     Write(value.Z);
     Write(value.W);
 }
Пример #4
0
 public static void PackOrientation(ref Quaternion source, out Vector3 packed)
 {
     packed = new Vector3(source.X, source.Y, source.Z);
     if (source.W < 0)
     {
         packed = -packed;
     }
 }
Пример #5
0
        public float CameraYRotation = 0f; //Character cameras current Y Rotation value

        //Moves character to spawn with default camera settings
        public void SetDefaultValues()
        {
            CurrentHealth   = MaxHealth;
            Position        = new Vector3(15.068f, 0.079f, 22.025f);
            Rotation        = new Quaternion(0f, 0.125f, 0f, -0.992f);
            CameraZoom      = 7f;
            CameraXRotation = -14.28f;
            CameraYRotation = 5.449f;
        }
Пример #6
0
        //Tries using the command arguments for performing a setallcharactersrotations
        private void TrySetAllCharactersRotations(string[] Input)
        {
            //Seperate the command arguments
            Quaternion Rotation = new Quaternion(float.Parse(Input[1]), float.Parse(Input[2]), float.Parse(Input[3]), float.Parse(Input[4]));

            //Log what is happening here
            MessageLog.Print("Setting the rotation of all characters in the database to " + Rotation.ToString());

            //Apply the new rotation to all characters in the database
            CharactersDatabase.SetAllRotations(Rotation);
        }
Пример #7
0
        //Sets the quaternion rotation value of all characters in the database
        public static void SetAllRotations(Quaternion Rotation)
        {
            //Define the SQL query for applying the new rotation values to the database
            string RotationQuery = "UPDATE characters SET " +
                                   "XRotation='" + Rotation.X + "', " +
                                   "YRotation='" + Rotation.Y + "', " +
                                   "ZRotation='" + Rotation.Z + "', " +
                                   "WRotation='" + Rotation.W + "'";

            //Pass the query on to be executed in a new SQL command
            CommandManager.ExecuteNonQuery(RotationQuery, "Setting the rotation of all characters in the database to " + Rotation.ToString() + ".");
        }
Пример #8
0
        //Reads 4 floating point values from the front of the RemainingPacketData, then removes them from that string, returning them in a Quaternion format
        public Quaternion ReadQuaternion()
        {
            //Create a new Quaternion object to store all the values within
            Quaternion QuaternionValue = new Quaternion();

            //Read the next 4 floating point values from the RemainingPacketData and store them in the Quaternion object as the X,Y,Z and W values
            QuaternionValue.X = ReadFloat();
            QuaternionValue.Y = ReadFloat();
            QuaternionValue.Z = ReadFloat();
            QuaternionValue.W = ReadFloat();
            //Return the final quaternion value that was requested
            return(QuaternionValue);
        }
Пример #9
0
        public static void HandlePlayerRotationUpdate(int ClientID, ref NetworkPacket Packet)
        {
            Quaternion       Rotation = Packet.ReadQuaternion();
            ClientConnection Client   = ConnectionManager.GetClient(ClientID);

            if (Client != null)
            {
                Client.Character.Rotation    = Rotation;
                Client.Character.NewRotation = true;
                foreach (ClientConnection OtherClient in ClientSubsetFinder.GetInGameClientsExceptFor(ClientID))
                {
                    PlayerManagementPacketSender.SendPlayerRotationUpdate(OtherClient.ClientID, Client.Character);
                }
            }
        }
Пример #10
0
        public static void BuildBasis(ref RigidPose a, ref RigidPose b, out Vector3 offsetB, out Vector3 x, out Vector3 y, out Vector3 z)
        {
            offsetB = b.Position - a.Position;
            y       = Vector3.Normalize(-offsetB);
            Quaternion.TransformUnitZ(a.Orientation, out var ax);
            x = Vector3.Cross(ax, y);
            var xLength = x.Length();

            if (xLength > 1e-7)
            {
                x /= xLength;
            }
            else
            {
                Quaternion.TransformUnitX(a.Orientation, out var az);
                x = Vector3.Normalize(Vector3.Cross(az, y));
            }
            z = Vector3.Cross(x, y);
        }
Пример #11
0
        ////Instructs the inner AI of any enemies which are currently targetting the given clients game character to drop their current target and return to their default start position and AI behaviour state
        //public static void DropTarget(ClientConnection PlayerTarget)
        //{
        //    //Loop through all the active entities trying to find any that currently have this player targetted
        //    foreach(var Entity in ActiveEntities)
        //    {
        //        //Cast it to the enemy entity type
        //        EnemyEntity Enemy = (EnemyEntity)Entity;

        //        //Tell them to drop target if they have this player targetted
        //        if (Enemy.PlayerTarget == PlayerTarget)
        //            Enemy.DropTarget();
        //    }
        //}

        ////Handles having a player disconnect from the game, has all enemies drop them as their target, and backs up the characters data to the database
        //public static void HandleClientDisconnect(ClientConnection Client)
        //{
        //    if (Client.BodyHandle != -1)
        //    {
        //        //Remove them from the physics scene
        //        Program.World.WorldSimulation.Bodies.Remove(Client.BodyHandle);
        //        Client.BodyHandle = -1;

        //        //Tell any enemies targetting this character to stop targetting them
        //        EntityManager.DropTarget(Client);

        //        //Backup the characters data
        //        CharactersDatabase.SaveCharacterLocation(Client.CharacterName, Maths.VectorTranslate.ConvertVector(Client.CharacterPosition));
        //    }
        //}

        //Checks if the attack hit any enemies, damages them accordingly
        public static void HandlePlayerAttack(Vector3 AttackPosition, Vector3 AttackScale, Quaternion AttackRotation)
        {
            ////Create a box shape and transform to apply to it for the players attack collider
            //BoxShape AttackCollider = new BoxShape(AttackScale.X, AttackScale.Y, AttackScale.Z);
            //RigidTransform AttackTransform = new RigidTransform(AttackPosition);

            ////While applying the attack to all the enemies in the scene, keep a list of enemies that were killed by the attack so they can
            ////be removed from the game once the collection of enemies has been iterated through completely
            //List<BaseEntity> DeadEntities = new List<BaseEntity>();

            ////Test this attack against every enemy in the scene
            //foreach (BaseEntity Enemy in ActiveEntities)
            //{
            //    //Create a collider for each enemy
            //    BoxShape EnemyCollider = new BoxShape(1, 1, 1);
            //    RigidTransform EnemyTransform = new RigidTransform(Enemy.Entity.Position);

            //    //Check if the attack hit this enemy
            //    bool AttackHit = BoxBoxCollider.AreBoxesColliding(AttackCollider, EnemyCollider, ref AttackTransform, ref EnemyTransform);
            //    if (AttackHit)
            //    {
            //        //Apply damage to the enemy and update all players on its new status
            //        Enemy.HealthPoints -= 1;

            //        //If the enemy has run out of health they need to be removed from the game, and all clients need to be told to remove them
            //        if (Enemy.HealthPoints <= 0)
            //            //Add the enemy to the list of enemies which were killed by this attack so they can be dealt with once the attack has been applied to all enemies
            //            DeadEntities.Add(Enemy);
            //    }
            //}

            ////If any entities were killed by this players attack they need to be removed from the game world
            //if (DeadEntities.Count > 0)
            //{
            //    //Get a list of all the active players who will need to be told about the enemies that were just killed
            //    List<ClientConnection> ActivePlayers = ConnectionManager.GetActiveClients();
            //    //Tell all of these active players which entities where killed by the players attack
            //    PacketManager.SendListRemoveEntities(ActivePlayers, DeadEntities);
            //    //Now remove all these entities from the servers world simulation
            //    RemoveEntities(DeadEntities);
            //}
        }
Пример #12
0
        //Backs up a characters world rotation value into the database
        private static void SetCharacterRotation(string CharacterName, Quaternion CharacterRotation)
        {
            string UpdateQuery = "UPDATE characters SET XRotation='" + CharacterRotation.X + "', YRotation='" + CharacterRotation.Y + "', ZRotation='" + CharacterRotation.Z + "', WRotation='" + CharacterRotation.W + "' WHERE CharacterName='" + CharacterName + "'";

            CommandManager.ExecuteNonQuery(UpdateQuery, "Setting " + CharacterName + "s rotation in the database to " + CharacterRotation.ToString());
        }
Пример #13
0
 //Adds a mesh into the physics scene as a static object, and applied a specific rotation to it
 public static int AddStatic(Simulation World, int MeshHandle, Vector3 Position, Quaternion Rotation)
 {
     return(World.Statics.Add(new StaticDescription(Position, Rotation, new CollidableDescription(World.Shapes.Add(GetMesh(MeshHandle)), 0.1f))));
 }
Пример #14
0
        public override void Initialize(ContentArchive content, Camera camera)
        {
            camera.Position = new Vector3(0, 5, 10);
            camera.Yaw      = 0;
            camera.Pitch    = 0;

            bodyProperties = new BodyProperty <TankDemoBodyProperties>();
            Simulation     = Simulation.Create(BufferPool, new TankCallbacks()
            {
                Properties = bodyProperties
            }, new DemoPoseIntegratorCallbacks(new Vector3(0, -10, 0)));

            var builder = new CompoundBuilder(BufferPool, Simulation.Shapes, 2);

            builder.Add(new Box(1.85f, 0.7f, 4.73f), RigidPose.Identity, 10);
            builder.Add(new Box(1.85f, 0.6f, 2.5f), new RigidPose(new Vector3(0, 0.65f, -0.35f)), 0.5f);
            builder.BuildDynamicCompound(out var children, out var bodyInertia, out _);
            builder.Dispose();
            var bodyShape      = new Compound(children);
            var bodyShapeIndex = Simulation.Shapes.Add(bodyShape);
            var wheelShape     = new Cylinder(0.4f, .18f);

            wheelShape.ComputeInertia(0.25f, out var wheelInertia);
            var wheelShapeIndex = Simulation.Shapes.Add(wheelShape);

            var projectileShape = new Sphere(0.1f);

            projectileShape.ComputeInertia(0.2f, out var projectileInertia);
            var tankDescription = new TankDescription
            {
                Body         = TankPartDescription.Create(10, new Box(4f, 1, 5), RigidPose.Identity, 0.5f, Simulation.Shapes),
                Turret       = TankPartDescription.Create(1, new Box(1.5f, 0.7f, 2f), new RigidPose(new Vector3(0, 0.85f, 0.4f)), 0.5f, Simulation.Shapes),
                Barrel       = TankPartDescription.Create(0.5f, new Box(0.2f, 0.2f, 3f), new RigidPose(new Vector3(0, 0.85f, 0.4f - 1f - 1.5f)), 0.5f, Simulation.Shapes),
                TurretAnchor = new Vector3(0f, 0.5f, 0.4f),
                BarrelAnchor = new Vector3(0, 0.5f + 0.35f, 0.4f - 1f),
                TurretBasis  = Quaternion.Identity,
                TurretServo  = new ServoSettings(1f, 0f, 40f),
                TurretSpring = new SpringSettings(10f, 1f),
                BarrelServo  = new ServoSettings(1f, 0f, 40f),
                BarrelSpring = new SpringSettings(10f, 1f),

                ProjectileShape            = Simulation.Shapes.Add(projectileShape),
                ProjectileSpeed            = 100f,
                BarrelLocalProjectileSpawn = new Vector3(0, 0, -1.5f),
                ProjectileInertia          = projectileInertia,

                LeftTreadOffset    = new Vector3(-1.9f, 0f, 0),
                RightTreadOffset   = new Vector3(1.9f, 0f, 0),
                SuspensionLength   = 1f,
                SuspensionSettings = new SpringSettings(2.5f, 1.5f),
                WheelShape         = wheelShapeIndex,
                WheelInertia       = wheelInertia,
                WheelFriction      = 2f,
                TreadSpacing       = 1f,
                WheelCountPerTread = 5,
                WheelOrientation   = Quaternion.CreateFromAxisAngle(Vector3.UnitZ, MathF.PI * -0.5f),
            };

            playerController = new TankController(Tank.Create(Simulation, bodyProperties, BufferPool, new RigidPose(new Vector3(0, 10, 0), Quaternion.Identity), tankDescription), 20, 5, 2, 1, 3.5f);


            const int   planeWidth          = 257;
            const float terrainScale        = 3;
            const float inverseTerrainScale = 1f / terrainScale;
            var         terrainPosition     = new Vector2(1 - planeWidth, 1 - planeWidth) * terrainScale * 0.5f;

            random = new Random(5);

            //Add some building-ish landmarks.
            var landmarkMin  = new Vector3(planeWidth * terrainScale * -0.45f, 0, planeWidth * terrainScale * -0.45f);
            var landmarkMax  = new Vector3(planeWidth * terrainScale * 0.45f, 0, planeWidth * terrainScale * 0.45f);
            var landmarkSpan = landmarkMax - landmarkMin;

            for (int j = 0; j < 25; ++j)
            {
                var buildingShape = new Box(10 + (float)random.NextDouble() * 10, 20 + (float)random.NextDouble() * 20, 10 + (float)random.NextDouble() * 10);
                var position      = landmarkMin + landmarkSpan * new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                Simulation.Statics.Add(new StaticDescription(
                                           new Vector3(0, buildingShape.HalfHeight - 4f + GetHeightForPosition(position.X, position.Z, planeWidth, inverseTerrainScale, terrainPosition), 0) + position,
                                           Quaternion.CreateFromAxisAngle(Vector3.UnitY, (float)random.NextDouble() * MathF.PI),
                                           new CollidableDescription(Simulation.Shapes.Add(buildingShape), 0.1f)));
            }

            DemoMeshHelper.CreateDeformedPlane(planeWidth, planeWidth,
                                               (int vX, int vY) =>
            {
                var position2D = new Vector2(vX, vY) * terrainScale + terrainPosition;
                return(new Vector3(position2D.X, GetHeightForPosition(position2D.X, position2D.Y, planeWidth, inverseTerrainScale, terrainPosition), position2D.Y));
            }, new Vector3(1, 1, 1), BufferPool, out var planeMesh);
            Simulation.Statics.Add(new StaticDescription(new Vector3(0, 0, 0),
                                                         new CollidableDescription(Simulation.Shapes.Add(planeMesh), 0.1f)));

            explosions = new QuickList <Explosion>(32, BufferPool);

            //Create the AI tanks.
            const int aiTankCount = 100;

            aiTanks     = new QuickList <AITank>(aiTankCount, BufferPool);
            playAreaMin = new Vector2(landmarkMin.X, landmarkMin.Z);
            playAreaMax = new Vector2(landmarkMax.X, landmarkMax.Z);
            var playAreaSpan = playAreaMax - playAreaMin;

            for (int i = 0; i < aiTankCount; ++i)
            {
                var horizontalPosition = playAreaMin + new Vector2((float)random.NextDouble(), (float)random.NextDouble()) * playAreaSpan;
                aiTanks.AllocateUnsafely() = new AITank
                {
                    Controller = new TankController(
                        Tank.Create(Simulation, bodyProperties, BufferPool, new RigidPose(
                                        new Vector3(horizontalPosition.X, 10, horizontalPosition.Y),
                                        Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), (float)random.NextDouble() * 0.1f)),
                                    tankDescription), 20, 5, 2, 1, 3.5f),
                    HitPoints = 5
                };
            }
        }
Пример #15
0
 public static Quaternion ToStandard(this BepuUtilities.Quaternion q)
 {
     return(new Quaternion(q.X, q.Y, q.Z, q.W));
 }