Example #1
0
 void SimulateDashForNFrames(Vector2 dir, int frameCount, Movement agentMovement)
 {
     agentMovement.Dash(dir.x, dir.y);
     for (int i = 0; i < frameCount; i++)
     {
         simPhysicsScene2D.Simulate(Time.fixedDeltaTime);
     }
 }
Example #2
0
        private void StoreCurrentStateAndStep(ref ClientState state, InputFrame input, float deltaTime)
        {
            if (!Initialized && !ConnectedToServer)
            {
                return;
            }

            for (int i = 0; i < m_updaters.Count; i++)
            {
                m_updaters[i].SetStateAndStep(ref state, input, deltaTime);
            }

            m_clientPhysics.Simulate(deltaTime);
        }
    public void predict(GameObject subject, Vector3 currentPosition, Vector2 launchVelocity)
    {
        if (currentPhysicsScene.IsValid() && predictionPhysicsScene.IsValid())
        {
            if (dummy == null)
            {
                dummy = Instantiate(subject);
                SceneManager.MoveGameObjectToScene(dummy, predictionScene);
            }

            dummy.transform.position = currentPosition;
            dummy.GetComponent<Rigidbody2D>().velocity = launchVelocity;
            //lineRenderer.positionCount = 0;
            //lineRenderer.positionCount = maxIterations;

            
            for (int i = 0; i < maxIterations; i++){
                predictionPhysicsScene.Simulate(Time.fixedDeltaTime);
                if (i % divider == 0){
                    dots[i/divider].transform.position = dummy.transform.position;

                }
                //lineRenderer.SetPosition(i, dummy.transform.position);
            }

            Destroy(dummy);
        }
    }
Example #4
0
    void Start()
    {
        //Create simulation scene
        Scene theScene = SceneManager.CreateScene(
            "PhysicsSimulation",
            new CreateSceneParameters(LocalPhysicsMode.Physics2D)
            );

        _physicsScene = theScene.GetPhysicsScene2D();

        //Create object that will be simulated
        _objectToSimulte = GameObject.Instantiate(ObjectToSimulatePrefab);
        SceneManager.MoveGameObjectToScene(_objectToSimulte, theScene);

        //Perform starting simulation
        SimulatedObjectLogic theSimulatedObjectLogic =
            _objectToSimulte.GetComponent <SimulatedObjectLogic>();

        theSimulatedObjectLogic.launch(new Vector2(200.0f, 500.0f));
        for (int theStepIndex = 0; theStepIndex < 50; ++theStepIndex)
        {
            theSimulatedObjectLogic.draw();

            theSimulatedObjectLogic.simulateStep();
            _physicsScene.Simulate(0.02f);
        }
    }
    // get a new pos_rec
    public List <Vector3> get_pred(Vector3 start_pos, Vector3 add_force)
    {
        Debug.Log("Projection Scene: " + gameObject.scene.name);
        PhysicsScene2D phys2d = gameObject.scene.GetPhysicsScene2D();

        Physics2D.autoSimulation = false;

        // init vars
        pos_rec.Clear();
        transform.position = start_pos;
        rb2.velocity       = Vector2.zero;
        rb2.AddForce(add_force);
        pos_rec.Add(transform.position);
        float time_count = 0f;
        int   time_scale = (int)(timestep / sim_step);

        // begin prediction
        while (time_count <= time_max)
        {
            // do simulation
            for (int a = 0; a < time_scale; a++)
            {
                phys2d.Simulate(sim_step);
            }
            // add to list
            time_count += timestep;
            pos_rec.Add(transform.position);
        }
        return(pos_rec);
    }
Example #6
0
    private void FixedUpdate()
    {
        //Simulate next steps in main scene FixedUpdate
        SimulatedObjectLogic theSimulatedObjectLogic =
            _objectToSimulte.GetComponent <SimulatedObjectLogic>();

        theSimulatedObjectLogic.draw();

        theSimulatedObjectLogic.simulateStep();
        _physicsScene.Simulate(0.02f);
    }
 void FixedUpdate()
 {
     if (currentPhysicsScene.IsValid())
     {
         if (!hasCopied)
         {
             copyAllObstacles();
             hasCopied = true;
         }
         currentPhysicsScene.Simulate(Time.fixedDeltaTime);
     }
 }
Example #8
0
        private void FixedUpdate()
        {
            if (!IsServer)
            {
                return;
            }

            if (simulatePhysicsScene)
            {
                physicsScene.Simulate(Time.fixedDeltaTime);
            }

            if (simulatePhysicsScene2D)
            {
                physicsScene2D.Simulate(Time.fixedDeltaTime);
            }
        }
Example #9
0
        void FixedUpdate()
        {
            if (!NetworkServer.active)
            {
                return;
            }

            if (simulatePhysicsScene)
            {
                physicsScene.Simulate(Time.fixedDeltaTime);
            }

            if (simulatePhysicsScene2D)
            {
                physicsScene2D.Simulate(Time.fixedDeltaTime);
            }
        }
Example #10
0
        protected virtual void OnPhysics2D(PhysicsScene2D physicsScene)
        {
            if (!physicsScene.IsValid())
            {
                return; // do nothing if the physics Scene is not valid.
            }
            _physicsTimer += Time.deltaTime;

            // Catch up with the game time.
            // Advance the physics simulation in portions of Time.fixedDeltaTime
            // Note that generally, we don't want to pass variable delta to Simulate as that leads to unstable results.
            while (_physicsTimer >= Time.fixedDeltaTime)
            {
                _physicsTimer -= Time.fixedDeltaTime;
                physicsScene.Simulate(Time.fixedDeltaTime);
            }

            // Here you can access the transforms state right after the simulation, if needed...
        }
Example #11
0
    void FixedUpdate()
    {
        for (int i = 0; i < simulationSteps; i++)
        {
            activePhysicsScene2D.Simulate(Time.fixedDeltaTime);
            if (referenceBallRigidbody2D.velocity == Vector2.zero)
            {
                reachedSteadyState         = true;
                ballSpriteRenderer.color   = Color.green;
                ballSpriteRenderer.enabled = true;
                break;
            }
            else
            {
                ballSpriteRenderer.color = Color.red;
                //ballSpriteRenderer.enabled = false;
            }
        }

        if (horizontalInput > 0.1 || horizontalInput < -0.1)
        {
            ballMoved = true;
            Walk(horizontalDir);
        }


        // if (isJumpPressed == true)
        // {
        //     Jump(referenceBallRigidbody2D);
        // }

        // if ball pos changed, and current sim has stopped, resimulate physics
        if (ballMoved == true && referenceBallRigidbody2D.velocity == Vector2.zero)
        {
            go.transform.position = ballToSpawn.transform.position;
            //Jump(referenceBallRigidbody2D);
            ballMoved = false;
        }
    }
Example #12
0
    void performSimpleFlyingInGravityTest()
    {
        //Create simulation scene
        Scene theScene = SceneManager.CreateScene(
            "PhysicsSimulation",                                  /*Scene name*/
            new CreateSceneParameters(LocalPhysicsMode.Physics2D) /*Type of physics on scene*/
            );
        PhysicsScene2D thePhysicsScene = theScene.GetPhysicsScene2D();

        //Create object that will be simulated
        GameObject theGameObject = new GameObject();
        var        theCollider   = theGameObject.AddComponent <BoxCollider2D>();
        var        theRigidBody  = theGameObject.AddComponent <Rigidbody2D>();

        //Object is created in scene that is currently active.
        // So we should move object to simulation scene using
        // SceneManager.MoveGameObjectToScene(...) or
        // set simulation scene as active using
        // SceneManager.SetActiveScene(...)
        SceneManager.MoveGameObjectToScene(theGameObject, theScene);

        //Perform simulation
        //-Give starting force
        theRigidBody.AddForce(new Vector2(200.0f, 500.0f));

        for (int theStepIndex = 0; theStepIndex < 50; ++theStepIndex)
        {
            //-Apply gravity (instead of default created physics
            //- we need to apply it manually)
            theRigidBody.AddForce(new Vector2(0.0f, -9.8f));

            //-Draw current object position in simulation
            drawSquare(theGameObject.transform.position, theCollider.size, Color.green, 10.0f);

            //-Perform scene simulation step with delta time 0.02 seconds
            thePhysicsScene.Simulate(0.02f);
        }
    }
Example #13
0
        protected override void StateFixedUpdate()
        {
            lock (m_lock)
            {
                if (++m_bufferedMasterTick > m_simulationBuffer + m_masterTick)
                {
                    foreach (int id in m_clientStates.Keys)
                    {
                        if (!m_connectedClients[id])
                        {
                            continue;
                        }

                        ClientState client = m_clientStates[id];
                        // if input buffer has a frame corresponding to this tick
                        InputFrame frame = null;
                        if (!m_clientInputBuffers[client].ContainsKey(m_masterTick))
                        {
#if DEBUG_LOG
                            Debug.Log("Missed a player input from " + id + " for tick " + m_masterTick);
#endif //DEBUG_LOG
                            // TODO : Cache new default frame ?
                            frame = new InputFrame(); // create a default frame to not move player
                        }
                        else
                        {
                            frame = m_clientInputBuffers[client][m_masterTick];
                        }

                        // must be called in main unity thread
                        foreach (ServerGameplayStateUpdater updater in m_updaters)
                        {
                            updater.FixedUpdateFromClient(client, frame, Time.fixedDeltaTime);
                        }

                        m_toRemoveCache.Clear();
                        foreach (int tick in m_clientInputBuffers[client].Keys)
                        {
                            if (tick <= m_masterTick)
                            {
                                m_toRemoveCache.Add(tick);
                            }
                        }

                        for (int i = 0; i < m_toRemoveCache.Count; i++)
                        {
                            m_clientInputBuffers[client].Remove(m_toRemoveCache[i]);
                        }
                    }

                    m_serverPhysics.Simulate(Time.fixedDeltaTime);

                    m_masterTick++;

                    foreach (int id in m_clientStates.Keys)
                    {
                        if (!m_connectedClients[id])
                        {
                            continue;
                        }

                        ClientState client = m_clientStates[id];
                        foreach (ServerGameplayStateUpdater updater in m_updaters)
                        {
                            updater.UpdateClient(client);
                        }
                        client.Tick.Value = (uint)m_masterTick;
                    }
                }

                if (++m_tickAccumulator > m_snapshotTicks)
                {
                    m_tickAccumulator = 0;
                    foreach (int id in m_connectedClients.Keys)
                    {
                        if (m_connectedClients[id])
                        {
                            m_UDPServer.Send(m_clientStates[id].GetBytes(), id);
                        }
                    }
                }
            }
        }
Example #14
0
 protected override void SimulateInternal(float fixedDeltaTime) => _world.Simulate(fixedDeltaTime);