2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

三角関数で球を回す

Posted at

デモ:http://mo49.tokyo/threejs/earth_trigonometric.html

sin, cosを x, y片方ずつに使っている「赤」「黄」の球は円を描きながら回転していることがわかる。

x y
cos sin
sin cos
sin sin
sin tan

javascript
window.addEventListener("load", init);
function init() {
    // シーン
    var scene = new THREE.Scene();

    // カメラ
    var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000);
    camera.position.set(0, 0, 1000);
    camera.lookAt(new THREE.Vector3(0, 0, 0));

    // レンダラー
    var renderer = new THREE.WebGLRenderer({ antialias: true });
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);
    var degree = 0; // 角度
    var radius = 300; // 半径
    var radius2 = 150; // 半径
    var radius3 = 50; // 半径
    var radius4 = 100; // 半径

    // 球
    var sphere = new THREE.Mesh(new THREE.SphereGeometry(20), new THREE.MeshBasicMaterial({ color: 0xffff00, wireframe: true }));
    scene.add(sphere);
    var sphere2 = new THREE.Mesh(new THREE.SphereGeometry(20), new THREE.MeshBasicMaterial({ color: 0xff0000, wireframe: true }));
    scene.add(sphere2);
    var sphere3 = new THREE.Mesh(new THREE.SphereGeometry(20), new THREE.MeshBasicMaterial({ color: 0x0000ff, wireframe: true }));
    scene.add(sphere3);
    var sphere4 = new THREE.Mesh(new THREE.SphereGeometry(20), new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: true }));
    scene.add(sphere4);
    // 地球
    var earth = new THREE.Mesh(new THREE.SphereGeometry(250, 20, 20), new THREE.MeshBasicMaterial({ wireframe: true }));
    scene.add(earth);

    // フレーム毎の再描画
    function ballRotate() {
        // 球を回転させる
        degree += 1;
        // 角度をラジアンに変換
        var rad = degree * Math.PI / 180;
        // xとyでsin,cosを入れ替えると逆回転する
        // http://www.ajaxtower.jp/js/math_class/index9.html
        var x = radius * Math.cos(rad);// X座標 = 半径 x Cosθ
        var y = radius * Math.sin(rad);// Y座標 = 半径 x Sinθ
        var x2 = radius2 * Math.sin(rad);
        var y2 = radius2 * Math.cos(rad);
        var x3 = radius3 * Math.sin(rad);
        var y3 = radius3 * Math.sin(rad);
        var x4 = radius4 * Math.sin(rad);
        var y4 = radius4 * Math.tan(rad);
        sphere.position.set(x, y, 0);
        sphere2.position.set(x2, y2, 0);
        sphere3.position.set(x3, y3, 0);
        sphere4.position.set(x4, y4, 0);

        renderer.render(scene, camera);

        requestAnimationFrame(ballRotate);
    }
    ballRotate();
}

参考
https://ics.media/entry/10657

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?