Skip to content

Instantly share code, notes, and snippets.

@iwaken71
Last active July 20, 2020 07:55
Show Gist options
  • Save iwaken71/1a3bbba55f8b61f72e0d04a16de42235 to your computer and use it in GitHub Desktop.
Save iwaken71/1a3bbba55f8b61f72e0d04a16de42235 to your computer and use it in GitHub Desktop.
ローレンツ方程式の挙動をUnityで再現するためのScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButterflyEffect : MonoBehaviour
{
[SerializeField] Transform ball; //このオブジェクトにTrail Rendererをつけると可愛い
[SerializeField] float scale = 0.002f; //scale=1だと大きすぎてARグラスで観測しにくい
LorenzModel model;
Vector3 initBallPosition;
// Start is called before the first frame update
void Start()
{
model = new LorenzModel(Time.fixedDeltaTime);
initBallPosition = ball.transform.position;
}
void FixedUpdate() {
UpdatePosition();
}
void UpdatePosition() {
model.UpdatePosition() ;
ball.transform.position = initBallPosition + model.GetPosition()*scale;
}
}
public class LorenzModel {
Vector3 position;
Vector3 velocity;
float deltaTime; //微小時間 = Time.fixedDeltaTime
public LorenzModel(float deltaTime) {
this.deltaTime = deltaTime;
this.position = new Vector3(0,0.001f,0); //初期位置が原点だと動かないので適当にずらす。
}
//p,r,b:有名な初期値設定を採用
float p = 10f;
float r = 28f;
float b = 8f/3f;
public void UpdatePosition() {
velocity = new Vector3(-p*position.x+p*position.y,-position.x*position.z+r*position.x-position.y,position.x*position.y-b*position.z);
position = position + velocity * deltaTime;
}
public Vector3 GetPosition() {
return this.position;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment