Revision | 694 (tree) |
---|---|
Time | 2011-02-12 20:09:37 |
Author | massa_senohito |
RigidBodyに統一しました
BodyKindに分けました
BodyKindをいいかんじに取得します:RigidBody88行目
BoxとCircleを区別できるようになりました
@@ -0,0 +1,484 @@ | ||
1 | +using System; | |
2 | +using System.Collections.Generic; | |
3 | +using System.Text; | |
4 | +using Box2DX.Collision; | |
5 | +using Box2DX.Common; | |
6 | +using Box2DX.Dynamics; | |
7 | + | |
8 | +namespace Nlgp1.B2DX | |
9 | +{ | |
10 | + public abstract class RigidBody | |
11 | + { | |
12 | + //todo いくつかの作業に分けて動作させる | |
13 | + //addShape(),setdef(fric,rest,dens),こうすることで後で属性を変更できる | |
14 | + public string BodyKind = ""; | |
15 | + | |
16 | + Body selfBody; | |
17 | + public Body SelfBody | |
18 | + { | |
19 | + get | |
20 | + { | |
21 | + return selfBody; | |
22 | + } | |
23 | + private set | |
24 | + { | |
25 | + selfBody = value; | |
26 | + } | |
27 | + } | |
28 | + float boxwidth; | |
29 | + public float Boxwidth | |
30 | + { | |
31 | + get { return boxwidth; } | |
32 | + internal set { boxwidth = value; } | |
33 | + } | |
34 | + float boxheight; | |
35 | + public float Boxheight | |
36 | + { | |
37 | + get { return boxheight; } | |
38 | + internal set { boxheight = value; } | |
39 | + } | |
40 | + public myUserData UserData; | |
41 | + MassData massdata = new MassData(); | |
42 | + /// <summary> | |
43 | + /// いまいち適当な数値が思い浮かばない | |
44 | + /// </summary> | |
45 | + protected const float inertia = 5; | |
46 | + | |
47 | + BodyDef Bodydef = new BodyDef(); | |
48 | + protected const float rest = 0.6f, dens = 0.1f, fric = 0.3f; | |
49 | + protected const float angularDamping = 0.3f, lineardamping = 0.5f; | |
50 | + Dictionary<ObjectDataKind, float> Objdata = new Dictionary<ObjectDataKind, float>(); | |
51 | + /// <summary> | |
52 | + /// ボックスの再生成を許可するかのフラグ | |
53 | + /// </summary> | |
54 | + bool alregene; | |
55 | + public bool Alregene | |
56 | + { | |
57 | + private get { return alregene; } | |
58 | + set { alregene = value; } | |
59 | + } | |
60 | + List<ShapeDef> plist = new List<ShapeDef>(); | |
61 | + List<Shape> shapelist = new List<Shape>(); | |
62 | + public List<Shape> Shapelist | |
63 | + { | |
64 | + get { return shapelist; } | |
65 | + private set { shapelist = value; } | |
66 | + } | |
67 | + /// <summary> | |
68 | + /// ポリゴンのリストにpolygondefを加える | |
69 | + /// </summary> | |
70 | + /// <param name="pa"></param> | |
71 | + public void Addtolist(params PolygonDef[] pa) | |
72 | + { | |
73 | + foreach (PolygonDef p in pa) | |
74 | + plist.Add(p); | |
75 | + } | |
76 | + | |
77 | + | |
78 | + #region 新コンストラクタ | |
79 | + //todo 新コンストラクタのクラス変数 | |
80 | + //public Box(Vec2 pos, float angle, World world, float boxwidth, float boxheight, bool noRotateFlag, Dictionary<UserDataKind, string> userdata,Dictionary<ObjectDataKind,float> data) | |
81 | + public RigidBody(Vec2 pos, float angle) | |
82 | + { | |
83 | + Bodydef = new Bodydef2(pos, angle, lineardamping, angularDamping); | |
84 | + } | |
85 | + public RigidBody(Vec2 pos, float angle, float lind, float angd) | |
86 | + { | |
87 | + Bodydef = new Bodydef2(pos, angle, lind, angd); | |
88 | + BodyKind += this.ToString().Split(".".ToCharArray())[2]; | |
89 | + } | |
90 | + public void SetAsBox(float boxwidth, float boxheight) | |
91 | + { | |
92 | + plist.Add(makeshape(boxwidth, boxheight)); | |
93 | + } | |
94 | + /// <summary> | |
95 | + /// Boxの形や係数を宣言する | |
96 | + /// </summary> | |
97 | + /// <param name="boxwidth">Boxの幅</param> | |
98 | + /// <param name="boxheight">Boxの高さ</param> | |
99 | + /// <param name="data">Boxの係数</param> | |
100 | + /// <param name="fil"></param> | |
101 | + /// <param name="isSensor">ぶつからなくても当たり判定をとるかどうか、trueだとすりぬけてもAdd等が発生します</param> | |
102 | + public void SetAsBox(float boxwidth, float boxheight, Polygondef2 data, bool isSensor) | |
103 | + { | |
104 | + Boxwidth = boxwidth; Boxheight = boxheight; | |
105 | + | |
106 | + #region | |
107 | + /* | |
108 | + Objdata[ObjectDataKind.Density] = dens; | |
109 | + Objdata[ObjectDataKind.Friction] = fric; | |
110 | + Objdata[ObjectDataKind.Restitution] = rest; | |
111 | + foreach (int i in Enum.GetValues(typeof(ObjectDataKind))) | |
112 | + { | |
113 | + if (data.ContainsKey((ObjectDataKind)i)) Objdata[(ObjectDataKind)i] = data[(ObjectDataKind)i]; | |
114 | + } | |
115 | + */ | |
116 | + #endregion | |
117 | + Polygondef2 p = data; | |
118 | + p.SetAsBox(boxwidth, boxheight); | |
119 | + #region | |
120 | + /* | |
121 | + p.Density = Objdata[ObjectDataKind.Density]; | |
122 | + p.Friction = Objdata[ObjectDataKind.Friction]; | |
123 | + p.Restitution = Objdata[ObjectDataKind.Restitution]; | |
124 | + */ | |
125 | + #endregion | |
126 | + p.IsSensor = isSensor; | |
127 | + UserData = data.User; | |
128 | + BodyKind += "Box"; | |
129 | + //UserDataに必要なものを追加する | |
130 | + //UserData.filter = Bodydef.UserData; | |
131 | + plist.Add(p); | |
132 | + } | |
133 | + public void SetAsCircle(CircleDef2 data, bool isSensor) | |
134 | + { | |
135 | + Boxwidth = data.Radius; Boxheight = data.Radius; | |
136 | + //todo (SetAsBoxも)IsSensorを統合するべきかどうか | |
137 | + data.IsSensor = isSensor; | |
138 | + UserData = data.User; | |
139 | + BodyKind += "Circle"; | |
140 | + plist.Add(data); | |
141 | + | |
142 | + } | |
143 | + /// <summary> | |
144 | + /// 旧コンストラクタの方 | |
145 | + /// </summary> | |
146 | + /// <param name="userdata"></param> | |
147 | + /// <returns></returns> | |
148 | + public FilterData SetFilter(Dictionary<UserDataKind, string> userdata) | |
149 | + { | |
150 | + FilterData fd = new FilterData(); | |
151 | + fd.CategoryBits = Convert.ToUInt16("111",2); | |
152 | + fd.MaskBits = Convert.ToUInt16("111",2); | |
153 | + if (userdata.ContainsKey(UserDataKind.GroupIndex)) fd.GroupIndex = Convert.ToInt16(userdata[UserDataKind.GroupIndex],2); | |
154 | + if (userdata.ContainsKey(UserDataKind.CategoryBits)) fd.CategoryBits = Convert.ToUInt16(userdata[UserDataKind.CategoryBits],2); | |
155 | + if (userdata.ContainsKey(UserDataKind.MaskBits)) fd.MaskBits = Convert.ToUInt16(userdata[UserDataKind.MaskBits],2); | |
156 | + return fd; | |
157 | + | |
158 | + } | |
159 | + | |
160 | + static PolygonDef makeshape(float boxwidth, float boxheight) | |
161 | + { | |
162 | + PolygonDef p = new PolygonDef(); | |
163 | + p.SetAsBox(boxwidth, boxheight); | |
164 | + return p; | |
165 | + | |
166 | + } | |
167 | + | |
168 | + | |
169 | + #endregion | |
170 | + /* | |
171 | + public void ApplyUserData(Dictionary<ObjectDataKind, float> data, FilterData fil) | |
172 | + { | |
173 | + if (data.ContainsKey(ObjectDataKind.Friction)) p.Friction = data[ObjectDataKind.Friction]; | |
174 | + if (data.ContainsKey(ObjectDataKind.Restitution)) p.Restitution = data[ObjectDataKind.Restitution]; | |
175 | + if (data.ContainsKey(ObjectDataKind.Density)) p.Density = data[ObjectDataKind.Density]; | |
176 | + p.Filter = fil; | |
177 | + } | |
178 | + */ | |
179 | + | |
180 | + /// <summary> | |
181 | + /// 最後にWorldを渡して指定されたポリゴンを作る | |
182 | + /// </summary> | |
183 | + /// <param name="w"></param> | |
184 | + public void MakeBody(World w) | |
185 | + { | |
186 | + SelfBody = w.CreateBody(Bodydef); | |
187 | + plist.ForEach(delegate(ShapeDef pd) | |
188 | + { | |
189 | + SelfBody.CreateShape(pd); | |
190 | + }); | |
191 | + if (!this.BodyKind.Equals("Static")) | |
192 | + { | |
193 | + SelfBody.SetMassFromShapes(); | |
194 | + } | |
195 | + SelfBody.SetUserData(UserData); | |
196 | + Shape s = SelfBody.GetShapeList(); | |
197 | + | |
198 | + while (s != null) | |
199 | + { | |
200 | + Shapelist.Add(s); | |
201 | + s=s.GetNext(); | |
202 | + } | |
203 | + | |
204 | + } | |
205 | + | |
206 | + /// <summary> | |
207 | + /// 回転するかしないかを定義する、この後必ずMakeBodyを通すこと | |
208 | + /// </summary> | |
209 | + public bool NoRotate | |
210 | + { | |
211 | + get | |
212 | + { | |
213 | + return Bodydef.FixedRotation; | |
214 | + } | |
215 | + set | |
216 | + { | |
217 | + Bodydef.FixedRotation = value; | |
218 | + } | |
219 | + } | |
220 | + #region 旧コンストラクタ処理 | |
221 | + /// <summary> | |
222 | + /// 力を加えると動く物体の定義 | |
223 | + /// </summary> | |
224 | + /// <param name="pos">場所</param> | |
225 | + /// <param name="angle">角度</param> | |
226 | + /// <param name="world">投入するWorld</param> | |
227 | + /// <param name="boxwidth">判定の広さ</param> | |
228 | + /// <param name="boxheight">判定の高さ</param> | |
229 | + /// <param name="noRotateFlag">非回転フラグ</param> | |
230 | + /// <param name="userdata">名前データと衝突判定</param> | |
231 | + public RigidBody(Vec2 pos, float angle, World world, float boxwidth, float boxheight, | |
232 | + bool noRotateFlag, Dictionary<UserDataKind, string> userdata, | |
233 | + Dictionary<ObjectDataKind, float> data, | |
234 | + bool allowregenerate | |
235 | + ) | |
236 | + { | |
237 | + alregene = allowregenerate; | |
238 | + | |
239 | + | |
240 | + //ボディの仮想的な初期状態を定義する | |
241 | + Bodydef.Angle = angle; | |
242 | + Bodydef.Position = pos; | |
243 | + Bodydef.FixedRotation = noRotateFlag; | |
244 | + //ボディの回転減衰率 | |
245 | + //デフォルトの状態を保存しておき、dataによって変更する | |
246 | + //todo 構造に不満があるんだけどどうしよ | |
247 | + // 新しい属性を追加するとここに追加>下のdefを調整 | |
248 | + Objdata[ObjectDataKind.Density] = dens; | |
249 | + Objdata[ObjectDataKind.Friction] = fric; | |
250 | + Objdata[ObjectDataKind.Restitution] = rest; | |
251 | + Objdata[ObjectDataKind.AngularDamping] = angularDamping; | |
252 | + Objdata[ObjectDataKind.LinearDamping] = lineardamping; | |
253 | + foreach (int i in Enum.GetValues(typeof(ObjectDataKind))) | |
254 | + { | |
255 | + if (data.ContainsKey((ObjectDataKind)i)) Objdata[(ObjectDataKind)i] = data[(ObjectDataKind)i]; | |
256 | + } | |
257 | + | |
258 | + NoRotate = noRotateFlag; | |
259 | + makepolygon(boxwidth, boxheight, world, userdata); | |
260 | + } | |
261 | + void makepolygon(float boxwidth, float boxheight, World world, Dictionary<UserDataKind, string> userdata) | |
262 | + { | |
263 | + //ポリゴンの形状を決定する | |
264 | + | |
265 | + //MassData mass = new MassData(); | |
266 | + //mass.Mass = 4; | |
267 | + Boxheight = boxheight; | |
268 | + Boxwidth = boxwidth; | |
269 | + #region ユーザーデータ定義 | |
270 | + FilterData fil = new FilterData(); | |
271 | + | |
272 | + UserData = new myUserData(); | |
273 | + if (userdata.ContainsKey(UserDataKind.Name)) UserData.name = userdata[UserDataKind.Name]; | |
274 | + if (userdata.ContainsKey(UserDataKind.Filter)) | |
275 | + { | |
276 | + | |
277 | + fil.CategoryBits = Convert.ToUInt16(userdata[UserDataKind.Filter],2); | |
278 | + fil.MaskBits = fil.CategoryBits; | |
279 | + | |
280 | + } | |
281 | + if (userdata.ContainsKey(UserDataKind.MaskBits)) | |
282 | + { | |
283 | + fil.MaskBits = Convert.ToUInt16(userdata[UserDataKind.MaskBits],2); | |
284 | + } | |
285 | + if (userdata.ContainsKey(UserDataKind.CategoryBits)) | |
286 | + { | |
287 | + fil.CategoryBits = Convert.ToUInt16(userdata[UserDataKind.CategoryBits],2); | |
288 | + } | |
289 | + if (userdata.ContainsKey(UserDataKind.GroupIndex)) | |
290 | + { | |
291 | + fil.GroupIndex = Convert.ToInt16(userdata[UserDataKind.GroupIndex],2); | |
292 | + } | |
293 | + UserData.filter = fil; | |
294 | + #endregion | |
295 | + //Userdataや減衰度を物質の定義に渡している | |
296 | + Bodydef.UserData = UserData; | |
297 | + Bodydef.LinearDamping = Objdata[ObjectDataKind.LinearDamping]; | |
298 | + Bodydef.AngularDamping = Objdata[ObjectDataKind.AngularDamping]; | |
299 | + | |
300 | + | |
301 | + PolygonDef p; | |
302 | + //p = def(rest, dens, fric, UserData.filter); | |
303 | + p = def(Objdata, fil); | |
304 | + p.SetAsBox(boxwidth, boxheight); | |
305 | + plist.Add(p); | |
306 | + | |
307 | + if (!alregene) MakeBody(world); | |
308 | + | |
309 | + } | |
310 | + | |
311 | + /// <summary> | |
312 | + /// ポリゴン定義 | |
313 | + /// </summary> | |
314 | + /// <param name="rest">反発</param> | |
315 | + /// <param name="dens">密度</param> | |
316 | + /// <param name="fric">摩擦</param> | |
317 | + /// <returns></returns> | |
318 | + static PolygonDef def(float rest, float dens, float fric, FilterData fil) | |
319 | + { | |
320 | + PolygonDef p = new PolygonDef(); | |
321 | + p.Restitution = rest; | |
322 | + p.Density = dens; | |
323 | + p.Friction = fric; | |
324 | + p.Filter = fil; | |
325 | + | |
326 | + return p; | |
327 | + } | |
328 | + static PolygonDef def(Dictionary<ObjectDataKind, float> objdata, FilterData fil) | |
329 | + { | |
330 | + PolygonDef p = new PolygonDef(); | |
331 | + p.Restitution = objdata[ObjectDataKind.Restitution]; | |
332 | + p.Density = objdata[ObjectDataKind.Density]; | |
333 | + p.Friction = objdata[ObjectDataKind.Friction]; | |
334 | + p.Filter = fil; | |
335 | + | |
336 | + return p; | |
337 | + } | |
338 | + #endregion | |
339 | + | |
340 | + #region Getプロパティ | |
341 | + | |
342 | + /// <summary> | |
343 | + /// 重心を返します | |
344 | + /// </summary> | |
345 | + public Vec2 Center | |
346 | + { | |
347 | + get | |
348 | + { | |
349 | + return selfBody.GetWorldCenter(); | |
350 | + } | |
351 | + } | |
352 | + /// <summary> | |
353 | + /// 座標を表します | |
354 | + /// </summary> | |
355 | + public Vec2 Position | |
356 | + { | |
357 | + get | |
358 | + { | |
359 | + return selfBody.GetPosition(); | |
360 | + } | |
361 | + set | |
362 | + { | |
363 | + selfBody.SetXForm(value, Angle); | |
364 | + } | |
365 | + } | |
366 | + /// <summary> | |
367 | + /// 角度を表します | |
368 | + /// </summary> | |
369 | + public float Angle | |
370 | + { | |
371 | + get | |
372 | + { | |
373 | + return selfBody.GetAngle(); | |
374 | + } | |
375 | + set | |
376 | + { | |
377 | + selfBody.SetXForm(Position, value); | |
378 | + } | |
379 | + } | |
380 | + /// <summary> | |
381 | + /// 加速度を表します | |
382 | + /// </summary> | |
383 | + public Vec2 LinearVelocity | |
384 | + { | |
385 | + get | |
386 | + { | |
387 | + return selfBody.GetLinearVelocity(); | |
388 | + } | |
389 | + set | |
390 | + { | |
391 | + selfBody.SetLinearVelocity(value); | |
392 | + } | |
393 | + } | |
394 | + /// <summary> | |
395 | + /// 質量を得ます | |
396 | + /// </summary> | |
397 | + public float Mass | |
398 | + { | |
399 | + get | |
400 | + { | |
401 | + return selfBody.GetMass(); | |
402 | + } | |
403 | + | |
404 | + } | |
405 | + /// <summary> | |
406 | + /// 慣性を得ます | |
407 | + /// </summary> | |
408 | + public float Inertia | |
409 | + { | |
410 | + get | |
411 | + { | |
412 | + return selfBody.GetInertia(); | |
413 | + } | |
414 | + } | |
415 | + #endregion | |
416 | + | |
417 | + //RayCastで得られた情報 | |
418 | + public Vec2 normal; | |
419 | + public float lambda; | |
420 | + public Colors color = Colors.None; | |
421 | + //イベント | |
422 | + event EventHandler<RaycastEvent> raycast = new EventHandler<RaycastEvent>(Rayhit); | |
423 | + | |
424 | + /// <summary> | |
425 | + /// toまでの位置に光を投げ、当たった物(最大5つ)の配列を返す | |
426 | + /// </summary> | |
427 | + /// <param name="from">自分の位置から見て離れた場所から光を出すこと、さもないと自分を検出する</param> | |
428 | + /// <param name="to">自分から見て光を当てる位置</param> | |
429 | + /// <returns></returns> | |
430 | + public Shape[] Raycast(Vec2 from,Vec2 to) | |
431 | + { | |
432 | + Segment s; | |
433 | + s.P1 = SelfBody.GetWorldPoint(from); | |
434 | + s.P2 = SelfBody.GetWorldVector(to)+s.P1; | |
435 | + if (color != Colors.None) Debug.Drawline(s, color); | |
436 | + XForm thisx = selfBody.GetXForm(); | |
437 | + World w = SelfBody.GetWorld(); | |
438 | + Shape[] shapes = new Shape[5]; | |
439 | + w.Raycast(s, shapes, 5, true, null); | |
440 | + if (shapes[0] != null) | |
441 | + { | |
442 | + EventHandler<RaycastEvent> temp = raycast; | |
443 | + temp(this,new RaycastEvent(shapes)); | |
444 | + } | |
445 | + /* | |
446 | + ユーザーデータがほしいのなら | |
447 | + foreach (Shape tem in shapes) tem.GetBody().GetUserData(); | |
448 | + */ | |
449 | + return shapes; | |
450 | + | |
451 | + } | |
452 | + | |
453 | + static void Rayhit(object sender, RaycastEvent ray) | |
454 | + { | |
455 | + foreach (Shape s in ray.Rayhit) | |
456 | + { | |
457 | + if (s != null) Debug.PrintContact(s.GetBody().GetPosition()); | |
458 | + } | |
459 | + } | |
460 | + | |
461 | + /// <summary> | |
462 | + /// フィルタデータを変更する | |
463 | + /// </summary> | |
464 | + /// <param name="f">フィルタデータ</param> | |
465 | + /// <param name="idx">Shapelistのインデックス</param> | |
466 | + public void SetFilter(FilterData f, int idx) | |
467 | + { | |
468 | + Shape s = Shapelist[idx]; | |
469 | + Body b = s.GetBody(); | |
470 | + b.GetShapeList().FilterData = f; | |
471 | + UserData.filter = f; | |
472 | + | |
473 | + SelfBody.SetUserData(UserData); | |
474 | + } | |
475 | + | |
476 | + | |
477 | + | |
478 | + } | |
479 | + class RaycastEvent : EventArgs | |
480 | + { | |
481 | + public Shape[] Rayhit; | |
482 | + public RaycastEvent(Shape[] rayhit) { Rayhit = rayhit; } | |
483 | + } | |
484 | +} |
@@ -57,7 +57,7 @@ | ||
57 | 57 | bool keyflag = false; |
58 | 58 | //サイズは半径で設定する |
59 | 59 | //todo IBoxインターフェイスからIBox.MakeShapeとか呼びだして、構成すれば処理を分けられるはず |
60 | - DynamicBox d = new DynamicBox(new Vec2(42, 170), 90 * pi / 180, world, 16, 16, false, | |
60 | + DynamicRigid d = new DynamicRigid(new Vec2(42, 170), 90 * pi / 180, world, 16, 16, false, | |
61 | 61 | new Dictionary<UserDataKind, string>() |
62 | 62 | { |
63 | 63 | { UserDataKind.Name, "player" }, |
@@ -67,9 +67,9 @@ | ||
67 | 67 | }, |
68 | 68 | new Dictionary<ObjectDataKind,float>(), |
69 | 69 | true); |
70 | - StaticBox s = new StaticBox(new Vec2(240, 202), 0, world, 16, 16, true, new Dictionary<UserDataKind, string>() { { UserDataKind.Name, "enemy" }, { UserDataKind.Filter, "111" } } | |
70 | + StaticRigid s = new StaticRigid(new Vec2(240, 202), 0, world, 16, 16, true, new Dictionary<UserDataKind, string>() { { UserDataKind.Name, "enemy" }, { UserDataKind.Filter, "111" } } | |
71 | 71 | ,false); |
72 | - StaticBox wall = new StaticBox(new Vec2(250, 16), 0, world, 250, 16, true, | |
72 | + StaticRigid wall = new StaticRigid(new Vec2(250, 16), 0, world, 250, 16, true, | |
73 | 73 | new Dictionary<UserDataKind, string>() |
74 | 74 | { |
75 | 75 | { UserDataKind.Name, "wall" }, |
@@ -77,7 +77,7 @@ | ||
77 | 77 | {UserDataKind.GroupIndex,"1"} |
78 | 78 | } |
79 | 79 | ,false); |
80 | - StaticBox floor = new StaticBox(new Vec2(500, 168), 0, world, 16, 150, true, | |
80 | + StaticRigid floor = new StaticRigid(new Vec2(500, 168), 0, world, 16, 150, true, | |
81 | 81 | new Dictionary<UserDataKind, string>() |
82 | 82 | { |
83 | 83 | { UserDataKind.Name, "floor" }, |
@@ -103,7 +103,7 @@ | ||
103 | 103 | FilterData f=new FilterData(); |
104 | 104 | f.CategoryBits = 1; |
105 | 105 | f.GroupIndex = 1; f.MaskBits = 1; |
106 | - DynamicBox d2 = new DynamicBox(new Vec2(50, 60), 0 * pi / 180, lineardamping, angularDamping); | |
106 | + DynamicRigid d2 = new DynamicRigid(new Vec2(50, 60), 0 * pi / 180, lineardamping, angularDamping); | |
107 | 107 | d2.SetAsBox(16, 16, new Polygondef2(fric2, rest2, dens2, new myUserData("d2", 1, f, 2)), false); |
108 | 108 | Polygondef2 d2p = new Polygondef2(friction, restitution, density, new myUserData("d2p", 3, f, 2)); |
109 | 109 | d2p.SetAsBox(24, 12, new Vec2(-20, 1), 0); |
@@ -113,7 +113,7 @@ | ||
113 | 113 | d2.NoRotate = false; |
114 | 114 | d2.MakeBody(world); |
115 | 115 | |
116 | - DynamicBox c1 = new DynamicBox(new Vec2(300, 90), 0, 0, 0); | |
116 | + DynamicRigid c1 = new DynamicRigid(new Vec2(300, 90), 0, 0, 0); | |
117 | 117 | c1.SetAsCircle(new CircleDef2(new Vec2(), 20, 0, 0, 1, new myUserData()), false); |
118 | 118 | c1.MakeBody(world); |
119 | 119 |
@@ -0,0 +1,149 @@ | ||
1 | +using System; | |
2 | +using System.Collections.Generic; | |
3 | +using System.Text; | |
4 | +using Box2DX.Collision; | |
5 | +using Box2DX.Common; | |
6 | +using Box2DX.Dynamics; | |
7 | + | |
8 | +namespace Nlgp1.B2DX | |
9 | +{ | |
10 | + public class DynamicRigid:RigidBody | |
11 | + { | |
12 | + public static new string BoxKind = "Dynamic"; | |
13 | + | |
14 | + /// <summary> | |
15 | + /// 力を加えると動く物体の定義 | |
16 | + /// </summary> | |
17 | + /// <param name="pos">場所</param> | |
18 | + /// <param name="angle">角度</param> | |
19 | + /// <param name="world">投入するWorld</param> | |
20 | + /// <param name="boxwidth">判定の広さ</param> | |
21 | + /// <param name="boxheight">判定の高さ</param> | |
22 | + /// <param name="noRotateFlag">非回転フラグ</param> | |
23 | + /// <param name="userdata">名前データと衝突判定</param> | |
24 | + public DynamicRigid(Vec2 pos,float angle,World world,float boxwidth,float boxheight,bool noRotateFlag,Dictionary<UserDataKind,string> userdata): | |
25 | + this(pos, angle, world, boxwidth, boxheight, noRotateFlag, userdata, new Dictionary<ObjectDataKind,float>(),false) | |
26 | + { | |
27 | + | |
28 | + } | |
29 | + | |
30 | + /// <summary> | |
31 | + /// 力を加えると動く物体の定義 | |
32 | + /// </summary> | |
33 | + /// <param name="pos">場所</param> | |
34 | + /// <param name="angle">角度</param> | |
35 | + /// <param name="world">投入するWorld</param> | |
36 | + /// <param name="boxwidth">判定の広さ</param> | |
37 | + /// <param name="boxheight">判定の高さ</param> | |
38 | + /// <param name="noRotateFlag">非回転フラグ</param> | |
39 | + /// <param name="userdata">名前データと衝突判定</param> | |
40 | + /// <param name="data">密度や摩擦の指定</param> | |
41 | + /// <param name="allowregenerate">ボックスの再生成を許可するかのフラグ</param> | |
42 | + public DynamicRigid(Vec2 pos, float angle, World world, float boxwidth, float boxheight, | |
43 | + bool noRotateFlag, Dictionary<UserDataKind, string> | |
44 | + userdata,Dictionary<ObjectDataKind,float> data, | |
45 | + bool allowregenerate): | |
46 | + base(pos, angle, world, boxwidth, boxheight, noRotateFlag, userdata, data,allowregenerate) | |
47 | + { | |
48 | + } | |
49 | + public DynamicRigid(Vec2 pos, float angle, World world, float boxwidth, float boxheight, | |
50 | + bool noRotateFlag, Dictionary<UserDataKind, string> | |
51 | + userdata, Dictionary<ObjectDataKind, float> data) : | |
52 | + base(pos, angle, world, boxwidth, boxheight, noRotateFlag, userdata, data, false) | |
53 | + { | |
54 | + } | |
55 | + public DynamicRigid(Vec2 pos, float angle,float lind,float angd) : base(pos, angle,lind,angd) { } | |
56 | + | |
57 | + | |
58 | + public void ApplyRelativeImpulse(float power, float angle) | |
59 | + { | |
60 | + SelfBody.SetLinearVelocity(culcForce(new Vec2(power,power),angle)); | |
61 | + } | |
62 | + public void ApplyRelativeImpulse(Vec2 power, float angle) | |
63 | + { | |
64 | + SelfBody.SetLinearVelocity(culcForce(power, angle)); | |
65 | + } | |
66 | + | |
67 | + public void ApplyRelativeForce(float power, float angle) | |
68 | + { | |
69 | + SelfBody.ApplyForce(culcForce(new Vec2(power, power), angle), Center); | |
70 | + } | |
71 | + public void ApplyRelativeForce(Vec2 power, float angle) | |
72 | + { | |
73 | + SelfBody.ApplyForce(culcForce(power, angle), Center); | |
74 | + } | |
75 | + /// <summary> | |
76 | + /// 力を加えます、作用点指定なしで重心へと力がかかります | |
77 | + /// </summary> | |
78 | + /// <param name="power"></param> | |
79 | + /// <param name="center"></param> | |
80 | + public void ApplyForce(Vec2 power, Vec2 center) | |
81 | + { | |
82 | + SelfBody.ApplyForce(power, center); | |
83 | + } | |
84 | + public void ApplyForce(Vec2 power) | |
85 | + { | |
86 | + SelfBody.ApplyForce(power, SelfBody.GetWorldCenter()); | |
87 | + } | |
88 | + /// <summary> | |
89 | + /// 加速度を加えます、作用点指定なしで重心へと力がかかります | |
90 | + /// </summary> | |
91 | + /// <param name="power"></param> | |
92 | + /// <param name="center"></param> | |
93 | + public void ApplyImpulse(Vec2 power, Vec2 center) | |
94 | + { | |
95 | + SelfBody.ApplyImpulse(power, center); | |
96 | + } | |
97 | + public void ApplyImpulse(Vec2 power) | |
98 | + { | |
99 | + SelfBody.ApplyImpulse(power, SelfBody.GetWorldCenter()); | |
100 | + } | |
101 | + | |
102 | + /// <summary> | |
103 | + /// 物体を回転させます、正で時計回りに回転させます | |
104 | + /// </summary> | |
105 | + /// <param name="angulervelocity">回転速度</param> | |
106 | + public void ApplyTorque(float angulervelocity) | |
107 | + { | |
108 | + SelfBody.ApplyTorque(angulervelocity); | |
109 | + } | |
110 | + | |
111 | + /// <summary> | |
112 | + /// 重さ、慣性、重心を設定します | |
113 | + /// </summary> | |
114 | + /// <param name="center">重心の位置(ワールド座標)</param> | |
115 | + /// <param name="inertia">慣性</param> | |
116 | + /// <param name="mass">重さ(大きければ大きいほど力がかかりにくくなる)</param> | |
117 | + public void SetMassData(Vec2 center,float inertia,float mass) | |
118 | + { | |
119 | + MassData m; | |
120 | + m.Center = center; | |
121 | + m.I = inertia; | |
122 | + m.Mass = mass; | |
123 | + SelfBody.SetMass(m); | |
124 | + } | |
125 | + public myUserData GetUserData() | |
126 | + { | |
127 | + return UserData; | |
128 | + } | |
129 | + public void SetUserData(myUserData value) | |
130 | + { | |
131 | + UserData = value; | |
132 | + } | |
133 | + | |
134 | + | |
135 | + | |
136 | + Vec2 culcForce(Vec2 power, float angle) | |
137 | + { | |
138 | + | |
139 | + float dirx, diry; | |
140 | + dirx = MathF.Cos(Angle + angle * MathF.PI / 180) * power.X; | |
141 | + diry = MathF.Sin(Angle + angle * MathF.PI / 180) * power.Y; | |
142 | + return new Vec2(dirx, diry); | |
143 | + | |
144 | + } | |
145 | + | |
146 | + | |
147 | + } | |
148 | +} | |
149 | + |
@@ -0,0 +1,97 @@ | ||
1 | +using System; | |
2 | +using System.Collections.Generic; | |
3 | +using System.Text; | |
4 | +using Box2DX.Collision; | |
5 | +using Box2DX.Common; | |
6 | +using Box2DX.Dynamics; | |
7 | + | |
8 | +namespace Nlgp1.B2DX | |
9 | +{ | |
10 | + public class StaticRigid:RigidBody | |
11 | + { | |
12 | + | |
13 | + | |
14 | + //float rest = 0.6f, dens = 0, fric = 0.3f; | |
15 | + | |
16 | + public StaticRigid(Vec2 pos, float angle, World world, | |
17 | + float boxwidth, float boxheight, bool noRotateFlag, | |
18 | + Dictionary<UserDataKind, string> userdata, | |
19 | + bool allowregenerate) | |
20 | + : base(pos, angle, world, boxwidth, boxheight, noRotateFlag, userdata, | |
21 | + new Dictionary<ObjectDataKind, float> {{ObjectDataKind.Density,0 }},allowregenerate) | |
22 | + { | |
23 | + //ボディの仮想的な初期状態を定義する | |
24 | + //Bodydef.Angle = angle; | |
25 | + //Bodydef.Position = pos; | |
26 | + //Bodydef.FixedRotation = noRotateFlag; | |
27 | + //ボディの回転減衰率 | |
28 | + //Bodydef.AngularDamping = 5; | |
29 | + | |
30 | + //NoRotateFlag = noRotateFlag; | |
31 | + //makepolygon(boxwidth, boxheight, world, userdata); | |
32 | + | |
33 | + | |
34 | + } | |
35 | + public StaticRigid(Vec2 pos, float angle, World world, | |
36 | + float boxwidth, float boxheight, bool noRotateFlag, | |
37 | + Dictionary<UserDataKind, string> userdata): | |
38 | + base(pos, angle, world, boxwidth, boxheight, noRotateFlag, userdata, | |
39 | + new Dictionary<ObjectDataKind, float> { { ObjectDataKind.Density, 0 } }, false) | |
40 | + { | |
41 | + | |
42 | + } | |
43 | + /// <summary> | |
44 | + /// 動かないBoxです | |
45 | + /// </summary> | |
46 | + /// <param name="pos">位置</param> | |
47 | + /// <param name="angle">角度</param> | |
48 | + /// <param name="lind">運動しないので0で</param> | |
49 | + /// <param name="angd">角速度なんかないので0で</param> | |
50 | + public StaticRigid(Vec2 pos, float angle, float lind, float angd) : base(pos, angle, lind, angd) { } | |
51 | + /* | |
52 | + //特に必要ない | |
53 | + void makepolygon(float boxwidth, float boxheight, World world, Dictionary<UserDataKind, string> userdata) | |
54 | + { | |
55 | + //ポリゴンの形状を決定する | |
56 | + Boxheight = boxheight; | |
57 | + Boxwidth = boxwidth; | |
58 | + | |
59 | + //if (userdata.ContainsKey(UserDataKind.Name)) UserData.name = userdata[UserDataKind.Name]; | |
60 | + if (userdata.ContainsKey(UserDataKind.Filter)) | |
61 | + { | |
62 | + FilterData fil = new FilterData(); | |
63 | + fil.CategoryBits = Convert.ToUInt16(userdata[UserDataKind.Filter]); | |
64 | + fil.MaskBits = fil.CategoryBits; | |
65 | + UserData.filter = fil; | |
66 | + } | |
67 | + Bodydef.UserData = UserData; | |
68 | + | |
69 | + | |
70 | + SelfBody = world.CreateBody(Bodydef); | |
71 | + PolygonDef p = def(rest, dens, fric, UserData.filter); | |
72 | + p.SetAsBox(boxwidth, boxheight); | |
73 | + SelfBody.CreateShape(p); | |
74 | + | |
75 | + //SelfBody.SetMassFromShapes(); | |
76 | + } | |
77 | + */ | |
78 | + /// <summary> | |
79 | + /// ポリゴン定義 | |
80 | + /// </summary> | |
81 | + /// <param name="rest">反発</param> | |
82 | + /// <param name="dens">密度</param> | |
83 | + /// <param name="fric">摩擦</param> | |
84 | + /// <returns></returns> | |
85 | + static PolygonDef def(float rest, float dens, float fric,FilterData fil) | |
86 | + { | |
87 | + PolygonDef p = new PolygonDef(); | |
88 | + p.Restitution = rest; | |
89 | + p.Density = dens; | |
90 | + p.Friction = fric; | |
91 | + p.Filter = fil; | |
92 | + return p; | |
93 | + } | |
94 | + | |
95 | + | |
96 | + } | |
97 | +} |