Physics2DのCreateConvexScene
凸形状のPhysicsShapeの生成方法。
PS Suite SDKの話。
Physics2Dのサンプルとしてある、Physics2DSample。
その中の、
CreateConvexScene(基本的な凸包の剛体を生成するサンプルシーンです。)
について。
はじめに
方法は二つ
- ランダム頂点を作成し、そこから自動生成
- 自分で計算し頂点を作成して生成。(もちろんしっかり凸形状になること)
注意1
It is good to choose the center of convex equals to (0, 0) (& center of gravity) for stability of simulation.
凸形状の中心はシミュレーションの安定のために(0,0)である方が良いですよ。
(おそらくこういうこと)
注意2
頂点は最大で30点まで。0は球を表す特別なケースで最初の点のX座標に半径が記録されている
ランダムから自動計算
143行目から。短いので丸々コピー。
Vector2[] rand_point = new Vector2[20]; for (int i = 0; i < 20; i++) { rand_point[i] = new Vector2(rand_gen.Next(-1000, 1000), rand_gen.Next(-1000, 1000)) * 4.0f / 1000.0f; } sceneShapes[4] = PhysicsShape.CreateConvexHull(rand_point, 10);
使っているのは、PhysicsShape.CreateConvexHull
static PhysicsShape CreateConvexHull (Vector2[] vertList, int num)
ランダム頂点から凸包を生成します
vertList ランダムな頂点列 num 頂点数 戻り値 凸形状
ランダム頂点から凸形状になるように自動生成をしてくれるので、
出来上がったPhysicsShapeの
numVert(頂点数) != 引数のnum
となる。
System.Random(10)の結果では
引数のnum | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
numVert(頂点数) | 3 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 5 | 5 | 6 | 6 | 6 | 7 | 7 | 6 | 6 |
自分で計算して生成。
157行目から。
少し修正を加えてコピー。
Vector2[] user_point = new Vector2[5]; for (int i = 0; i < 5; i++) { user_point[i] = 4.0f * new Vector2((float)Math.Cos(72.0f*(float)i/180.0f*PhysicsUtility.Pi), (float)Math.Sin(72.0f*(float)i/180.0f*PhysicsUtility.Pi)); } Vector2 center_point = new Vector2(0, 0); for(int i=0; i<5; i++) { center_point += user_point[i]; } center_point *= (1.0f/5.0f); //作った頂点の中心を計算。 for(int i=0; i<5; i++) { user_point[i] -= center_point; // ここで中心が(0,0)になるように修正 } sceneShapes[5] = new PhysicsShape(user_point, 5);
使っているのは、PhysicsShape.PhysicsShape
PhysicsShape (Vector2[] pos, int num)
接続したライン形状生成
pos 凸形状の頂点リスト num 頂点リストの数