public static int[] AddObject(GameObject gameObject, Texture2D texture, float minMass, float maxMass, float minStiffness, float maxStiffness, float minDamping, float maxDamping)
    {
        List <int> indexes = new List <int>();
        var        filters = gameObject.GetComponentsInChildren <MeshFilter>();

        if (filters != null)
        {
            foreach (var filter in filters)
            {
                filter.mesh.MarkDynamic();
                ObjectData meshData = new ObjectData();
                meshData.mesh      = filter.mesh;
                meshData.vertices  = meshData.mesh.vertices;
                meshData.uv        = meshData.mesh.uv;
                meshData.pixels    = texture.GetPixels();
                meshData.transform = filter.gameObject.transform;
                meshData.position  = filter.gameObject.transform.position;
                meshData.rotation  = filter.gameObject.transform.rotation;
                meshData.scale     = filter.gameObject.transform.lossyScale;

                int index = PhysicsSimulation.AddObjectToSimulation(ref meshData.position, ref meshData.rotation, ref meshData.scale, meshData.vertices, meshData.uv, meshData.mesh.vertexCount, meshData.pixels, texture.height, texture.width, minMass, maxMass, minStiffness, maxStiffness, minDamping, maxDamping);
                meshData.index = index;
                datas.Add(meshData);
                indexes.Add(index);
            }
        }
        return(indexes.ToArray());
    }
Example #2
0
        public void CapsuleSphereCollision()
        {
            // r=1.5 at (0,8,0)
            var sphere = SphereBody.Create(_simulation,
                                           new Vector3(0, 8, 0),
                                           1.5f);

            // pointing straight up, should hit sphere
            var capsule1 = CapsuleBody.Create(this._simulation,
                                              new Vector3(0, 0, 0),
                                              Quaternion.Identity,
                                              1f,
                                              6f);

            // rotated 45deg, should miss sphere
            var capsule2 = CapsuleBody.Create(this._simulation,
                                              new Vector3(0, 0, 0),
                                              Quaternion.CreateFromAxisAngle(Vector3.UnitX, (float)(0.125 * Math.Tau)),
                                              1f,
                                              6f);

            // floating, rotated 90deg, should hit sphere
            var capsule3 = CapsuleBody.Create(this._simulation,
                                              new Vector3(8, 8, 0),
                                              Quaternion.CreateFromAxisAngle(Vector3.UnitZ, (float)(0.25 * Math.Tau)),
                                              1f,
                                              6f);

            Assert.IsTrue(PhysicsSimulation.CapsuleSphereCollision(capsule1, sphere));
            Assert.IsFalse(PhysicsSimulation.CapsuleSphereCollision(capsule2, sphere));
            Assert.IsTrue(PhysicsSimulation.CapsuleSphereCollision(capsule3, sphere));
        }
 public static void ChangeObjectsPhysicsVariableLimits(float minMass, float maxMass, float minStiffness, float maxStiffness, float minDamping, float maxDamping, params int[] index)
 {
     for (int i = 0; i < index.Length; i++)
     {
         PhysicsSimulation.ChangeObjectsPhysicsVariableLimits(index[i], minMass, maxMass, minStiffness, maxStiffness, minDamping, maxDamping);
     }
 }
 public static void RemoveObjectsFromSimulation(params int[] index)
 {
     for (int i = 0; i < index.Length; i++)
     {
         PhysicsSimulation.RemoveObjectFromSimulation(index[i]);
         datas.RemoveAll(data => data.index == index[i]);
     }
 }
Example #5
0
        public bool Contains(PointF point)
        {
            Body body   = GenerateBody(PhysicsSimulation.World, false);
            bool result = PhysicsSimulation.CheckPoint(body, point);

            PhysicsSimulation.RemoveBody(body);

            return(result);
        }
    void OnValidate()
    {
        if (minMass < 0)
        {
            minMass = 0;
        }
        if (maxMass < 0)
        {
            maxMass = minMass;
        }
        if (minStiffness < 0)
        {
            minStiffness = 0;
        }
        if (maxStiffness < 0)
        {
            maxStiffness = minStiffness;
        }
        if (minDamping < 0)
        {
            minDamping = 0;
        }
        if (maxDamping < 0)
        {
            maxDamping = minDamping;
        }

        if (minMass > maxMass)
        {
            minMass = maxMass;
        }
        if (minStiffness > maxStiffness)
        {
            minStiffness = maxStiffness;
        }
        if (minDamping > maxDamping)
        {
            minDamping = maxDamping;
        }

        if (!Application.isPlaying)
        {
            return;
        }
        if (!initialValidationDone)
        {
            initialValidationDone = true;
            return;
        }

        if (physicsTexture != null)
        {
            PhysicsSimulation.ChangePhysicsTexture(physicsTexture, indexes);
        }
        PhysicsSimulation.ChangeObjectsPhysicsVariableLimits(minMass, maxMass, minStiffness, maxStiffness, minDamping, maxDamping, indexes);
    }
Example #7
0
        static void RunPhysicsOnSelected()
        {
            if (Selection.activeGameObject != null)
            {
                PhysicsSimulation.RunSimulation(Selection.gameObjects);
            }

            Selection.activeGameObject = null;
            Selection.activeObject     = null;
        }
Example #8
0
 public static void PhysicsUpdate()
 {
     while (!GameExiting && PhysicsOnBackgroundThread)
     {
         DateTime now     = DateTime.Now;
         double   elapsed = (now - physicsLastUpdate).TotalMilliseconds;
         PhysicsSimulation.Update((float)elapsed / 1000);
         physicsLastUpdate = now;
         Thread.Sleep(5);
     }
 }
Example #9
0
        static void Main(string[] args)
        {
            int THREADS = 2;
            int OBJECTS = 2;

            if (args.Length != 0)
            {
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i] == "--threads")
                    {
                        i++;
                        if (int.Parse(args[i]) > THREADS)
                        {
                            THREADS = int.Parse(args[i]);
                        }
                    }

                    if (args[i] == "--objects")
                    {
                        i++;
                        OBJECTS = int.Parse(args[i]);
                    }
                }
            }

            Console.WriteLine("Using {0} threads", THREADS);
            Console.WriteLine("Creating {0} objects", OBJECTS);
            Thread.Sleep(1000);

            Random rand = new Random();

            System.Collections.ArrayList objs      = new System.Collections.ArrayList();
            PhysicsSimulation            m_Physics = new PhysicsSimulation(THREADS);

            for (int i = 0; i < OBJECTS; i++)
            {
                Spheroid m_Spheroid = new Spheroid(
                    new Vector3(rand.Next(-100, 100), rand.Next(-100, 100), rand.Next(-100, 100)),
                    new Vector3(1, 1, 1),
                    rand.Next(1, 9));
                objs.Add(m_Spheroid);
            }
            Console.SetCursorPosition(0, Console.CursorTop);
            Console.Write(">");

            ControlPlane control = new ControlPlane(objs, THREADS);

            control.Start();
        }
    public static void ChangePhysicsTexture(Texture2D newTexture, params int[] index)
    {
        Color[] colors = newTexture.GetPixels();

        int textureHeight = newTexture.height;
        int textureWidth  = newTexture.width;

        for (int i = 0; i < index.Length; i++)
        {
            ObjectData oD = datas.Find(data => data.index == index[i]);
            oD.pixels = colors;
            PhysicsSimulation.ChangeObjectsPhysicsTexture(index[i], colors, textureHeight, textureWidth);
        }
    }
Example #11
0
        public void BoxBoxCollision()
        {
            // This test does not check cases where the boxes collide but none of
            // the vertices are contained by the other box (edge-edge collisions).

            // 10x10x10 at (0,0,0)
            var boxAtZero = BoxBody.Create(_simulation,
                                           new Vector3(0, 0, 0),
                                           Quaternion.Identity,
                                           Vector3.One * 10);

            // 1x1x1 at (5,5,5)
            var smallBox5 = BoxBody.Create(_simulation,
                                           new Vector3(5, 5, 5),
                                           Quaternion.Identity,
                                           Vector3.One);

            // 5x5x5 at (-7,0,0)
            var box7 = BoxBody.Create(_simulation,
                                      new Vector3(-7, 0, 0),
                                      Quaternion.Identity,
                                      Vector3.One * 5);

            // 1x10x1 at (0,8,0)
            var box8 = BoxBody.Create(_simulation,
                                      new Vector3(0, 8, 0),
                                      Quaternion.Identity,
                                      new Vector3(1, 10, 1));

            // 1x10x1 at (0,8,0) but rotated 90 deg around Z axis
            var box8Rotated = BoxBody.Create(_simulation,
                                             new Vector3(0, 8, 0),
                                             Quaternion.CreateFromAxisAngle(Vector3.UnitZ, (float)(Math.Tau / 4)),
                                             new Vector3(1, 10, 1));

            // 1x1x1 at (4,8.5,0.5)
            var box9 = BoxBody.Create(_simulation,
                                      new Vector3(4, 8.5f, 0.5f),
                                      Quaternion.Identity,
                                      new Vector3(1, 1, 1));

            Assert.IsTrue(PhysicsSimulation.BoxBoxCollision(boxAtZero, smallBox5));
            Assert.IsTrue(PhysicsSimulation.BoxBoxCollision(boxAtZero, box7));
            Assert.IsTrue(PhysicsSimulation.BoxBoxCollision(boxAtZero, box8));
            Assert.IsFalse(PhysicsSimulation.BoxBoxCollision(boxAtZero, box8Rotated));
            Assert.IsFalse(PhysicsSimulation.BoxBoxCollision(smallBox5, box7));
            Assert.IsTrue(PhysicsSimulation.BoxBoxCollision(box9, box8Rotated));
        }
Example #12
0
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        PhysicsSimulation sim = (PhysicsSimulation)target;

        if (GUILayout.Button("Simulate"))
        {
            sim.RunSimulation();
        }

        if (GUILayout.Button("Reset"))
        {
            sim.ResetAllBodies();
        }
    }
Example #13
0
        internal Workspace()
        {
            VoidHeight = -500;

            PlaceLoaded   = new Signal <string>(this);
            CameraChanged = new Signal <Camera>(this);

            CurrentCamera        = MakeDefaultCamera();
            Physics              = new PhysicsSimulation();
            RenderObjectProvider = new WorldRenderer();
            Gravity              = new Vector3(0, -196.2f, 0);

            Terrain = new Terrain(this);

            Game.AddWorld(this);
        }
    void Update()
    {
        for (int i = 0; i < datas.Count; i++)
        {
            datas[i].position = datas[i].transform.position;
            datas[i].rotation = datas[i].transform.rotation;
            datas[i].scale    = datas[i].transform.lossyScale;
            UpdateMesh(datas[i]);
        }

        PhysicsSimulation.Simulate(Time.deltaTime);

        for (int i = 0; i < datas.Count; i++)
        {
            datas[i].mesh.vertices      = datas[i].vertices;
            datas[i].transform.position = datas[i].position;
        }
    }
Example #15
0
        public void BoxSphereCollision()
        {
            // r=10 at (100,0,0)
            var sphereFar = SphereBody.Create(_simulation,
                                              new Vector3(100, 0, 0),
                                              10f);

            // r=4 at (7,0,0)
            var sphereClose = SphereBody.Create(_simulation,
                                                new Vector3(7, 0, 0),
                                                4f);

            // r=0.5 at (5,5,0)
            var sphere2 = SphereBody.Create(_simulation,
                                            new Vector3(5, 5, 0),
                                            0.5f);

            // r=0.5 at (6,0,0)
            var sphere3 = SphereBody.Create(_simulation,
                                            new Vector3(6, 0, 0),
                                            0.5f);

            // 10x10x10 at (0,0,0)
            var boxAtZero = BoxBody.Create(_simulation,
                                           new Vector3(0, 0, 0),
                                           Quaternion.Identity,
                                           Vector3.One * 10);

            // No rotation
            Assert.IsTrue(PhysicsSimulation.BoxSphereCollision(boxAtZero, sphereClose));
            Assert.IsFalse(PhysicsSimulation.BoxSphereCollision(boxAtZero, sphereFar));

            Assert.IsTrue(PhysicsSimulation.BoxSphereCollision(boxAtZero, sphere2));
            Assert.IsFalse(PhysicsSimulation.BoxSphereCollision(boxAtZero, sphere3));

            // Rotate box by 45deg around Z axis. Should now miss small sphere at (5,5,0) and hit small sphere at (6,0,0)
            var boxAtZeroRotated = BoxBody.Create(_simulation,
                                                  new Vector3(0, 0, 0),
                                                  Quaternion.CreateFromAxisAngle(Vector3.UnitZ, (float)Math.Tau / 8),
                                                  Vector3.One * 10);

            Assert.IsFalse(PhysicsSimulation.BoxSphereCollision(boxAtZeroRotated, sphere2));
            Assert.IsTrue(PhysicsSimulation.BoxSphereCollision(boxAtZeroRotated, sphere3));
        }
Example #16
0
        public void SphereSphereCollision()
        {
            // r=10 at (0,0,0)
            var sphereAtZero = SphereBody.Create(_simulation, new Vector3(0, 0, 0),
                                                 10f);

            // r=10 at (100,0,0)
            var sphereFar = SphereBody.Create(_simulation,
                                              new Vector3(100, 0, 0),
                                              10f);

            // r=4 at (11,0,0)
            var sphereClose = SphereBody.Create(_simulation,
                                                new Vector3(11, 0, 0),
                                                4f);

            Assert.IsFalse(PhysicsSimulation.SphereSphereCollision(sphereAtZero, sphereFar));
            Assert.IsTrue(PhysicsSimulation.SphereSphereCollision(sphereAtZero, sphereClose));
        }
Example #17
0
        public static void Update(GameTime gameTime)
        {
            for (int i = 0; i < gameSubSystems.Count; i++)
            {
                gameSubSystems[i].Update(gameTime);
            }

            if (CursorVisible)
            {
                if (!SystemCore.Game.IsMouseVisible)
                {
                    SystemCore.Game.IsMouseVisible = true;
                }
            }
            else
            {
                if (SystemCore.Game.IsMouseVisible)
                {
                    SystemCore.Game.IsMouseVisible = false;
                }
            }


            if (SystemCore.EscapeQuitsGame)
            {
                if (SystemCore.Input.KeyPress(Keys.Escape))
                {
                    SystemCore.Game.Exit();
                }
            }

            if (!PhysicsOnBackgroundThread)
            {
                PhysicsSimulation.Update((float)gameTime.ElapsedGameTime.TotalSeconds);
            }

            DebugText.Update(gameTime);

            EventManager.Update(gameTime.ElapsedGameTime);
        }
Example #18
0
        public Zone(ZoneInfo zoneInfo, UchuServer uchuServer, ushort instanceId = default, uint cloneId = default)
        {
            Zone       = this;
            ZoneInfo   = zoneInfo;
            UchuServer = uchuServer;
            InstanceId = instanceId;
            CloneId    = cloneId;

            EarlyPhysics  = new Event();
            LatePhysics   = new Event();
            OnPlayerLoad  = new Event <Player>();
            OnObject      = new Event <Object>();
            OnTick        = new Event();
            OnChatMessage = new Event <Player, string>();

            ScriptManager       = new ScriptManager(this);
            ManagedScriptEngine = new ManagedScriptEngine();
            UpdatedObjects      = new List <UpdatedObject>();
            ManagedObjects      = new List <Object>();
            SpawnedObjects      = new List <GameObject>();
            Simulation          = new PhysicsSimulation();

            Listen(OnDestroyed, () => { _running = false; });
        }
 public RayHitHandler(PhysicsSimulation simulation, Memory <RayHit> hits) : this()
 {
     Simulation = simulation;
     Hits       = hits;
 }
 public void LoadWithPhysicsControlData(VehiclePhysicsControlData controlData)
 {
     this.ControlData = controlData;
     Simulation       = new PhysicsSimulation(ControlData);
 }
 void OnApplicationQuit()
 {
     PhysicsSimulation.CleanSimulationData();
 }
Example #22
0
 static void ResetPhysicsOnSelected()
 {
     PhysicsSimulation.ResetAllBodies();
 }
Example #23
0
 public void SetupTest()
 {
     _simulation = new PhysicsSimulation();
 }
 void Start()
 {
     animator          = GetComponent <Animator>();
     transformPlayer   = GetComponent <Transform>();
     physicsSimulation = GetComponent <PhysicsSimulation>();
 }