unityで最速降下曲線

unityでサイクロイド曲線にボール転がしてみた。

 

まずサイクロイド曲線のパラメータを決める必要がある。

θ=0で(x,y)=(0,0)とすると

x=A(θ-sinθ)

y=A(1-cosθ)

という式で表される。(0,0)と(x0,x0)を結ぶことにすると

A(θ0-sinθ0)=A(1-cosθ0)

となる。θ0について解けばθ0≒2.41201となる。あとは具体的にx0の値を決めればAも決まる。

x0=-10として次のコードでサイクロイドの座標を生成した。

 

#include <stdio.h>
#include <math.h>

int main()
{
    double theta0 = 2.41201;
    double x0 = -10, y0 = -10;
    double A = y0 / (1 - cos(theta0));

    for (double theta=0; theta<=theta0; theta+=0.05)
    {
        double x = A*(theta - sin(theta));
        double y = A*(1 - cos(theta));
        printf("%e %e¥n", x, y);
    }

    return 0;
}

 

座標データをjwcadに取り込んでdxfファイルに変換し、creo elementsに取り込んで坂を作成した。生成したstlファイルをblenderに取り込んでobjファイルをエクスポートすればunityにインポートできる。

creo elementsに取り込むときの注意としては、ちゃんと坂をワールド原点付近にとらないと、unityで配置するとき苦労する。

スタートゲートは上キーで開くようにした。

    void Update () {
       // Destroy(gameObject);
        if (Input.GetAxis("Vertical")!=0) Destroy(gameObject);
    }

ジャジャガッチ | コンピュータ | 22:56 | comments(0) | trackbacks(0) |

unityに任意曲面データを取り込む

任意曲面でボール転がしてみたいのでまずは3dデータを作りましょう。

まずは3次元の数値データを用意します。

z = √x^2+y^2としました。

 

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

int main() {
    for (double x = -10; x <= 10; x += 0.1)
        for (double y = -10; y <= 10; y += 0.1)
        {
            printf("%f %f %f¥n", x, y, sqrt(x*x + y*y));

        }
}

 

これをMeshで読み込ませた後、

 

Filters/Pointset/Compute nomals for point seets/

 

を実行。次に

 

Filters/Remeshing simpleification and Reconstruction/Surface Reconstruction : Ball Pivoting/

 

を実行してメッシュにします。最後にスムージング。

 

Filters/Smooth ,Fairing and Deformation/Laplacian Smooth/

 

あとは適当な形式で保存すればOK。

 

このままunityに取り込めばいけるかなと思ったが、どうもうまくいかない。

厚みがゼロなのが悪いのかな。

3dプリンタも視野に入れて厚みをつけとくか。

 

blenderにMeshLabから出力したファイルを読み込ませます。

 

左下のボタンでモード変更してエディットモードにします。

aキーを押すと全選択されます。Ctrl-F=>Solidifyで厚みがつきます。

左側にThicknessというウィンドウが表示されるのでここで厚みを変えれます。

あとはobjとかでエクスポートすればOK。

 

あとはunityで取り込んでdefault_MeshPart*のMeshColiderにチェックを入れればOK。テンション上がる!

副産物として3Dプリンタで任意曲面作れるようになった。

次はサイクロイド取り込む?

ジャジャガッチ | コンピュータ | 14:48 | comments(0) | trackbacks(0) |

unityおもろいな

久しぶりにゲーム作りたくなったので、かなり前から話題のunityに手を出してみた。

C#はよく知らないがきっと大丈夫。

とりあえず玉転がしのチュートリアルを飛ばし飛ばしなんとなくやってみた。

すげえ、簡単にゲーム作れそう。

ジャジャガッチ | コンピュータ | 14:07 | comments(0) | trackbacks(0) |

codingame

paizaの配信メールでcode warriorsというブラウザゲームを知った。

コーディングしてロボット同士で戦うというもので面白そうなのでやってみた。

が、あんまりコーディングする意味を感じなくて面白くなかった。チュートリアルしかやってないので先に進めば面白いのかもしれないけど。ターン制なのでコマンド選択をコードでやってるだけという感じがしたんだけど。

それで消化不良感があったのでempire of codeというゲームを試してみた。リアルタイムストラテジか?こっちのほうが面白そうな感じはしたが、やはり何か求めてるものとは違う感があってチュートリアルだけでやめてしまった。

 

次にcodingameを試してみた。ミニゲームをクリアしていく感じで面白そう。

というわけでいっこやってみた。画面は下のような感じ。

このゲームの目的は主人公を雷マークへ導くこと。

最初は次のコードが雛形として与えられている。

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 * ---
 * Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders.
 **/
int main()
{
    int lightX; // the X position of the light of power
    int lightY; // the Y position of the light of power
    int initialTX; // Thor's starting X position
    int initialTY; // Thor's starting Y position
    cin >> lightX >> lightY >> initialTX >> initialTY; cin.ignore();

    // game loop
    while (1) {
        int remainingTurns; // The remaining amount of turns Thor can move. Do not remove this line.
        cin >> remainingTurns; cin.ignore();

        // Write an action using cout. DON'T FORGET THE "<< endl"
        // To debug: cerr << "Debug messages..." << endl;


        // A single line providing the move to be made: N NE E SE S SW W or NW
        cout << "SE" << endl;
    }
}

 

coutで移動できる仕様となっている。これを改変して目的を達成するわけだ。

動かすと主人公が実際に移動する様子が見えてうれしい。

やってることは競技プログラミングみたいなもんなんだけど、絵があると断然うれしいな。

次のコードでクリアできた。

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 * ---
 * Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders.
 **/
int main()
{
    int lightX; // the X position of the light of power
    int lightY; // the Y position of the light of power
    int initialTX; // Thor's starting X position
    int initialTY; // Thor's starting Y position
    cin >> lightX >> lightY >> initialTX >> initialTY; cin.ignore();
    
    int tx = initialTX;
    int ty = initialTY;
    // game loop
    while (1) {
        int remainingTurns; // The remaining amount of turns Thor can move. Do not remove this line.
        cin >> remainingTurns; cin.ignore();

        // Write an action using cout. DON'T FORGET THE "<< endl"
        // To debug: cerr << "Debug messages..." << endl;


        // A single line providing the move to be made: N NE E SE S SW W or NW
        //cout << "SE" << endl;
        if(lightY-ty > 0)
        {
            cout << "S";
            ty++;
        }
        else if(lightY!=ty)
        {
            cout <<  "N";
            ty--;
        }
        if(lightX-tx > 0)
        {
            cout << "E" << endl;
            tx++;
        }
        else if(lightX!=tx)
        {
            cout <<  "W" << endl;
            tx--;
        }
        else
        {
            cout << endl;
        }
        
    }
}

ジャジャガッチ | C/C++ | 23:19 | comments(0) | trackbacks(0) |
1/1PAGES | |

05
--
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
--
>>
<<
--
PR
RECOMMEND
RECENT COMMENT
MOBILE
qrcode
OTHERS
Since 2013/09/17
LATEST ENTRY
CATEGORY
ARCHIVE
LINKS
PROFILE
SEARCH