コード例 #1
0
ファイル: MyWall.cs プロジェクト: weimingtom/erica
        public static Node Create(Vector3 size, Vector3 pos, float angle)
        {
            var width = (int)size.X;
            var height = (int)size.Y;
            var depth = (int)size.Z;

            var cmp = new MyWall ();

            var spr = new Sprite (width, height);
            spr.SetOffset (-width / 2, -height / 2);
            spr.Color = Color.White;

            var body = new RigidBody ();
            body.Mass = 0;
            body.Material = new PhysicsMaterial ();
            body.Material.Restitution = 1;
            body.Use2D = true;
            body.AddShape (new BoxShape (width / 2, height / 2, depth/2));

            var node = new Node ("Wall");
            node.Attach (cmp);
            node.Attach (spr);
            node.Attach (body);

            node.Translation = pos;
            node.Rotate (angle, 0, 0, 1);

            return node;
        }
コード例 #2
0
ファイル: MyBox.cs プロジェクト: weimingtom/erica
        public static Node Create(Vector3 pos, float angle)
        {
            var cmp = new MyBox ();
            var width = 64;
            var height = 64;
            var depth = 64;

            var spr = new Sprite (width, height);
            spr.AddTexture (new Texture("media/Box-64x64.png"));
            spr.SetOffset (-width / 2, -height / 2);

            var body = new RigidBody ();
            body.AddShape( new BoxShape (width / 2, height / 2, depth / 2));
            body.Material = new PhysicsMaterial ();
            body.Material.Restitution = 1.0f;
            //body.UseCCD = true;

            var node = new Node ("Block");
            node.Attach (cmp);
            node.Attach (spr);
            node.Attach (body);

            node.Translation = pos;
            node.Rotation = new Quaternion (angle, 0, 0, 1);

            return node;
        }
コード例 #3
0
ファイル: MyBall.cs プロジェクト: weimingtom/erica
        public static Node Create(string texName, Vector3 pos, float angle)
        {
            var cmp = new MyBall ();

            var tex = new Texture (texName);
            var radius = tex.Width / 2;

            var spr = new Sprite (tex.Width, tex.Height);
            spr.AddTexture (tex);
            spr.SetOffset (-tex.Width/2, -tex.Height/2);

            var body = new RigidBody ();
            body.AddShape (new SphereShape (radius));
            body.Mass = 1;
            body.Material = new PhysicsMaterial ();
            body.Material.Restitution = 1f;
            body.Use2D = true;

            var node = new Node ("Ball");
            node.Attach (spr);
            node.Attach (body);

            node.Translation = pos;
            node.Rotate (angle, 0, 0, 1);

            return node;
        }
コード例 #4
0
        public void Test_IsRegistered()
        {
            var node = new Node ();
            var rb = new RigidBody ();
            rb.AddShape (new SphereShape (1));
            rb.Material = new PhysicsMaterial ();
            node.Attach (rb);

            var phys = new PhysicsSimulator ();
            phys.AddRigidBody (node);

            Assert.AreEqual (true, phys.IsRegistered(node));
        }
コード例 #5
0
        public void Test_AddRigidBody()
        {
            var node = new Node ();
            var rb = new RigidBody ();
            rb.AddShape(new SphereShape (1));
            rb.Material = new PhysicsMaterial ();
            node.Attach (rb);

            var phys = new PhysicsSimulator ();
            phys.AddRigidBody (node);

            Assert.AreEqual (1, phys.RigidBodyCount);
            Assert.AreEqual (1, phys.RigidBodies.Count ());
        }
コード例 #6
0
        public void Test_ApplyForce()
        {
            var ball1 = new Node ("Ball1");
            var rb1 = new RigidBody ();
            rb1.Mass = 1;
            rb1.AddShape ( new SphereShape (1));
            rb1.Material = new PhysicsMaterial ();
            rb1.Material.Restitution = 1;
            ball1.Attach (rb1);

            var ball2 = new Node ("Ball2");
            var rb2 = new RigidBody ();
            rb2.Mass = 1;
            rb2.AddShape ( new SphereShape (1));
            rb2.Material = new PhysicsMaterial ();
            rb2.Material.Restitution = 1;
            ball2.Attach (rb2);

            var wld = new World ("");
            wld.AddChild (ball1);
            wld.AddChild (ball2);

            wld.PhysicsSimulator.SetGravity (0, 0, 0);

            ball2.Translate (10, 0, 0);

            // ①→②
            //   ①②→ (運動量保存の法則)
            ball1.RigidBody.ApplyForce (1000, 0, 0);

            for (var i = 0; i < 100; i++) {
                wld.PhysicsUpdate (33);
                Debug.WriteLine ("Ball1 = {0}, Ball2 = {1}", ball1.Position, ball2.Position);
            }

            Assert.AreEqual (new Vector3 (8.0333f, 0, 0), ball1.Position);
            Assert.AreEqual (new Vector3 (56.6890f, 0, 0), ball2.Position);

            wld.Destroy ();
        }