示例#1
0
        private void Update()
        {
            Gamnet.Session.EventLoop.Update();

            deltaTime += Time.deltaTime;
            while (deltaTime >= Time.fixedDeltaTime)
            {
                deltaTime -= Time.fixedDeltaTime;
                physicsScene.Simulate(Time.fixedDeltaTime);
            }

            if (Room.State.Ready == room.state)
            {
                if (true == Input.GetMouseButtonUp(MOUSE_BUTTON_LEFT))
                {
                    room.state = Room.State.Play;
                    ball.transform.SetParent(room.transform);
                    ball.rigidBody.useGravity = false;
                    Packet.MsgCliSvr_Start_Ntf ntf = new Packet.MsgCliSvr_Start_Ntf();
                    Network.Send(ntf);
                }
            }

            // https://gamedevbeginner.com/how-to-convert-the-mouse-position-to-world-space-in-unity-2d-3d/#screen_to_world_3d
            if (Room.State.Init != room.state)
            {
                if (true == Input.GetMouseButton(MOUSE_BUTTON_LEFT))
                {
                    /*
                     * Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                     * RaycastHit hit;
                     * if (true == Physics.Raycast(ray, out hit, Mathf.Infinity))
                     * {
                     *  Bar bar = hit.transform.GetComponent<Bar>();
                     *  if (null != bar)
                     *  {
                     *      isTouched = true;
                     *  }
                     * }
                     */
                    float distance;
                    Ray   ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                    if (true == backPlane.Raycast(ray, out distance))
                    {
                        Vector3 worldPosition = ray.GetPoint(distance);
                        // 월드 좌표를 로컬 좌표로 이동
                        bar.destination = room.transform.InverseTransformPoint(worldPosition);

                        Packet.MsgCliSvr_SyncBar_Ntf ntf = new Packet.MsgCliSvr_SyncBar_Ntf();
                        ntf.destination = bar.destination;
                        Network.Send(ntf);
                    }
                }
                //bar.rigidBody.velocity = Vector3.zero;
            }
        }
    public void CreateTrajectory()
    {
        // match simulated golfball pos with actual golfball pos
        simGolfableRb.transform.SetPositionAndRotation(GameManager.Instance.GolfBall.transform.position, GameManager.Instance.GolfBall.transform.rotation);

        // apply initvelocity to simulated golfball
        simGolfableRb.velocity = GameManager.Instance.Controller.InitVelocity;

        // simulate trajectory
        for (int i = 0; i < steps; i++)
        {
            // Calculate vector between golfball and planet
            Vector3 between = _simTarget.transform.position - simGolfableRb.transform.position;

            // Calculate the acceleration that needs to be applied to golfball
            Vector3 acceleration = GameManager.Instance.GravitationalConstant * _simTarget.mass /
                                   between.sqrMagnitude * between.normalized;

            // Apply acceleration to rigidbody
            simGolfableRb.AddForce(acceleration, ForceMode.Acceleration);

            // run simulation for one fixed update
            _trajSimScene.Simulate(Time.fixedDeltaTime);

            // add pos to linerenderer every 5 simulated steps
            if (i % 5 == 0)
            {
                _renderer.SetPosition(i / 5, simGolfableRb.transform.position);
            }
        }

        // stop simulated golfball
        simGolfableRb.velocity = Vector3.zero;
    }
示例#3
0
 private void FixedUpdate()
 {
     if (UseManyWorlds)
     {
         _spawnedPhysicsScene.Simulate(Time.fixedDeltaTime);
     }
 }
示例#4
0
    void Update()
    {
        return;

        timer1 += Time.deltaTime;
        timer2 += Time.deltaTime;

        if (scene1Physics != null && scene1Physics.IsValid())
        {
            while (timer1 >= Time.fixedDeltaTime)
            {
                timer1 -= Time.fixedDeltaTime;

                //commenting out this line will stop the physics in scene1
                scene1Physics.Simulate(Time.fixedDeltaTime);
            }
        }

        if (scene2Physics != null && scene2Physics.IsValid())
        {
            while (timer2 >= Time.fixedDeltaTime)
            {
                timer2 -= Time.fixedDeltaTime;

                //commenting out this line will stop the physics in scene2
                scene2Physics.Simulate(Time.fixedDeltaTime);
            }
        }
    }
示例#5
0
    public void Simulate(PlayerController player, Vector3 currentPosition, Vector3 speed)
    {
        PlayerController simulatedPlayer;

        simulationRenderer.enabled = true;
        if (currentPhysicsScene.IsValid() && predictionPhysicsScene.IsValid())
        {
            dummy           = Instantiate(player.gameObject);
            simulatedPlayer = dummy.GetComponent <PlayerController>();
            SceneManager.MoveGameObjectToScene(dummy, predictionScene);

            dummy.transform.position = currentPosition;
            simulatedPlayer.Push(speed);
            simulationRenderer.positionCount = 0;
            simulationRenderer.positionCount = UIController.predictionCount;

            for (int i = 0; i < UIController.predictionCount; i++)
            {
                predictionPhysicsScene.Simulate(Time.fixedDeltaTime);
                simulationRenderer.SetPosition(i, simulatedPlayer.positionBone.transform.position);
            }

            Destroy(dummy);
        }
    }
示例#6
0
        private void DoPrediction()
        {
            GameObject predictionBall = Instantiate(_markerPrefab);

            SceneManager.MoveGameObjectToScene(predictionBall, scenePrediction);

            Rigidbody rigidbody = predictionBall.AddComponent <Rigidbody>();

            rigidbody.position = transform.position;
            rigidbody.AddForce(_force, ForceMode.Impulse);

            int markerIndex = 0;

            for (int i = 0; i < _predictionTotalIterations; i++)
            {
                scenePredictionPhysics.Simulate(Time.fixedDeltaTime);

                if (i % _predictionSteps == 0)
                {
                    _markerList[markerIndex].transform.position = predictionBall.transform.position;
                    markerIndex++;
                }
            }

            Destroy(predictionBall);
        }
    /// Linerenderer object  renderer by the ball position and force
    public void Predict(GameObject ball, Vector3 force)
    {
        if (currentPhysicsScene.IsValid() && predictionPhysicsScene.IsValid())
        {
            if (dummyBall == null)
            {
                dummyBall = Instantiate(ball);
                SceneManager.MoveGameObjectToScene(dummyBall, predictionScene);
            }
            dummyBall.GetComponent <Renderer>().enabled = false;
            dummyBall.transform.position = ball.transform.position;
            dummyBall.GetComponent <Collider>().enabled = true;
            dummyBall.GetComponent <Rigidbody>().AddForce(force, ForceMode.Impulse);
            lineRenderer.positionCount = 0;
            lineRenderer.positionCount = maxIterations;
            lineRenderer.SetPosition(0, ball.transform.position);
            for (int i = 1; i < maxIterations; i++)
            {
                predictionPhysicsScene.Simulate(Time.fixedDeltaTime);
                lineRenderer.SetPosition(i, dummyBall.transform.position);
            }

            Destroy(dummyBall);
        }
    }
示例#8
0
 void FixedUpdate() // shouldn't be necessary, but to be on the safe side.
 {
     if (!Physics.autoSimulation && _currentPhysicsScene.IsValid())
     {
         _currentPhysicsScene.Simulate(Time.fixedDeltaTime);
     }
 }
示例#9
0
 private void FixedUpdate()
 {
     mainPhysicsScene.Simulate(Time.fixedDeltaTime);
     transform.position = controller.transform.position;
     transform.rotation = controller.transform.rotation;
     hamsterSimulationPhyicsScene.Simulate(Time.fixedDeltaTime);
 }
示例#10
0
    public void predict(GameObject subject, Vector3 currentPosition, Vector3 force)
    {
        if (currentPhysicsScene.IsValid() && predictionPhysicsScene.IsValid())
        {
            if (dummy == null)
            {
                dummy = Instantiate(subject);
                SceneManager.MoveGameObjectToScene(dummy, predictionScene);
            }

            dummy.transform.position = currentPosition;
            dummy.GetComponent <Rigidbody>().AddForce(force, ForceMode.Impulse);
            lineRenderer.positionCount = 0;
            lineRenderer.positionCount = maxIterations;


            for (int i = 0; i < maxIterations; i++)
            {
                predictionPhysicsScene.Simulate(Time.fixedDeltaTime);
                lineRenderer.SetPosition(i, dummy.transform.position);
            }

            Destroy(dummy);
        }
    }
示例#11
0
 void FixedUpdate()
 {
     if (physicsScene != null && physicsScene.IsValid())
     {
         physicsScene.Simulate(Time.fixedDeltaTime);
     }
 }
示例#12
0
 private void FixedUpdate()
 {
     if (CreateUniquePhysicsScene)
     {
         _spawnedPhysicsScene.Simulate(Time.fixedDeltaTime);
     }
 }
 public void simulate()
 {
     if (simulation_enabled)
     {
         physics_scene.Simulate(simulation_speed);
     }
 }
示例#14
0
 void FixedUpdate()
 {
     if (batchPhysicsScene != null && batchPhysicsScene.IsValid())
     {
         batchPhysicsScene.Simulate(Time.fixedDeltaTime);
     }
 }
示例#15
0
    public void Predict(GameObject subject, Vector3 currentPosition, Vector3 force)
    {
        if (currentPhysicsScene.IsValid() && predictionPhysicsScene.IsValid())
        {
            if (dummy == null)
            {
                dummy = Instantiate(subject);
                SceneManager.MoveGameObjectToScene(dummy, predictionScene);
            }
            // print($"Prediction time for {dummy.name} created from {subject.name}\n at {currentPosition}");
            // Instantiate(subject, currentPosition, Quaternion.identity);

            dummy.transform.position = currentPosition;
            dummy.GetComponent <Rigidbody>().isKinematic = false;
            dummy.GetComponent <Rigidbody>().AddForce(force, ForceMode.Impulse);
            line.positionCount = 0;
            line.positionCount = maxIterations;

            // line.sortingOrder = 1;
            // line.material = new Material (Shader.Find ("Sprites/Default"));
            // line.material.color = Color.red;

            for (int i = 0; i < maxIterations; i++)
            {
                predictionPhysicsScene.Simulate(Time.fixedDeltaTime);
                line.SetPosition(i, dummy.transform.position);
            }

            Destroy(dummy);
        }
    }
示例#16
0
 void FixedUpdate()
 {
     if (currentPhysicsScene.IsValid())
     {
         currentPhysicsScene.Simulate(Time.fixedDeltaTime);
     }
 }
 private void FixedUpdate()
 {
     if (!_mainPhysicsScene.IsValid())//Check if our scene has valid physics.
     {
         return;
     }
     _mainPhysicsScene.Simulate(Time.fixedDeltaTime);//Simulated the main scene.
 }
示例#18
0
 private void FixedUpdate()
 {
     //Simulate the scene on FixedUpdate.
     if (physicsScene != null)
     {
         physicsScene.Simulate(Time.fixedDeltaTime * physicsSceneTimeScale);
     }
 }
示例#19
0
    public void Predict(/*GameObject subject,*/ Vector3 currentPosition, Vector3 force)
    {
        bool isInterrupted = false;

        if (currentPhysicsScene.IsValid() && predictionPhysicsScene.IsValid())
        {
            if (dummy == null)
            {
                //dummy = Instantiate(ballMain);
                dummy = FakeBall.fakeBallPool.Get();
                SceneManager.MoveGameObjectToScene(dummy, predictionScene);
            }

            dummy.transform.position = currentPosition;

            //dummy.GetComponent<Rigidbody>().WakeUp();
            dummy.GetComponent <Rigidbody>().AddForce(force, ForceMode.Impulse);
            if (lineRenderer == null)
            {
                lineRenderer = GetComponent <LineRenderer>();
            }
            lineRenderer.positionCount = maxIterations;

            Vector3[] positionsForLR = new  Vector3 [maxIterations];
            Vector3[] positionsForLRinterrupted;
            for (int i = 0; i < maxIterations; i++)
            {
                positionsForLR[i] = dummy.transform.position;
                predictionPhysicsScene.Simulate(Time.fixedDeltaTime);
                //if (Physics.SphereCast(dummy.transform.position, 0.1f,  dummy.transform.forward, out var hit, 100f, layer))
                if (IsOverlapSphere(dummy.transform.position, 0.5f, layer))
                {
                    positionsForLRinterrupted = new Vector3[i];
                    Array.Copy(positionsForLR, positionsForLRinterrupted, i);
                    lineRenderer.positionCount = i;
                    lineRenderer.SetPositions(positionsForLRinterrupted);
                    isInterrupted = true;
                    //return;  //weird behaviour
                    break;
                }
            }

            if (!isInterrupted)
            {
                lineRenderer.SetPositions(positionsForLR);
            }


            lineRenderer.Simplify(0.5f); //smoothing the line

            //dummy.GetComponent<Rigidbody>().velocity = Vector3.zero; ///didnt help
            //dummy.GetComponent<Rigidbody>().Sleep();///didnt help
            //SceneManager.MoveGameObjectToScene(dummy, currentScene); //didnt help
            //FakeBall.fakeBallPool.Free(dummy);

            Destroy(dummy);
        }
    }
示例#20
0
        public void CreateAndDropPhysicalCube()
        {
            PrimitiveBaseShape newcube  = PrimitiveBaseShape.CreateBox();
            Vector3            position = new Vector3(((float)Constants.RegionSize * 0.5f), ((float)Constants.RegionSize * 0.5f), 128f);
            Vector3            size     = new Vector3(0.5f, 0.5f, 0.5f);
            Quaternion         rot      = Quaternion.Identity;
            PhysicsActor       prim     = ps.AddPrimShape("CoolShape", newcube, position, size, rot, true);
            OdePrim            oprim    = (OdePrim)prim;
            OdeScene           pscene   = (OdeScene)ps;

            Assert.That(oprim.m_taintadd);

            prim.LocalID = 5;

            for (int i = 0; i < 58; i++)
            {
                ps.Simulate(0.133f);

                Assert.That(oprim.prim_geom != (IntPtr)0);

                Assert.That(oprim.m_targetSpace != (IntPtr)0);

                //Assert.That(oprim.m_targetSpace == pscene.space);
                m_log.Info("TargetSpace: " + oprim.m_targetSpace + " - SceneMainSpace: " + pscene.space);

                Assert.That(!oprim.m_taintadd);
                m_log.Info("Prim Position (" + oprim.m_localID + "): " + prim.Position.ToString());

                // Make sure we're above the ground
                //Assert.That(prim.Position.Z > 20f);
                //m_log.Info("PrimCollisionScore (" + oprim.m_localID + "): " + oprim.m_collisionscore);

                // Make sure we've got a Body
                Assert.That(oprim.Body != (IntPtr)0);
                //m_log.Info(
            }

            // Make sure we're not somewhere above the ground
            Assert.That(prim.Position.Z < 21.5f);

            ps.RemovePrim(prim);
            Assert.That(oprim.m_taintremove);
            ps.Simulate(0.133f);
            Assert.That(oprim.Body == (IntPtr)0);
        }
示例#21
0
    private void FixedUpdate()
    {
        if (!sceneMainPhysics.IsValid())
        {
            return;
        }

        sceneMainPhysics.Simulate(Time.fixedDeltaTime);
    }
示例#22
0
    void FixedUpdate()
    {
        if (!mainScenePhysics.IsValid())
        {
            return;
        }

        mainScenePhysics.Simulate(Time.fixedDeltaTime * physicsTimescale);
    }
示例#23
0
        private void updatePhysics(float timeSinceLastFrame)
        {
            while (!PhysicsScene.FetchResults(SimulationStatuses.AllFinished, false))
            {
            }

            PhysicsScene.Simulate(timeSinceLastFrame);
            PhysicsScene.FlushStream();
        }
示例#24
0
    /// <summary>
    /// 客户端 储存当前状态 并模拟下一帧
    /// </summary>
    /// <param name="current_state">当前状态</param>
    /// <param name="rigidbody">刚体</param>
    /// <param name="inputs">输入</param>
    /// <param name="dt">时间 间隔</param>
    private void ClientStoreCurrentStateAndStep(ref ClientState current_state, Rigidbody rigidbody, Inputs inputs, float dt)
    {
        current_state.position = rigidbody.position;
        current_state.rotation = rigidbody.rotation;

        this.PrePhysicsStep(rigidbody, inputs);

        client_physics_scene.Simulate(dt);
    }
示例#25
0
        // [TestCase(2f, 0.2f, 0.785f,   0.0f,  0.25f) /*, "Leaning 45 degrees to the side" */]
        // [TestCase(2f, 0.2f, 1.650f,   0.0f,  0.25f) /*, "Leaning more than 90 degrees to the side" */]
        // [TestCase(2f, 0.2f, 2.750f,   0.0f,  0.25f) /*, "Almost upside down, tipped right" */]
        // [TestCase(2f, 0.2f,-2.750f,   0.0f,  0.25f) /*, "Almost upside down, tipped left" */]
        // [TestCase(2f, 0.2f,   0.0f, 0.785f,  0.25f) /*, "Tipped back 45 degrees" */]
        // [TestCase(2f, 0.2f,   0.0f, 1.650f,  0.25f) /*, "Tipped back more than 90 degrees" */]
        // [TestCase(2f, 0.2f,   0.0f, 2.750f,  0.25f) /*, "Almost upside down, tipped back" */]
        // [TestCase(2f, 0.2f,   0.0f,-2.750f,  0.25f) /*, "Almost upside down, tipped forward" */]
        public void AngularVerticalAttraction(float timeScale, float efficiency, float initRoll, float initPitch, float initYaw)
        {
            // Enough simulation steps to cover the timescale the operation should take
            int simSteps = (int)(timeScale / simulationTimeStep) + 1;

            // Tip the vehicle
            Quaternion initOrientation = Quaternion.CreateFromEulers(initRoll, initPitch, initYaw);

            TestVehicle.Orientation = initOrientation;

            TestVehicle.Position = TestVehicleInitPosition;

            // The vehicle controller is not enabled directly (by setting a vehicle type).
            //    Instead the appropriate values are set and calls are made just the parts of the
            //    controller we want to exercise. Stepping the physics engine then applies
            //    the actions of that one feature.
            BSDynamics vehicleActor = TestVehicle.GetVehicleActor(true /* createIfNone */);

            if (vehicleActor != null)
            {
                vehicleActor.ProcessFloatVehicleParam(Vehicle.VERTICAL_ATTRACTION_EFFICIENCY, efficiency);
                vehicleActor.ProcessFloatVehicleParam(Vehicle.VERTICAL_ATTRACTION_TIMESCALE, timeScale);
                // vehicleActor.enableAngularVerticalAttraction = true;

                TestVehicle.IsPhysical = true;
                PhysicsScene.ProcessTaints();

                // Step the simulator a bunch of times and vertical attraction should orient the vehicle up
                for (int ii = 0; ii < simSteps; ii++)
                {
                    vehicleActor.ForgetKnownVehicleProperties();
                    vehicleActor.ComputeAngularVerticalAttraction();
                    vehicleActor.PushKnownChanged();

                    PhysicsScene.Simulate(simulationTimeStep);
                }
            }

            TestVehicle.IsPhysical = false;
            PhysicsScene.ProcessTaints();

            // After these steps, the vehicle should be upright

            /*
             * float finalRoll, finalPitch, finalYaw;
             * TestVehicle.Orientation.GetEulerAngles(out finalRoll, out finalPitch, out finalYaw);
             * Assert.That(finalRoll, Is.InRange(-0.01f, 0.01f));
             * Assert.That(finalPitch, Is.InRange(-0.01f, 0.01f));
             * Assert.That(finalYaw, Is.InRange(initYaw - 0.1f, initYaw + 0.1f));
             */

            Vector3 upPointer = Vector3.UnitZ * TestVehicle.Orientation;

            Assert.That(upPointer.Z, Is.GreaterThan(0.99f));
        }
示例#26
0
 // Update is called once per frame
 void FixedUpdate()
 {
     if (LocalPhysicsMode == LocalPhysicsMode.Physics3D)
     {
         PhysicsScene.Simulate(Time.fixedDeltaTime);
     }
     else if (LocalPhysicsMode == LocalPhysicsMode.Physics2D)
     {
         PhysicsScene2D.Simulate(Time.fixedDeltaTime);
     }
 }
示例#27
0
文件: Client.cs 项目: Adevien/Netcode
        void SimulateTick(ref SimulationStep step)
        {
            m_GetPlayer.HandlePlayer(step.input);
            m_ClientSimulation.Simulate(Constants.SNAPSHOT_INTERVAL);
            step.state.Set(m_GetPlayer.transform.position, m_GetPlayer.transform.rotation, m_GetPlayer.Yaw, m_GetPlayer.Pitch);

            _previous = _current;
            _current  = new PredictedState(m_GetPlayer.transform.position, m_GetPlayer.transform.rotation, m_GetPlayer.Yaw, m_GetPlayer.Pitch);

            //todo rebuild a simulation with a simple controller + camera to try the code
        }
    public IEnumerable Simulate()
    {
        // todo :: start simulate
        while (true)
        {
            yield return(new WaitForFixedUpdate());

            if (physicsScene.IsValid())
            {
                physicsScene.Simulate(Time.fixedDeltaTime);
            }
        }
    }
        public override void ProcessInput(RigidbodyInput input)
        {
            MoveToScene(_mainScene);

            var __force = new Vector3(input.movement.x, 0f, input.movement.y);

            __force *= _speed * input.deltaTime;
            _rigidbody.AddForce(__force, ForceMode.Impulse);

            _physicsScene.Simulate(input.deltaTime);

            MoveToScene(_idleScene);
        }
示例#30
0
        public void Refresh(double time)
        {
            menuAnimation1Instance1.RefreshControllers();
            camera3Animation1Instance1.RefreshControllers();

            GL.Clear(ClearBufferMask.DepthBufferBit);

            scene.Simulate(time);

            if (demo.EnableMenu)
            {
                scene.Draw(time);
            }
        }