夜風のMixedReality

xRと出会って変わった人生と出会った技術を書き残すためのGeekなHoloRangerの居場所

HoloLensで惑星運動を再現する

最近調査枠が多いので本日は少し趣向を変えHoloLensの表現枠です。

〇HoloLens×教育

HoloLens では現実の重力や一定の物理作用を無視してオブジェクトを配置、描画することができます。

また遠隔非接触にも対応しておりこれから注目の技術でもあります。

筆者は以前より教科書をHoloLensで再現したいという夢があったのですが今回その第一成果をまとめます。

〇惑星運動

HoloLensアプリ[GalaxyExplorer]では太陽系の惑星の位置関係や銀河系に関して学ぶことができます。

このプロジェクトはMixedRealityDesignLabで公開されています。

github.com

今回このGalaxyExplorerにインスピレーションを受け惑星運動を再現してみることにしました。

惑星運動は惑星の持つ質量に応じた万有引力に従って運動しています。 またケプラーの法則にのっとり、楕円軌道を描きます。

今回はこれを再現します。

①[GalaxyExplorere]のアセットから太陽と地球のアセットを配置します。(独自Shaderの関係で描画が美しくありませんが今回はいったん無視します。)

f:id:Holomoto-Sumire:20210401174759j:plain

②次のスクリプトを書きました。

これは以下の記事よりスクリプトを使用させていただきました。

mzmlab.hatenablog.com

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteAlways]
public class Gravitation : MonoBehaviour
{
   [SerializeField] private GameObject sun;
   public float initVelocityZ;
   private float f;
   private float m;
   private float M;
   private float r;
   private float G = 6.67E-5f;

   public  bool test;
   private bool tests=false;
   private Vector3 startpos;
   private void Start()
   {
      Vector3 initVelocity = new Vector3(0, 0, initVelocityZ);
      GetComponent<Rigidbody>().velocity = initVelocity;

      m = GetComponent<Rigidbody>().mass;
      M = sun.GetComponent<Rigidbody>().mass;
      startpos = this.transform.position;
   }

   private void Update()
   {
      if (test)
      {
         this.transform.position = startpos;
         tests = true;
      }
      else
      {
         if (tests)
         {
            Start();
            tests = false;
         }
      }
   }

   private void FixedUpdate()
   {
      Vector3 direction = sun.transform.position - GetComponent<Transform>().position;
      r = direction.magnitude;
      direction.Normalize();

      f = G * m * M / (r * r);
      GetComponent<Rigidbody>().AddForce(f*direction,ForceMode.Force);
   }
}

③地球のオブジェクト、太陽のオブジェクトに[RigidBody]をアタッチし、[UseGravity]のチェックを外します。

これによって重力ではなくフォースとしてRigidBodyを使用できます。

④太陽のオブジェクトの[RigidBody]から[Mass]の値を大きくします。 ここでは[1000]を指定しました。

f:id:Holomoto-Sumire:20210401183825j:plain

⑤地球のオブジェクトに[Gravitation]コンポーネントをアタッチします。

f:id:Holomoto-Sumire:20210401184110j:plain

以上で設定完了です。

Editor上では次のような運動を示します。

f:id:Holomoto-Sumire:20210401183626g:plain

太陽に近づいた時速度が速くなっていることがわかります。 またケプラーの法則に従い楕円軌道を描きます。

〇実機見る

実機では次の様になります。

youtu.be

今回思い付きで実装しましたが理科教育などで非常に相性良く使えそうです。