Revision | 614 (tree) |
---|---|
Time | 2011-01-08 12:05:09 |
Author | massa_senohito |
修正
@@ -0,0 +1,49 @@ | ||
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 DxgameForBox2d | |
9 | +{ | |
10 | + class ContactEvent:ContactListener | |
11 | + { | |
12 | + public override void Persist(ContactPoint point) | |
13 | + { | |
14 | + contactRaise(point); | |
15 | + Debug.PrintContact(point.Position); | |
16 | + } | |
17 | + | |
18 | + public override void Add(ContactPoint point) | |
19 | + { | |
20 | + | |
21 | + contactRaise(point); | |
22 | + Debug.PrintContact(point.Position); | |
23 | + } | |
24 | + public override void Result(ContactResult point) | |
25 | + { | |
26 | + base.Result(point); | |
27 | + } | |
28 | + | |
29 | + void contactRaise(ContactPoint p) | |
30 | + { | |
31 | + /*todo GetBodyを参照しないといけない理由 | |
32 | + */ | |
33 | + myUserData u = p.Shape1.GetBody().GetUserData() as myUserData; | |
34 | + myUserData u2 = p.Shape2.GetBody().GetUserData() as myUserData; | |
35 | + //何故かはわからないが、shape1は必ずあたった方になる | |
36 | + //あてられた方のデータをUserDataに記憶している | |
37 | + u.preCollide = p.Shape2; | |
38 | + p.Shape1.GetBody().SetUserData(u); | |
39 | + | |
40 | + if (u == null || u2 == null) return; | |
41 | + | |
42 | + if (u.name == "player") | |
43 | + { | |
44 | + Debug.DebugHit(u.name + " hits " + u2.name); | |
45 | + | |
46 | + } | |
47 | + } | |
48 | + } | |
49 | +} |
@@ -0,0 +1,173 @@ | ||
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 | + | |
9 | +namespace DxgameForBox2d | |
10 | +{ | |
11 | + class DinamicBox | |
12 | + { | |
13 | + double pi = System.Math.PI; | |
14 | + public Body SelfBody; | |
15 | + public BodyDef Bodydef=new BodyDef(); | |
16 | + public float Boxwidth; | |
17 | + public float Boxheight; | |
18 | + public bool NoRotateFlag; | |
19 | + public myUserData UserData=new myUserData(); | |
20 | + float rest = 0.6f, dens = 0.1f, fric = 0.3f; | |
21 | + float angularDamping = 5, lineardamping = 0; | |
22 | + Dictionary<ObjectDataKind, float> Objdata=new Dictionary<ObjectDataKind,float>(); | |
23 | + | |
24 | + /// <summary> | |
25 | + /// 力を加えると動く物体の定義 | |
26 | + /// </summary> | |
27 | + /// <param name="pos">場所</param> | |
28 | + /// <param name="angle">角度</param> | |
29 | + /// <param name="world">投入するWorld</param> | |
30 | + /// <param name="boxwidth">判定の広さ</param> | |
31 | + /// <param name="boxheight">判定の高さ</param> | |
32 | + /// <param name="noRotateFlag">非回転フラグ</param> | |
33 | + /// <param name="userdata">名前データと衝突判定</param> | |
34 | + public DinamicBox(Vec2 pos,float angle,World world,float boxwidth,float boxheight,bool noRotateFlag,Dictionary<UserDataKind,string> userdata): | |
35 | + this(pos, angle, world, boxwidth, boxheight, noRotateFlag, userdata, new Dictionary<ObjectDataKind,float>()) | |
36 | + { | |
37 | + | |
38 | + } | |
39 | + | |
40 | + /// <summary> | |
41 | + /// 力を加えると動く物体の定義 | |
42 | + /// </summary> | |
43 | + /// <param name="pos">場所</param> | |
44 | + /// <param name="angle">角度</param> | |
45 | + /// <param name="world">投入するWorld</param> | |
46 | + /// <param name="boxwidth">判定の広さ</param> | |
47 | + /// <param name="boxheight">判定の高さ</param> | |
48 | + /// <param name="noRotateFlag">非回転フラグ</param> | |
49 | + /// <param name="userdata">名前データと衝突判定</param> | |
50 | + public DinamicBox(Vec2 pos, float angle, World world, float boxwidth, float boxheight, bool noRotateFlag, Dictionary<UserDataKind, string> userdata,Dictionary<ObjectDataKind,float> data) | |
51 | + { | |
52 | + //ボディの仮想的な初期状態を定義する | |
53 | + Bodydef.Angle = angle; | |
54 | + Bodydef.Position = pos; | |
55 | + Bodydef.FixedRotation = noRotateFlag; | |
56 | + //ボディの回転減衰率 | |
57 | + Bodydef.AngularDamping = angularDamping; | |
58 | + //デフォルトの状態を保存しておき、dataによって変更する | |
59 | + //todo 構造に不満があるんだけどどうしよ | |
60 | + // 新しい属性を追加するとここに追加>下のdefを調整 | |
61 | + Objdata[ObjectDataKind.Density] = dens; | |
62 | + Objdata[ObjectDataKind.Friction]=fric; | |
63 | + Objdata[ObjectDataKind.Restitution]=rest; | |
64 | + Objdata[ObjectDataKind.AngularDamping]=angularDamping; | |
65 | + Objdata[ObjectDataKind.LinearDamping]=lineardamping; | |
66 | + //dens,fric,restを内包したstructをわたす? | |
67 | + | |
68 | + //if (data.ContainsKey(ObjectData.Density)) st[ObjectData.Density] = data[ObjectData.Density]; | |
69 | + foreach (int i in Enum.GetValues(typeof(ObjectDataKind))) | |
70 | + { | |
71 | + if (data.ContainsKey((ObjectDataKind)i)) Objdata[(ObjectDataKind)i] = data[(ObjectDataKind)i]; | |
72 | + } | |
73 | + | |
74 | + //if (data.ContainsKey(ObjectData.Density)) dens = data[ObjectData.Density]; | |
75 | + //if (data.ContainsKey(ObjectData.Friction)) fric = data[ObjectData.Friction]; | |
76 | + //if (data.ContainsKey(ObjectData.Restitution)) rest = data[ObjectData.Restitution]; | |
77 | + | |
78 | + NoRotateFlag = noRotateFlag; | |
79 | + makepolygon(boxwidth, boxheight, world, userdata); | |
80 | + } | |
81 | + void makepolygon(float boxwidth, float boxheight,World world, Dictionary<UserDataKind, string> userdata) | |
82 | + { | |
83 | + //ポリゴンの形状を決定する | |
84 | + | |
85 | + //MassData mass = new MassData(); | |
86 | + //mass.Mass = 4; | |
87 | + Boxheight = boxheight; | |
88 | + Boxwidth = boxwidth; | |
89 | + FilterData fil=new FilterData(); | |
90 | + if (userdata.ContainsKey(UserDataKind.Name)) UserData.name = userdata[UserDataKind.Name]; | |
91 | + if (userdata.ContainsKey(UserDataKind.Filter)) | |
92 | + { | |
93 | + | |
94 | + fil.CategoryBits = Convert.ToUInt16(userdata[UserDataKind.Filter]); | |
95 | + fil.MaskBits=fil.CategoryBits; | |
96 | + | |
97 | + } | |
98 | + if (userdata.ContainsKey(UserDataKind.MaskBits)) | |
99 | + { | |
100 | + fil.MaskBits = Convert.ToUInt16(userdata[UserDataKind.MaskBits]); | |
101 | + } | |
102 | + if (userdata.ContainsKey(UserDataKind.CategoryBits)) | |
103 | + { | |
104 | + fil.CategoryBits = Convert.ToUInt16(userdata[UserDataKind.CategoryBits]); | |
105 | + } | |
106 | + if (userdata.ContainsKey(UserDataKind.GroupIndex)) | |
107 | + { | |
108 | + fil.GroupIndex = Convert.ToInt16(userdata[UserDataKind.GroupIndex]); | |
109 | + } | |
110 | + UserData.filter = fil; | |
111 | + Bodydef.UserData = UserData; | |
112 | + PolygonDef p; | |
113 | + //p = def(rest, dens, fric, UserData.filter); | |
114 | + p=def(Objdata,fil); | |
115 | + p.SetAsBox(boxwidth, boxheight); | |
116 | + this.SelfBody = world.CreateBody(Bodydef); | |
117 | + this.SelfBody.CreateShape(p); | |
118 | + | |
119 | + | |
120 | + if (!this.GetType().ToString().Contains("StaticBox")) | |
121 | + { | |
122 | + SelfBody.SetMassFromShapes(); | |
123 | + } | |
124 | + | |
125 | + | |
126 | + } | |
127 | + | |
128 | + /// <summary> | |
129 | + /// ポリゴン定義 | |
130 | + /// </summary> | |
131 | + /// <param name="rest">反発</param> | |
132 | + /// <param name="dens">密度</param> | |
133 | + /// <param name="fric">摩擦</param> | |
134 | + /// <returns></returns> | |
135 | + PolygonDef def(float rest, float dens, float fric,FilterData fil) | |
136 | + { | |
137 | + PolygonDef p = new PolygonDef(); | |
138 | + p.Restitution = rest; | |
139 | + p.Density = dens; | |
140 | + p.Friction = fric; | |
141 | + p.Filter = fil; | |
142 | + | |
143 | + return p; | |
144 | + } | |
145 | + PolygonDef def(Dictionary<ObjectDataKind,float> objdata, FilterData fil) | |
146 | + { | |
147 | + PolygonDef p = new PolygonDef(); | |
148 | + p.Restitution = objdata[ObjectDataKind.Restitution]; | |
149 | + p.Density = objdata[ObjectDataKind.Density]; | |
150 | + p.Friction = objdata[ObjectDataKind.Friction]; | |
151 | + p.Filter = fil; | |
152 | + | |
153 | + return p; | |
154 | + } | |
155 | + | |
156 | + public void ApplyRelativeImpulse(float power, float angle) | |
157 | + { | |
158 | + double dirx, diry; | |
159 | + dirx = System.Math.Cos(SelfBody.GetAngle() + angle * pi / 180) * power; | |
160 | + dirx = System.Math.Round(dirx); | |
161 | + diry = System.Math.Sin(SelfBody.GetAngle() + angle * pi / 180) * power; | |
162 | + SelfBody.SetLinearVelocity(new Vec2((float)dirx, (float)diry)); | |
163 | + } | |
164 | + public void ApplyRelativeImpulse(Vec2 power, float angle) | |
165 | + { | |
166 | + double dirx, diry; | |
167 | + dirx = System.Math.Cos(SelfBody.GetAngle() + angle * pi / 180) * power.X; | |
168 | + dirx = System.Math.Round(dirx); | |
169 | + diry = System.Math.Sin(SelfBody.GetAngle() + angle * pi / 180) * power.Y; | |
170 | + SelfBody.SetLinearVelocity(new Vec2((float)dirx, (float)diry)); | |
171 | + } | |
172 | + } | |
173 | +} |
@@ -0,0 +1,121 @@ | ||
1 | +using System; | |
2 | +using System.Collections.Generic; | |
3 | +using System.Text; | |
4 | +using Importing.DxLib; | |
5 | +using Box2DX.Collision; | |
6 | +using Box2DX.Common; | |
7 | +using Box2DX.Dynamics; | |
8 | +using System.Collections; | |
9 | + | |
10 | +namespace DxgameForBox2d | |
11 | +{ | |
12 | + class Gamemain | |
13 | + { | |
14 | + | |
15 | + | |
16 | + static void Main() | |
17 | + { | |
18 | + try | |
19 | + { | |
20 | + Window.SetWindowSize(512, 384); | |
21 | + Window.ChangeWindowMode(true); | |
22 | + Window.SetAlwaysRunFlag(false); | |
23 | + Window.DxLib_Init(); | |
24 | + | |
25 | + //画像読み込み | |
26 | + Graph mech; | |
27 | + double pi = System.Math.PI; | |
28 | + mech = Window.LoadGraph("mech.png"); | |
29 | + | |
30 | + AABB worldBox = new AABB(); | |
31 | + //左上座標の定義 | |
32 | + worldBox.LowerBound = new Vec2(0, 0); | |
33 | + //右下座標の定義 | |
34 | + worldBox.UpperBound = new Vec2(500, 300); | |
35 | + //重力の定義 | |
36 | + Vec2 grav = new Vec2(0, 0); | |
37 | + | |
38 | + World world = new World(worldBox, grav, false); | |
39 | + DebugDraw dd = new Debug(); | |
40 | + dd.AppendFlags(DebugDraw.DrawFlags.Shape); | |
41 | + world.SetDebugDraw(dd); | |
42 | + | |
43 | + world.SetContactListener(new ContactEvent()); | |
44 | + | |
45 | + | |
46 | + Vec2[] gravAr = new Vec2[] | |
47 | + { | |
48 | + new Vec2(0, -18), | |
49 | + new Vec2(18, 0), | |
50 | + new Vec2(-18,0), | |
51 | + new Vec2(0,18) | |
52 | + }; | |
53 | + int gr = 1; | |
54 | + bool keyflag = false; | |
55 | + //サイズは半径で設定する | |
56 | + //todo IBoxインターフェイスからIBox.MakeShapeとか呼びだして、構成すれば処理を分けられるはず | |
57 | + DinamicBox d = new DinamicBox(new Vec2(42, 170), (float)(45 * pi / 180), world, 16, 16, false, | |
58 | + new Dictionary<UserDataKind, string>() | |
59 | + { | |
60 | + { UserDataKind.Name, "player" }, | |
61 | + { UserDataKind.CategoryBits,"110" }, | |
62 | + {UserDataKind.MaskBits,"010"}, | |
63 | + {UserDataKind.GroupIndex,"2"} | |
64 | + }); | |
65 | + StaticBox s = new StaticBox(new Vec2(240, 202), 0, world, 16, 16, true, new Dictionary<UserDataKind, string>() { { UserDataKind.Name, "enemy" }, { UserDataKind.Filter, "111" } }); | |
66 | + StaticBox wall = new StaticBox(new Vec2(250, 16), 0, world, 250, 16, true, | |
67 | + new Dictionary<UserDataKind, string>() | |
68 | + { | |
69 | + { UserDataKind.Name, "wall" }, | |
70 | + { UserDataKind.Filter, "0010" }, | |
71 | + {UserDataKind.GroupIndex,"3"} | |
72 | + }); | |
73 | + StaticBox floor = new StaticBox(new Vec2(500, 168), 0, world, 16, 150, true, | |
74 | + new Dictionary<UserDataKind, string>() | |
75 | + { | |
76 | + { UserDataKind.Name, "floor" }, | |
77 | + { UserDataKind.Filter, "101" } | |
78 | + }); | |
79 | + | |
80 | + Window.SetDrawScreen(Window.DX_SCREEN_BACK); | |
81 | + | |
82 | + | |
83 | + | |
84 | + while (Window.ProcessMessage()) | |
85 | + { | |
86 | + if (Window.CheckHitKey(KEY_INPUT.RETURN)&&!keyflag) | |
87 | + { | |
88 | + //瞬間的にある方向に力を加える(270度が設定されているのでオブジェクトから見て真上へ) | |
89 | + d.ApplyRelativeImpulse(60, 270); | |
90 | + } | |
91 | + | |
92 | + if (!Window.CheckHitKey(KEY_INPUT.RETURN)) | |
93 | + { | |
94 | + keyflag = false; | |
95 | + } | |
96 | + | |
97 | + if (Window.CheckHitKey(KEY_INPUT.LEFT)) | |
98 | + { | |
99 | + d.SelfBody.ApplyImpulse(new Vec2(-3 * d.SelfBody.GetMass(), 0), new Vec2(0, 0)); | |
100 | + //d.SelfBody.ApplyTorque(20*d.SelfBody.GetMass()); | |
101 | + } | |
102 | + Window.ClearDrawScreen(); | |
103 | + Window.ProcessActKeyInput(); | |
104 | + Window.DrawString(2, 7, world.Gravity.X.ToString(), new COLOR_U8(0xffffff)); | |
105 | + Window.DrawString(2, 27, world.Gravity.Y.ToString(), new COLOR_U8(0xffffff)); | |
106 | + | |
107 | + Window.DrawRotaGraph((int)d.SelfBody.GetPosition().X, (int)d.SelfBody.GetPosition().Y, 1, d.SelfBody.GetAngle(), mech, true,true); | |
108 | + Window.DrawRotaGraph((int)s.SelfBody.GetPosition().X, (int)s.SelfBody.GetPosition().Y, 1, 0, mech, false); | |
109 | + | |
110 | + world.Step(1 / 60f, 8, 1); | |
111 | + Window.ScreenFlip(); | |
112 | + } Window.DxLib_End(); | |
113 | + } catch (Exception e) | |
114 | + { | |
115 | + System.Console.Write(e.ToString()); | |
116 | + } | |
117 | + } | |
118 | + | |
119 | + | |
120 | + } | |
121 | +} |
@@ -0,0 +1,151 @@ | ||
1 | +package | |
2 | +{ | |
3 | + import flash.display.Sprite; | |
4 | + import flash.events.Event; | |
5 | + import flash.events.MouseEvent; | |
6 | + import Box2D.Dynamics.b2World; | |
7 | + import Box2D.Dynamics.b2DebugDraw; | |
8 | + import Box2D.Collision.b2AABB; | |
9 | + import Box2D.Common.Math.b2Vec2; | |
10 | + import Box2D.Dynamics.b2Body; | |
11 | + import Box2D.Dynamics.Joints.b2MouseJoint; | |
12 | + import Box2D.Dynamics.Joints.b2MouseJointDef; | |
13 | + | |
14 | + [SWF(backgroundColor="0x414647")] | |
15 | + public class Main extends Sprite | |
16 | + { | |
17 | + private var world:b2World; | |
18 | + private var scale:Number = 10; | |
19 | + private var joint:b2MouseJoint; | |
20 | + private var jointDef:b2MouseJointDef; | |
21 | + | |
22 | + public function Main() | |
23 | + { | |
24 | + var worldAABB:b2AABB = new b2AABB(); | |
25 | + worldAABB.lowerBound.Set(-100, -100); | |
26 | + worldAABB.upperBound.Set(stage.stageWidth + 100, stage.stageHeight + 100); | |
27 | + | |
28 | + var gravity:b2Vec2 = new b2Vec2(0, 10); | |
29 | + world = new b2World(worldAABB, gravity, true); | |
30 | + | |
31 | + var draw:b2DebugDraw = new b2DebugDraw(); | |
32 | + draw.m_sprite = this; | |
33 | + draw.m_drawScale = scale; | |
34 | + draw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit; | |
35 | + world.SetDebugDraw(draw); | |
36 | + | |
37 | + Shape.world = world; | |
38 | + Shape.scale = scale; | |
39 | + | |
40 | + Shape.create( { shape:Shape.RECT, x:0, y: -1, width:465, height:1 } ); | |
41 | + Shape.create( { shape:Shape.RECT, x:-1, y: 0, width:1, height:465 } ); | |
42 | + Shape.create( { shape:Shape.RECT, x:0, y: 465, width:465, height:1 } ); | |
43 | + Shape.create( { shape:Shape.RECT, x:465, y: 0, width:1, height:465 } ); | |
44 | + | |
45 | + var body:b2Body = Shape.create( { shape:Shape.CIRCLE, x:100, y:200, radius:20, density:1, restitution:0.5 } ); | |
46 | + jointDef = new b2MouseJointDef(); | |
47 | + jointDef.body1 = world.GetGroundBody(); | |
48 | + jointDef.body2 = body; | |
49 | + jointDef.target = body.GetWorldCenter(); | |
50 | + jointDef.maxForce = 5000; | |
51 | + | |
52 | + addEventListener(Event.ENTER_FRAME, onEnterFrame); | |
53 | + stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); | |
54 | + stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); | |
55 | + stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); | |
56 | + } | |
57 | + | |
58 | + private function onMouseDown(event:MouseEvent):void | |
59 | + { | |
60 | + joint = world.CreateJoint(jointDef) as b2MouseJoint; | |
61 | + onMouseMove(); | |
62 | + } | |
63 | + | |
64 | + private function onMouseUp(event:MouseEvent):void | |
65 | + { | |
66 | + if (joint) world.DestroyJoint(joint); | |
67 | + joint = null; | |
68 | + } | |
69 | + | |
70 | + private function onMouseMove(event:MouseEvent = null):void | |
71 | + { | |
72 | + if (joint) | |
73 | + { | |
74 | + joint.SetTarget(new b2Vec2(mouseX / scale, mouseY / scale)); | |
75 | + } | |
76 | + } | |
77 | + | |
78 | + private function onEnterFrame(event:Event):void | |
79 | + { | |
80 | + world.Step(1 / stage.frameRate, 10); | |
81 | + } | |
82 | + } | |
83 | +} | |
84 | + | |
85 | +import Box2D.Collision.Shapes.b2CircleDef; | |
86 | +import Box2D.Collision.Shapes.b2PolygonDef; | |
87 | +import Box2D.Collision.Shapes.b2ShapeDef; | |
88 | +import Box2D.Common.Math.b2Vec2; | |
89 | +import Box2D.Dynamics.b2Body; | |
90 | +import Box2D.Dynamics.b2BodyDef; | |
91 | +import Box2D.Dynamics.b2World; | |
92 | + | |
93 | +class Shape | |
94 | +{ | |
95 | + public static const CIRCLE:int = 0; | |
96 | + public static const RECT:int = 1; | |
97 | + | |
98 | + public static var world:b2World; | |
99 | + public static var scale:Number; | |
100 | + | |
101 | + public static function addVec2(body:b2Body, x:Number, y:Number):void | |
102 | + { | |
103 | + body.SetXForm(new b2Vec2(body.GetPosition().x + x / scale, body.GetPosition().y + y / scale), body.GetAngle()); | |
104 | + } | |
105 | + | |
106 | + public static function setVec2(body:b2Body, x:Number, y:Number):void | |
107 | + { | |
108 | + body.SetXForm(new b2Vec2(x / scale, y / scale), body.GetAngle()); | |
109 | + } | |
110 | + | |
111 | + public static function addAngle(body:b2Body, rotate:Number):void | |
112 | + { | |
113 | + body.SetXForm(body.GetPosition(), body.GetAngle() + rotate * Math.PI / 180); | |
114 | + } | |
115 | + | |
116 | + public static function setAngle(body:b2Body, rotate:Number):void | |
117 | + { | |
118 | + body.SetXForm(body.GetPosition(), rotate * Math.PI / 180); | |
119 | + } | |
120 | + | |
121 | + public static function create(params:Object):b2Body | |
122 | + { | |
123 | + var def:b2BodyDef = new b2BodyDef(); | |
124 | + if (params.angle) def.angle = params.angle * Math.PI / 180; | |
125 | + | |
126 | + var shape:b2ShapeDef; | |
127 | + if (params.shape == Shape.RECT) | |
128 | + { | |
129 | + def.position.Set((params.x + params.width / 2) / scale, (params.y + params.height / 2) / scale); | |
130 | + shape = new b2PolygonDef(); | |
131 | + b2PolygonDef(shape).SetAsBox(params.width / 2 / scale, params.height / 2 / scale); | |
132 | + } | |
133 | + else if (params.shape == Shape.CIRCLE) | |
134 | + { | |
135 | + def.position.Set(params.x / scale, params.y / scale); | |
136 | + shape = new b2CircleDef(); | |
137 | + b2CircleDef(shape).radius = params.radius / scale; | |
138 | + } | |
139 | + | |
140 | + shape.density = params.density; | |
141 | + shape.restitution = params.restitution; | |
142 | + | |
143 | + var body:b2Body = world.CreateBody(def); | |
144 | + body.CreateShape(shape); | |
145 | + | |
146 | + if (shape.density > 0) body.SetMassFromShapes(); | |
147 | + | |
148 | + return body; | |
149 | + } | |
150 | +} | |
151 | + | |
\ No newline at end of file |
@@ -0,0 +1,80 @@ | ||
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 DxgameForBox2d | |
9 | +{ | |
10 | + class StaticBox:DinamicBox | |
11 | + { | |
12 | + | |
13 | + | |
14 | + float rest = 0.6f, dens = 0, fric = 0.3f; | |
15 | + delegate void func(); | |
16 | + public StaticBox(Vec2 pos, float angle, World world, float boxwidth, float boxheight, bool noRotateFlag, Dictionary<UserDataKind, string> userdata) | |
17 | + : base(pos, angle, world, boxwidth, boxheight, noRotateFlag, userdata, new Dictionary<ObjectDataKind, float> {{ObjectDataKind.Density,0 }}) | |
18 | + { | |
19 | + //ボディの仮想的な初期状態を定義する | |
20 | + //Bodydef.Angle = angle; | |
21 | + //Bodydef.Position = pos; | |
22 | + //Bodydef.FixedRotation = noRotateFlag; | |
23 | + //ボディの回転減衰率 | |
24 | + //Bodydef.AngularDamping = 5; | |
25 | + | |
26 | + //NoRotateFlag = noRotateFlag; | |
27 | + //makepolygon(boxwidth, boxheight, world, userdata); | |
28 | + | |
29 | + //todo コンストラクタの処理を変えたい | |
30 | + //やるなら関数のデリゲート渡してそれ実行? | |
31 | + | |
32 | + | |
33 | + } | |
34 | + /* | |
35 | + //特に必要ない | |
36 | + void makepolygon(float boxwidth, float boxheight, World world, Dictionary<UserDataKind, string> userdata) | |
37 | + { | |
38 | + //ポリゴンの形状を決定する | |
39 | + Boxheight = boxheight; | |
40 | + Boxwidth = boxwidth; | |
41 | + | |
42 | + //if (userdata.ContainsKey(UserDataKind.Name)) UserData.name = userdata[UserDataKind.Name]; | |
43 | + if (userdata.ContainsKey(UserDataKind.Filter)) | |
44 | + { | |
45 | + FilterData fil = new FilterData(); | |
46 | + fil.CategoryBits = Convert.ToUInt16(userdata[UserDataKind.Filter]); | |
47 | + fil.MaskBits = fil.CategoryBits; | |
48 | + UserData.filter = fil; | |
49 | + } | |
50 | + Bodydef.UserData = UserData; | |
51 | + | |
52 | + | |
53 | + SelfBody = world.CreateBody(Bodydef); | |
54 | + PolygonDef p = def(rest, dens, fric, UserData.filter); | |
55 | + p.SetAsBox(boxwidth, boxheight); | |
56 | + SelfBody.CreateShape(p); | |
57 | + | |
58 | + //SelfBody.SetMassFromShapes(); | |
59 | + } | |
60 | + */ | |
61 | + /// <summary> | |
62 | + /// ポリゴン定義 | |
63 | + /// </summary> | |
64 | + /// <param name="rest">反発</param> | |
65 | + /// <param name="dens">密度</param> | |
66 | + /// <param name="fric">摩擦</param> | |
67 | + /// <returns></returns> | |
68 | + PolygonDef def(float rest, float dens, float fric,FilterData fil) | |
69 | + { | |
70 | + PolygonDef p = new PolygonDef(); | |
71 | + p.Restitution = rest; | |
72 | + p.Density = dens; | |
73 | + p.Friction = fric; | |
74 | + p.Filter = fil; | |
75 | + return p; | |
76 | + } | |
77 | + | |
78 | + | |
79 | + } | |
80 | +} |
@@ -0,0 +1,63 @@ | ||
1 | +using System; | |
2 | +using System.Collections.Generic; | |
3 | + | |
4 | +using System.Text; | |
5 | +using Importing.DxLib; | |
6 | +using Box2DX.Dynamics; | |
7 | +using Box2DX.Common; | |
8 | + | |
9 | +namespace DxgameForBox2d | |
10 | +{ | |
11 | + class Debug:DebugDraw | |
12 | + { | |
13 | + | |
14 | + public static void PrintContact(Vec2 pos) | |
15 | + { | |
16 | + Window.DrawLine((int)pos.X - 5, (int)pos.Y - 5, (int)pos.X + 5, (int)pos.Y + 5, new COLOR_U8(0xFF0000)); | |
17 | + Window.DrawLine((int)pos.X + 5, (int)pos.Y - 5, (int)pos.X - 5, (int)pos.Y + 5, new COLOR_U8(0xFF0000)); | |
18 | + } | |
19 | + | |
20 | + public static void DebugHit(string colname) | |
21 | + { | |
22 | + Window.DrawString(2, 300, colname, new COLOR_U8(0xFFFFFF)); | |
23 | + } | |
24 | + | |
25 | + public override void DrawCircle(Box2DX.Common.Vec2 center, float radius, Color color) | |
26 | + { | |
27 | + //Window.DrawLine | |
28 | + } | |
29 | + | |
30 | + public override void DrawPolygon(Box2DX.Common.Vec2[] vertices, int vertexCount, Color color) | |
31 | + { | |
32 | + for (int i = 0; i < vertexCount - 1; i++) | |
33 | + { | |
34 | + Window.DrawLine((int)vertices[i].X, (int)vertices[i].Y, (int)vertices[i + 1].X, (int)vertices[i + 1].Y, new COLOR_U8(0xffffff)); | |
35 | + } | |
36 | + Window.DrawLine((int)vertices[vertexCount].X, (int)vertices[vertexCount].Y, (int)vertices[0].X, (int)vertices[0].Y, new COLOR_U8(0xffffff)); | |
37 | + } | |
38 | + public override void DrawSegment(Box2DX.Common.Vec2 p1, Box2DX.Common.Vec2 p2, Color color) | |
39 | + { | |
40 | + //throw new NotImplementedException(); | |
41 | + } | |
42 | + | |
43 | + public override void DrawSolidCircle(Box2DX.Common.Vec2 center, float radius, Box2DX.Common.Vec2 axis, Color color) | |
44 | + { | |
45 | + //throw new NotImplementedException(); | |
46 | + } | |
47 | + | |
48 | + public override void DrawSolidPolygon(Box2DX.Common.Vec2[] vertices, int vertexCount, Color color) | |
49 | + { | |
50 | + | |
51 | + for (int i = 0; i < vertexCount - 1; i++) | |
52 | + { | |
53 | + Window.DrawLine((int)vertices[i].X, (int)vertices[i].Y, (int)vertices[i + 1].X, (int)vertices[i + 1].Y, new COLOR_U8(0x00ffff)); | |
54 | + } | |
55 | + Window.DrawLine((int)vertices[vertexCount-1].X, (int)vertices[vertexCount-1].Y, (int)vertices[0].X, (int)vertices[0].Y, new COLOR_U8(0x00ffff)); | |
56 | + } | |
57 | + | |
58 | + public override void DrawXForm(Box2DX.Common.XForm xf) | |
59 | + { | |
60 | + //throw new NotImplementedException(); | |
61 | + } | |
62 | + } | |
63 | +} |
@@ -0,0 +1,44 @@ | ||
1 | +public enum UserDataKind | |
2 | +{ | |
3 | + Name, | |
4 | + /// <summary> | |
5 | + /// フィルター。単純な衝突判定の時にはこちらを使う、マスクとカテゴリに同値が入る。 | |
6 | + /// </summary> | |
7 | + Filter, | |
8 | + /// <summary> | |
9 | + /// カテゴリのビット。マスクと組み合わせて複雑な衝突判定をあらわす。 | |
10 | + /// </summary> | |
11 | + CategoryBits, | |
12 | + /// <summary> | |
13 | + /// マスクのビット。カテゴリと組み合わせて複雑な衝突判定をあらわす。 | |
14 | + /// </summary> | |
15 | + MaskBits, | |
16 | + /// <summary> | |
17 | + /// グループのインデックス。同じグループにいる場合、必ず衝突し、負の同じグループにいるのなら必ず衝突しない | |
18 | + /// </summary> | |
19 | + GroupIndex, | |
20 | +} | |
21 | + | |
22 | +public enum ObjectDataKind | |
23 | +{ | |
24 | + /// <summary> | |
25 | + /// 物質の密度を指定します、50位でかなり重たい物体、0.1f位でかなり軽い物体、0で動かない物体になります | |
26 | + /// </summary> | |
27 | + Density, | |
28 | + /// <summary> | |
29 | + /// 反発を指定します、デフォルトでは0.6fが入ります、結構強めに跳ね返ってます | |
30 | + /// </summary> | |
31 | + Restitution, | |
32 | + /// <summary> | |
33 | + /// 摩擦を指定します、デフォルトでは0.3fが入ります、あまり滑らない感じです | |
34 | + /// </summary> | |
35 | + Friction, | |
36 | + /// <summary> | |
37 | + /// 回転の減衰率を指定します、デフォルトでは5、あまり回りません | |
38 | + /// </summary> | |
39 | + AngularDamping, | |
40 | + /// <summary> | |
41 | + /// 加速の減衰率を指定します、デフォルトでは0、特に減衰しません | |
42 | + /// </summary> | |
43 | + LinearDamping, | |
44 | +} | |
\ No newline at end of file |
@@ -0,0 +1,22 @@ | ||
1 | +using System; | |
2 | +using System.Collections.Generic; | |
3 | +using System.Text; | |
4 | +using Box2DX.Collision; | |
5 | + | |
6 | + | |
7 | +namespace DxgameForBox2d | |
8 | +{ | |
9 | + /// <summary> | |
10 | + /// HPだとか発動したいイベントを内包したり、オブジェクトがデータを持つ時に使用する | |
11 | + /// </summary> | |
12 | + public class myUserData | |
13 | + { | |
14 | + public string name; | |
15 | + public int id; | |
16 | + public Shape preCollide; | |
17 | + public FilterData filter; | |
18 | + public int HP; | |
19 | + public bool JointBroken; | |
20 | + } | |
21 | + | |
22 | +} |