void FixedUpdate() { if (currentPhysicsScene.IsValid()) { currentPhysicsScene.Simulate(Time.fixedDeltaTime); } }
void FixedUpdate() // shouldn't be necessary, but to be on the safe side. { if (!Physics.autoSimulation && _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. }
private void FixedUpdate() { if (!sceneMainPhysics.IsValid()) { return; } sceneMainPhysics.Simulate(Time.fixedDeltaTime); }
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); } }
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); } }
/// 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); } }
public void SimulateBallTrajectory(object force) { // skip simulation if result will be the same if (_cachedForce == (Vector3)force) { return; } else { _cachedForce = (Vector3)force; } Physics.autoSimulation = false; if (_currentPhysicsScene.IsValid() && _simulationPhysicsScene.IsValid()) { ResetBallTrajectory(); _simulatedCueBallRB.AddForce((Vector3)force, ForceMode.Impulse); _lineRenderer.positionCount = maxSimulationIterations + 1; _lineRenderer.SetPosition(0, cueBall.transform.position); for (int i = 0; i < maxSimulationIterations; i++) { _simulationScene.GetPhysicsScene().Simulate(Time.fixedDeltaTime); _lineRenderer.SetPosition(i + 1, _simulatedCueBallRB.transform.position); } } Physics.autoSimulation = true; }
void FixedUpdate() { if (physicsScene != null && physicsScene.IsValid()) { physicsScene.Simulate(Time.fixedDeltaTime); } }
void FixedUpdate() { if (batchPhysicsScene != null && batchPhysicsScene.IsValid()) { batchPhysicsScene.Simulate(Time.fixedDeltaTime); } }
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); } }
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); } } }
private void PredictForce(bool mustPredict) { if (!sceneMainPhysics.IsValid() || !scenePrediction.IsValid() || !scenePredictionPhysics.IsValid()) { return; } if (mustPredict || _predictionSteps != _predictionStepsLastUsed || _predictionTotalIterations != _predictionTotalIterationsLastUsed) { StartCoroutine(RebuildAndDoPrediction_Coroutine()); } else { if (_forceLastUsed == _force) { return; } } _forceLastUsed = _force; _predictionStepsLastUsed = _predictionSteps; _predictionTotalIterationsLastUsed = _predictionTotalIterations; StartCoroutine(UseMarkers_Coroutine()); }
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); } }
//Predict: predicts the path of the ball and renders lines. public void Predict(Vector3 force) { _lineRenderer.enabled = true; //Enabled our line renderer. if (!_mainPhysicsScene.IsValid() || !_predictionPhysicsScene.IsValid()) //Check if our both scenes has valid physics. { return; } _predictionBall = Instantiate(_objPrefab, _objTransform.position, Quaternion.identity); //Instantiated one of our prediction ball. SceneManager.MoveGameObjectToScene(_predictionBall, _predictionScene); //Set our prediction ball to prediction scene. _predictionBall.GetComponent <Rigidbody>().AddForce(force); //Added force to prediction ball. for (int i = 0; i < _linePositionAmount; i++) //Loop that turns _linePositionAmount times. { _predictionPhysicsScene.Simulate(Time.fixedDeltaTime); //Simulate our prediction physics. _lineRenderer.SetPosition(i, _predictionBall.transform.position); //Rendered a line at prediction ball's position. } Destroy(_predictionBall);//Destroyed our prediction ball, we don't need it. }
public IEnumerable Simulate() { // todo :: start simulate while (true) { yield return(new WaitForFixedUpdate()); if (physicsScene.IsValid()) { physicsScene.Simulate(Time.fixedDeltaTime); } } }
/************************************** * ACTIONS */ void UpdateTrajectory() { // reset the (hidden) scene to match the active scene (the table never moves so only worry about the balls) foreach (BallController ball in allBalls) { ball.Reset(); } // update the force we'll apply to the active ball applyForce.Set(horizontalForce * Mathf.Cos(forceAngle), verticalForce, horizontalForce * Mathf.Sin(forceAngle)); prevAngle = forceAngle; prevHorForce = horizontalForce; prevVertForce = verticalForce; // make sure the scenes are valid and a ball is selected if (!mainScenePhysics.IsValid() || !trajectoryPhysicsScene.IsValid() || selection.activeBody == null) { return; } BallController bc = selection.activeBody.gameObject.GetComponent <BallController>(); bc.UpdateTrajectory(applyForce, simIterations, physicsTimescale); }
void Awake() { if (NetworkServer.active) { physicsScene = gameObject.scene.GetPhysicsScene(); simulatePhysicsScene = physicsScene.IsValid() && physicsScene != Physics.defaultPhysicsScene; physicsScene2D = gameObject.scene.GetPhysicsScene2D(); simulatePhysicsScene2D = physicsScene2D.IsValid() && physicsScene2D != Physics2D.defaultPhysicsScene; } else { enabled = false; } }
private void Start() { if (IsServer) { physicsScene = gameObject.scene.GetPhysicsScene(); simulatePhysicsScene = physicsScene.IsValid() && physicsScene != Physics.defaultPhysicsScene; physicsScene2D = gameObject.scene.GetPhysicsScene2D(); simulatePhysicsScene2D = physicsScene2D.IsValid() && physicsScene2D != Physics2D.defaultPhysicsScene; } else { enabled = false; } }
void UpdateTracking() { if (!m_EnvironmentScene.IsValid() || !m_EnvironmentPhysicsScene.IsValid()) { return; } var marsCamera = MarsRuntimeUtils.GetActiveCamera(true); if (marsCamera == null) { return; } foreach (var kvp in m_SimulatedTrackableObjects) { var synthTrackable = kvp.Key; var simObject = kvp.Value; var previouslyFound = m_DiscoveredTrackables.Contains(synthTrackable); if (!synthTrackable.isActiveAndEnabled || !simObject.isActiveAndEnabled) { if (previouslyFound) { m_DiscoveredTrackables.Remove(synthTrackable); RemoveTrackableData(synthTrackable); } continue; } var trackingState = GetTrackingState(synthTrackable, marsCamera); var tracking = trackingState != MARSTrackingState.Unknown; if (!previouslyFound) { if (!tracking) { return; } m_DiscoveredTrackables.Add(synthTrackable); AddTrackableData(synthTrackable, trackingState); } else { UpdateTrackableData(synthTrackable, trackingState); } } }
protected virtual void OnPhysics(PhysicsScene 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... }
void UpdateTracking() { if (!m_EnvironmentScene.IsValid() || !m_EnvironmentPhysicsScene.IsValid()) { return; } var marsCamera = MarsRuntimeUtils.GetActiveCamera(true); if (marsCamera == null) { return; } foreach (var synthesizedBody in m_SynthMarkers) { UpdateTracking(synthesizedBody, marsCamera); } }
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 = maxIterations; lineRenderer.material = mat1; for (int i = 0; i < maxIterations; i++) { predictionPhysicsScene.Simulate(Time.fixedDeltaTime); lineRenderer.SetPosition(i, dummy.transform.position); // Change color of the Prediction line as soon as it hit the star and stop the line from moving. Vector3 p = Vector3.zero; p = lineRenderer.GetPosition(i); Collider[] hits = Physics.OverlapSphere(p, 0.01f); foreach (var hitCollider in hits) { if (hitCollider.tag == "Goal") { lineRenderer.material = mat2; // StateManager.instance.player.GetComponent<Shooter>().rotationSpeed = 0; i = maxIterations; break; } } } Destroy(dummy); } }
public void Create(Vector3 pos, Vector3 force) { if (!scenePredictionPhysics.IsValid()) { return; } Clear(); Rigidbody predictionBall = Instantiate(prediction, pos, Quaternion.identity); SceneManager.MoveGameObjectToScene(predictionBall.gameObject, scenePrediction); predictionBall.AddForce(force); for (int i = 0; i < predictionCycles; i++) { scenePredictionPhysics.Simulate(Time.fixedDeltaTime); AddPoint(i, predictionBall.transform.position); } Destroy(predictionBall.gameObject); }
private void ShootBall() { if (!sceneMainPhysics.IsValid() || !scenePredictionPhysics.IsValid()) { return; } GameObject ball = GameObject.CreatePrimitive(PrimitiveType.Sphere); SceneManager.MoveGameObjectToScene(ball, sceneMain); ball.AddComponent <Rigidbody>().AddForce(applyForce, ForceMode.Impulse); GameObject predictionBall = GameObject.CreatePrimitive(PrimitiveType.Sphere); SceneManager.MoveGameObjectToScene(predictionBall, scenePrediction); predictionBall.AddComponent <Rigidbody>().AddForce(applyForce, ForceMode.Impulse); Material redMaterial = new Material(Shader.Find("Diffuse")); redMaterial.color = Color.red; for (int i = 0; i < 500; i++) { scenePredictionPhysics.Simulate(Time.fixedDeltaTime); GameObject pathMarkSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); pathMarkSphere.GetComponent <Collider>().isTrigger = true; pathMarkSphere.transform.localScale = new Vector3(.2f, .2f, .2f); pathMarkSphere.transform.position = predictionBall.transform.position; pathMarkSphere.GetComponent <MeshRenderer>().material = redMaterial; SceneManager.MoveGameObjectToScene(pathMarkSphere, scenePrediction); } Destroy(predictionBall); Debug.Break(); }