Exemplo n.º 1
0
		void CreateNinja() {
			
			
			spriteImageName = String.Format("{0}_standing", baseImageName); 

			onGround = false;
			
			// Define the dynamic body.
			var bodyDef = new b2BodyDef();
			bodyDef.type = b2BodyType.b2_staticBody; //or you could use b2DynamicBody to start the ninja as dynamic
			
			bodyDef.position.Set(initialLocation.X/Constants.PTM_RATIO, initialLocation.Y/Constants.PTM_RATIO);
			
			var shape = new b2CircleShape();
			var radiusInMeters = (40 / Constants.PTM_RATIO) * 0.5f; //increase or decrease 40 for a different circle size definition
			
			shape.Radius = radiusInMeters;
			
			
			// Define the dynamic body fixture.
			var fixtureDef = new b2FixtureDef();
			fixtureDef.shape = shape;	
			fixtureDef.density = 1.0f;
			fixtureDef.friction = 1.0f;
			fixtureDef.restitution =  0.1f;
			
			CreateBodyWithSpriteAndFixture(theWorld, bodyDef, fixtureDef, spriteImageName);
			
		}
Exemplo n.º 2
0
        public SphereStack()
        {
            {
                b2BodyDef bd  = new b2BodyDef();
                b2Body ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                b2CircleShape shape = new b2CircleShape();
                shape.Radius = 1.0f;

                for (int i = 0; i < e_count; ++i)
                {
                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(0.0f, 4.0f + 3.0f * i);

                    m_bodies[i] = m_world.CreateBody(bd);

                    m_bodies[i].CreateFixture(shape, 1.0f);

                    m_bodies[i].LinearVelocity = new b2Vec2(0.0f, -50.0f);
                }
            }
        }
Exemplo n.º 3
0
        public JumpPad(b2Vec2 JumpImpuls, CCPoint Position, b2World gameWorld)
        {
            this.Texture = new CCTexture2D ("jumppad");
            this.Scale = SpriteScale;
            this.Position = Position;
            this.IsAntialiased = false;

            jumpImpuls = JumpImpuls;
            totalJumps = 0;

            //box2d
            b2BodyDef jumpPadDef = new b2BodyDef ();
            jumpPadDef.type = b2BodyType.b2_kinematicBody;
            jumpPadDef.position = new b2Vec2 ((Position.X + this.ScaledContentSize.Width/2)/PhysicsHandler.pixelPerMeter, (Position.Y + this.ScaledContentSize.Height/4) / PhysicsHandler.pixelPerMeter);
            JumpPadBody = gameWorld.CreateBody (jumpPadDef);

            b2PolygonShape jumpPadShape = new b2PolygonShape ();
            jumpPadShape.SetAsBox ((float)this.ScaledContentSize.Width / PhysicsHandler.pixelPerMeter / 2, (float)this.ScaledContentSize.Height / PhysicsHandler.pixelPerMeter / 4);// /4 weil die hitbox nur die hälfte der textur ist

            b2FixtureDef jumpPadFixture = new b2FixtureDef ();
            jumpPadFixture.shape = jumpPadShape;
            jumpPadFixture.density = 0.0f; //Dichte
            jumpPadFixture.restitution = 0f; //Rückprall
            jumpPadFixture.friction = 0f;
            jumpPadFixture.userData = WorldFixtureData.jumppad;
            JumpPadBody.CreateFixture (jumpPadFixture);
            //
        }
Exemplo n.º 4
0
        public VaryingRestitution()
        {
            {
                b2BodyDef bd  = new b2BodyDef();
                b2Body ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                b2CircleShape shape = new b2CircleShape();
                shape.Radius = 1.0f;

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = shape;
                fd.density = 1.0f;

                float[] restitution = {0.0f, 0.1f, 0.3f, 0.5f, 0.75f, 0.9f, 1.0f};

                for (int i = 0; i < 7; ++i)
                {
                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(-10.0f + 3.0f * i, 20.0f);

                    b2Body body = m_world.CreateBody(bd);

                    fd.restitution = restitution[i];
                    body.CreateFixture(fd);
                }
            }
        }
Exemplo n.º 5
0
        public void Break()
        {
            // Create two bodies from one.
            b2Body body1 = m_piece1.Body;
            b2Vec2 center = body1.WorldCenter;

            body1.DestroyFixture(m_piece2);
            m_piece2 = null;

            b2BodyDef bd  = new b2BodyDef();
            bd.type = b2BodyType.b2_dynamicBody;
            bd.position = body1.Position;
            bd.angle = body1.Angle;

            b2Body body2 = m_world.CreateBody(bd);
            m_piece2 = body2.CreateFixture(m_shape2, 1.0f);

            // Compute consistent velocities for new bodies based on
            // cached velocity.
            b2Vec2 center1 = body1.WorldCenter;
            b2Vec2 center2 = body2.WorldCenter;

            var diff1 = center1 - center;
            var diff2 = center2 - center;

            b2Vec2 velocity1 = m_velocity + b2Math.b2Cross(m_angularVelocity, ref diff1);
            b2Vec2 velocity2 = m_velocity + b2Math.b2Cross(m_angularVelocity, ref diff2);

            body1.AngularVelocity = m_angularVelocity;
            body1.LinearVelocity = velocity1;

            body2.AngularVelocity = m_angularVelocity;
            body2.LinearVelocity = velocity2;
        }
Exemplo n.º 6
0
        public Breakable()
        {
            // Ground body
            {
                b2BodyDef bd  = new b2BodyDef();
                b2Body ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            // Breakable dynamic body
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position = new b2Vec2(0.0f, 40.0f);
                bd.angle = 0.25f * b2Settings.b2_pi;
                m_body1 = m_world.CreateBody(bd);

                m_shape1.SetAsBox(0.5f, 0.5f, new b2Vec2(-0.5f, 0.0f), 0.0f);
                m_piece1 = m_body1.CreateFixture(m_shape1, 1.0f);

                m_shape2.SetAsBox(0.5f, 0.5f, new b2Vec2(0.5f, 0.0f), 0.0f);
                m_piece2 = m_body1.CreateFixture(m_shape2, 1.0f);
            }

            m_break = false;
            m_broke = false;
        }
Exemplo n.º 7
0
        void InitPhysics()
        {
            var gravity = new b2Vec2(0.0f, -10.0f);
            world = new b2World(gravity);

            world.SetAllowSleeping(true);
            world.SetContinuousPhysics(true);

            var def = new b2BodyDef();
            def.allowSleep = true;
            def.position = b2Vec2.Zero;
            def.type = b2BodyType.b2_staticBody;

            b2Body groundBody = world.CreateBody(def);
            groundBody.SetActive(true);

            b2EdgeShape groundBox = new b2EdgeShape();
            groundBox.Set(b2Vec2.Zero, new b2Vec2(900, 100));

            b2FixtureDef fd = new b2FixtureDef();
            fd.friction = 0.3f;
            fd.restitution = 0.1f;
            fd.shape = groundBox;

            groundBody.CreateFixture(fd);
        }
Exemplo n.º 8
0
 public MouseTouch(b2World world, RubeBasicLayer layer)
 {
     parent = layer;
     m_world = world;
     b2BodyDef bodyDef = new b2BodyDef();
     m_groundBody = m_world.CreateBody(bodyDef);
 }
Exemplo n.º 9
0
        public ContinuousTest()
        {
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.position.Set(0.0f, 0.0f);
                b2Body body = m_world.CreateBody(bd);

                b2EdgeShape edge = new b2EdgeShape();

                edge.Set(new b2Vec2(-10.0f, 0.0f), new b2Vec2(10.0f, 0.0f));
                body.CreateFixture(edge, 0.0f);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.2f, 1.0f, new b2Vec2(0.5f, 1.0f), 0.0f);
                body.CreateFixture(shape, 0.0f);
            }

#if true
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(0.0f, 20.0f);
                //bd.angle = 0.1f;

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(2.0f, 0.1f);

                m_body = m_world.CreateBody(bd);
                m_body.CreateFixture(shape, 1.0f);

                m_angularVelocity = Rand.RandomFloat(-50.0f, 50.0f);
                //m_angularVelocity = 46.661274f;
                m_body.LinearVelocity = new b2Vec2(0.0f, -100.0f);
                m_body.AngularVelocity = m_angularVelocity;
            }
#else
        {
            b2BodyDef bd  = new b2BodyDef();
            bd.type = b2BodyType.b2_dynamicBody;
            bd.position.Set(0.0f, 2.0f);
            b2Body body = m_world.CreateBody(bd);

            b2CircleShape shape = new b2CircleShape();
            shape.Position = b2Vec2.Zero;
            shape.Radius = 0.5f;
            body.CreateFixture(shape, 1.0f);

            bd.bullet = true;
            bd.position.Set(0.0f, 10.0f);
            body = m_world.CreateBody(bd);
            body.CreateFixture(shape, 1.0f);
            body.LinearVelocity = new b2Vec2(0.0f, -100.0f);
        }
#endif
        }
Exemplo n.º 10
0
        public Pulleys()
        {
            float y = 16.0f;
            float L = 12.0f;
            float a = 1.0f;
            float b = 2.0f;

            b2Body ground = null;
            {
                b2BodyDef bd  = new b2BodyDef();
                ground = m_world.CreateBody(bd);

                b2EdgeShape edge = new b2EdgeShape();
                edge.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                //ground->CreateFixture(&shape, 0.0f);

                b2CircleShape circle = new b2CircleShape();
                circle.Radius = 2.0f;

                circle.Position = new b2Vec2(-10.0f, y + b + L);
                ground.CreateFixture(circle, 0.0f);

                circle.Position = new b2Vec2(10.0f, y + b + L);
                ground.CreateFixture(circle, 0.0f);
            }

            {

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(a, b);

                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;

                //bd.fixedRotation = true;
                bd.position.Set(-10.0f, y);
                b2Body body1 = m_world.CreateBody(bd);
                body1.CreateFixture(shape, 5.0f);

                bd.position.Set(10.0f, y);
                b2Body body2 = m_world.CreateBody(bd);
                body2.CreateFixture(shape, 5.0f);

                b2PulleyJointDef pulleyDef = new b2PulleyJointDef();
                b2Vec2 anchor1 = new b2Vec2(-10.0f, y + b);
                b2Vec2 anchor2 = new b2Vec2(10.0f, y + b);
                b2Vec2 groundAnchor1 = new b2Vec2(-10.0f, y + b + L);
                b2Vec2 groundAnchor2 = new b2Vec2(10.0f, y + b + L);
                pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 1.5f);

                m_joint1 = (b2PulleyJoint) m_world.CreateJoint(pulleyDef);
            }
        }
Exemplo n.º 11
0
        public SensorTest()
        {
            {
                b2BodyDef bd  = new b2BodyDef();
                b2Body ground = m_world.CreateBody(bd);

                {
                    b2EdgeShape shape = new b2EdgeShape();
                    shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                    ground.CreateFixture(shape, 0.0f);
                }

#if false
            {
                b2FixtureDef sd = new b2FixtureDef();
                sd.SetAsBox(10.0f, 2.0f, new b2Vec2(0.0f, 20.0f), 0.0f);
                sd.isSensor = true;
                m_sensor = ground.CreateFixture(sd);
            }
#else
                {
                    b2CircleShape shape = new b2CircleShape();
                    shape.Radius = 5.0f;
                    shape.Position = new b2Vec2(0.0f, 10.0f);

                    b2FixtureDef fd = new b2FixtureDef();
                    fd.shape = shape;
                    fd.isSensor = true;
                    m_sensor = ground.CreateFixture(fd);
                }
#endif
            }

            {
                b2CircleShape shape = new b2CircleShape();
                shape.Radius = 1.0f;

                for (int i = 0; i < e_count; ++i)
                {
                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(-10.0f + 3.0f * i, 20.0f);
                    bd.userData = i; //  m_touching[i];

                    m_touching[i] = false;
                    m_bodies[i] = m_world.CreateBody(bd);

                    m_bodies[i].CreateFixture(shape, 1.0f);
                }
            }
        }
Exemplo n.º 12
0
        //public const int e_columnCount = 1;
        //public const int e_rowCount = 1;

        public VerticalStack()
        {
            {
                b2BodyDef bd  = new b2BodyDef();
                b2Body ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);

                shape.Set(new b2Vec2(20.0f, 0.0f), new b2Vec2(20.0f, 20.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            float[] xs = {0.0f, -10.0f, -5.0f, 5.0f, 10.0f};

            for (int j = 0; j < e_columnCount; ++j)
            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.5f, 0.5f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = shape;
                fd.density = 1.0f;
                fd.friction = 0.3f;

                for (int i = 0; i < e_rowCount; ++i)
                {
                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;

                    int n = j * e_rowCount + i;
                    Debug.Assert(n < e_rowCount * e_columnCount);
                    m_indices[n] = n;
                    bd.userData = m_indices[n];

                    float x = 0.0f;
                    //float32 x = RandomFloat(-0.02f, 0.02f);
                    //float32 x = i % 2 == 0 ? -0.025f : 0.025f;
                    bd.position.Set(xs[j] + x, 0.752f + 1.54f * i);
                    b2Body body = m_world.CreateBody(bd);

                    m_bodies[n] = body;

                    body.CreateFixture(fd);
                }
            }

            m_bullet = null;
        }
Exemplo n.º 13
0
        public Confined()
        {
            {
                b2BodyDef bd  = new b2BodyDef();
                b2Body ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();

                // Floor
                shape.Set(new b2Vec2(-10.0f, 0.0f), new b2Vec2(10.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);

                // Left wall
                shape.Set(new b2Vec2(-10.0f, 0.0f), new b2Vec2(-10.0f, 20.0f));
                ground.CreateFixture(shape, 0.0f);

                // Right wall
                shape.Set(new b2Vec2(10.0f, 0.0f), new b2Vec2(10.0f, 20.0f));
                ground.CreateFixture(shape, 0.0f);

                // Roof
                shape.Set(new b2Vec2(-10.0f, 20.0f), new b2Vec2(10.0f, 20.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            float radius = 0.5f;
            b2CircleShape shape1 = new b2CircleShape();
            shape1.Position = b2Vec2.Zero;
            shape1.Radius = radius;

            b2FixtureDef fd = new b2FixtureDef();
            fd.shape = shape1;
            fd.density = 1.0f;
            fd.friction = 0.1f;

            for (int j = 0; j < e_columnCount; ++j)
            {
                for (int i = 0; i < e_rowCount; ++i)
                {
                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(-10.0f + (2.1f * j + 1.0f + 0.01f * i) * radius, (2.0f * i + 1.0f) * radius);
                    b2Body body = m_world.CreateBody(bd);

                    body.CreateFixture(fd);
                }
            }

            m_world.Gravity = new b2Vec2(0.0f, 0.0f);
        }
Exemplo n.º 14
0
		void CreatePlatform() {
			
			// Define the dynamic body.
			var bodyDef = new b2BodyDef();
			bodyDef.type = b2BodyType.b2_staticBody; //or you could use b2_staticBody
			
			bodyDef.position.Set(initialLocation.X/Constants.PTM_RATIO, initialLocation.Y/Constants.PTM_RATIO);

			var shape = new b2PolygonShape();
			
			var num = 4;
			b2Vec2[] vertices = {
				new b2Vec2(-102.0f / Constants.PTM_RATIO, -49.5f / Constants.PTM_RATIO),
				new b2Vec2(-113.0f / Constants.PTM_RATIO, -81.5f / Constants.PTM_RATIO),
				new b2Vec2(113.0f / Constants.PTM_RATIO, -84.5f / Constants.PTM_RATIO),
				new b2Vec2(106.0f / Constants.PTM_RATIO, -47.5f / Constants.PTM_RATIO)
			};
			
			shape.Set(vertices, num);
			
			
			// Define the dynamic body fixture.
			var fixtureDef = new b2FixtureDef();
			fixtureDef.shape = shape;	
			fixtureDef.density = 1.0f;
			fixtureDef.friction = 0.3f;
			fixtureDef.restitution =  0.1f;
			
			CreateBodyWithSpriteAndFixture(theWorld, bodyDef, fixtureDef, spriteImageName);
			
			//CONTINUING TO ADD BODY SHAPE....
			
			// THIS IS THE Sling base....
			
			//row 1, col 1
			var num2 = 4;
			b2Vec2[] vertices2 = {
				new b2Vec2(41.0f / Constants.PTM_RATIO, -6.5f / Constants.PTM_RATIO),
				new b2Vec2(35.0f / Constants.PTM_RATIO, -57.5f / Constants.PTM_RATIO),
				new b2Vec2(57.0f / Constants.PTM_RATIO, -65.5f / Constants.PTM_RATIO),
				new b2Vec2(49.0f / Constants.PTM_RATIO, -7.5f / Constants.PTM_RATIO)
			};
			
			shape.Set(vertices2, num2);
			fixtureDef.shape = shape;
			body.CreateFixture(fixtureDef);
			
		}
Exemplo n.º 15
0
        protected void CreateBodyWithSpriteAndFixture(b2World world, b2BodyDef bodyDef,
                                                       b2FixtureDef fixtureDef, string spriteName)
        {
            // this is the meat of our class, it creates (OR recreates) the body in the world with the body definition, fixture definition and sprite name

            RemoveBody(); //if remove the body if it already exists
            RemoveSprite(); //if remove the sprite if it already exists

            sprite = new CCSprite(spriteName);
            AddChild(sprite);

            body = world.CreateBody(bodyDef);
            body.UserData = this;

            if (fixtureDef != null)
                body.CreateFixture(fixtureDef);
        }
Exemplo n.º 16
0
        public override void Step(Settings settings)
        {
            base.Step(settings);

            if (m_count < e_count)
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(0.0f, 10.0f);
                b2Body body = m_world.CreateBody(bd);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.125f, 0.125f);
                body.CreateFixture(shape, 1.0f);

                ++m_count;
            }
        }
Exemplo n.º 17
0
		private void CreateGround()
		{
			// Define the dynamic body.
			var bodyDef = new b2BodyDef();
			bodyDef.type = b2BodyType.b2_staticBody; //or you could use b2_staticBody
			
			bodyDef.position.Set(initialLocation.X/Constants.PTM_RATIO, initialLocation.Y/Constants.PTM_RATIO);
			
			b2PolygonShape shape = new b2PolygonShape();
			
			int num = 4;
			b2Vec2[] vertices = {
				new b2Vec2(-1220.0f / Constants.PTM_RATIO, 54.0f / Constants.PTM_RATIO),
				new b2Vec2(-1220.0f / Constants.PTM_RATIO, -52.0f / Constants.PTM_RATIO),
				new b2Vec2(1019.0f / Constants.PTM_RATIO, -52.0f / Constants.PTM_RATIO),
				new b2Vec2(1019.0f / Constants.PTM_RATIO, 54.0f / Constants.PTM_RATIO)
			};
			
			shape.Set(vertices, num);
			
			
			// Define the dynamic body fixture.
			var fixtureDef = new b2FixtureDef();
			fixtureDef.shape = shape;	
			fixtureDef.density = 1.0f;
			fixtureDef.friction = 1.0f;
			fixtureDef.restitution =  0.1f;
			
			CreateBodyWithSpriteAndFixture(theWorld, bodyDef, fixtureDef, spriteImageName);
			
			if (!TheLevel.SharedLevel.IS_RETINA)
			{
				//non retina adjustment
				sprite.ScaleX = 1.05f;
				
			} else {
				
				// retina adjustment
				
				sprite.ScaleX = 2.05f;
				sprite.ScaleY = 2.0f;
			}

		}
Exemplo n.º 18
0
        public Prismatic()
        {
            b2Body ground = null;
            {
                b2BodyDef bd  = new b2BodyDef();
                ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(2.0f, 0.5f);

                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(-10.0f, 10.0f);
                bd.angle = 0.5f * b2Settings.b2_pi;
                bd.allowSleep = false;
                b2Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                b2PrismaticJointDef pjd = new b2PrismaticJointDef();

                // Bouncy limit
                b2Vec2 axis = new b2Vec2(2.0f, 1.0f);
                axis.Normalize();
                pjd.Initialize(ground, body, new b2Vec2(0.0f, 0.0f), axis);

                // Non-bouncy limit
                //pjd.Initialize(ground, body, b2Vec2(-10.0f, 10.0f), b2Vec2(1.0f, 0.0f));

                pjd.motorSpeed = 10.0f;
                pjd.maxMotorForce = 10000.0f;
                pjd.enableMotor = true;
                pjd.lowerTranslation = 0.0f;
                pjd.upperTranslation = 20.0f;
                pjd.enableLimit = true;

                m_joint = (b2PrismaticJoint) m_world.CreateJoint(pjd);
            }
        }
Exemplo n.º 19
0
        public Platform(List<CCPoint> platformWaypoints, int platformSpeed, Container gameContainer)
        {
            this.Texture = new CCTexture2D("platform");
            this.Scale = SpriteScale;
            this.IsAntialiased = false;

            this.Position = platformWaypoints [0];
            Waypoints = platformWaypoints;
            speed = platformSpeed;
            //umso geringer der speed umso schneller die platform

            CurrentWaypoint = 0;
            wayToMove = new CCSize (Waypoints [CurrentWaypoint + 1].X - Waypoints [CurrentWaypoint].X, Waypoints [CurrentWaypoint + 1].Y - Waypoints [CurrentWaypoint].Y);

            //box2d
            b2BodyDef platformDef = new b2BodyDef ();
            platformDef.type = b2BodyType.b2_kinematicBody;
            platformDef.position = new b2Vec2 (Waypoints[CurrentWaypoint].X / PhysicsHandler.pixelPerMeter, Waypoints[CurrentWaypoint].Y / PhysicsHandler.pixelPerMeter);
            platformBody = gameContainer.physicsHandler.gameWorld.CreateBody (platformDef);

            b2PolygonShape platformShape = new b2PolygonShape ();
            platformShape.SetAsBox ((float)this.ScaledContentSize.Width / PhysicsHandler.pixelPerMeter / 2, (float)this.ScaledContentSize.Height / PhysicsHandler.pixelPerMeter / 2);

            b2FixtureDef platformFixture = new b2FixtureDef ();
            platformFixture.shape = platformShape;
            platformFixture.density = 0.0f; //Dichte
            platformFixture.restitution = 0f; //Rückprall
            platformFixture.userData = WorldFixtureData.platform;
            platformBody.CreateFixture (platformFixture);
            //

            this.Position = new CCPoint (platformBody.Position.x * PhysicsHandler.pixelPerMeter, platformBody.Position.y * PhysicsHandler.pixelPerMeter);

            progressionX = wayToMove.Width/ (float)speed;
            progressionY =  wayToMove.Height/(float)speed ;
            if (float.IsInfinity (progressionX))
                progressionX = 0;
            if (float.IsInfinity (progressionY))
                progressionY = 0;
            b2Vec2 Velocity = platformBody.LinearVelocity;
            Velocity.y = progressionY;
            Velocity.x = progressionX;
            platformBody.LinearVelocity = Velocity;
        }
Exemplo n.º 20
0
        public AddPair()
        {
            m_world.Gravity = new b2Vec2(0.0f, 0.0f);
            {
                b2CircleShape shape = new b2CircleShape();
                shape.Position = b2Vec2.Zero;
                shape.Radius = 0.1f;

                float minX = -6.0f;
                float maxX = 0.0f;
                float minY = 4.0f;
                float maxY = 6.0f;

                for (int i = 0; i < 400; ++i)
                {
                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position = new b2Vec2(Rand.RandomFloat(minX, maxX), Rand.RandomFloat(minY, maxY));
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape, 0.01f);
                }
            }

            {
                b2Shape shape = null;
                if (_DoCircle)
                {
                    shape = new b2CircleShape();
                    ((b2CircleShape)shape).Radius = 1.5f;
                }
                else
                {
                    shape = new b2PolygonShape();
                    ((b2PolygonShape)shape).SetAsBox(1.6f, 1.6f);
                }
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position = new b2Vec2(-40.0f, 5.0f);
                bd.bullet = true;
                b2Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 1.0f);
                body.LinearVelocity = new b2Vec2(150.0f, 0.0f);
            }
        }
Exemplo n.º 21
0
        public OneSidedPlatform()
        {
            // Ground
            {
                b2BodyDef bd  = new b2BodyDef();
                b2Body ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-20.0f, 0.0f), new b2Vec2(20.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            // Platform
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.position.Set(0.0f, 10.0f);
                b2Body body = m_world.CreateBody(bd);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(3.0f, 0.5f);
                m_platform = body.CreateFixture(shape, 0.0f);

                m_bottom = 10.0f - 0.5f;
                m_top = 10.0f + 0.5f;
            }

            // Actor
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(0.0f, 12.0f);
                b2Body body = m_world.CreateBody(bd);

                m_radius = 0.5f;
                b2CircleShape shape = new b2CircleShape();
                shape.Radius = m_radius;
                m_character = body.CreateFixture(shape, 20.0f);

                body.LinearVelocity = new b2Vec2(0.0f, -50.0f);

                m_state = State.e_unknown;
            }
        }
Exemplo n.º 22
0
        public Chain()
        {
            b2Body ground = null;
            {
                b2BodyDef bd  = new b2BodyDef();
                ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.6f, 0.125f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = shape;
                fd.density = 20.0f;
                fd.friction = 0.2f;

                b2RevoluteJointDef jd = new b2RevoluteJointDef();
                jd.CollideConnected = false;

                const float y = 25.0f;
                b2Body prevBody = ground;
                for (int i = 0; i < 30; ++i)
                {
                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(0.5f + i, y);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    b2Vec2 anchor = new b2Vec2(i, y);
                    jd.Initialize(prevBody, body, anchor);
                    m_world.CreateJoint(jd);

                    prevBody = body;
                }
            }
        }
Exemplo n.º 23
0
        public void SetJson(string fullpath)
        {
            Console.WriteLine("Full path is: %s", fullpath);

            Nb2dJson json = new Nb2dJson();
            StringBuilder tmp = new StringBuilder();
            m_world = json.ReadFromFile(fullpath, tmp);

            if (m_world != null)
            {
                Console.WriteLine("Loaded JSON ok");
                m_world.SetDebugDraw(m_debugDraw);

                b2BodyDef bodyDef = new b2BodyDef();
                m_groundBody = m_world.CreateBody(bodyDef);
            }
            else
                Console.WriteLine(tmp); //if this warning bothers you, turn off "Typecheck calls to printf/scanf" in the project build settings

        }
Exemplo n.º 24
0
        public CircleBenchmarkTest()
        {
            b2BodyDef bd = new b2BodyDef();
            b2Body ground = m_world.CreateBody(bd);

            // Floor
            b2EdgeShape shape = new b2EdgeShape();
            shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
            ground.CreateFixture(shape, 0.0f);

            // Left wall
            shape = new b2EdgeShape();
            shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(-40.0f, 45.0f));
            ground.CreateFixture(shape, 0.0f);

            // Right wall
            shape = new b2EdgeShape();
            shape.Set(new b2Vec2(40.0f, 0.0f), new b2Vec2(40.0f, 45.0f));
            ground.CreateFixture(shape, 0.0f);

            // Roof
            shape = new b2EdgeShape();
            shape.Set(new b2Vec2(-40.0f, 45.0f), new b2Vec2(40.0f, 45.0f));
            ground.CreateFixture(shape, 0.0f);

            var sphere = new b2CircleShape();
            sphere.Radius = 1.0f;

            for (int i = 0; i < XCount; i++)
            {
                for (int j = 0; j < YCount; ++j)
                {
                    bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(-38f + 2.1f * i, 2.0f + 2.0f * j);

                    var body = m_world.CreateBody(bd);
                    body.CreateFixture(sphere, 1.0f);
                }
            }
        }
Exemplo n.º 25
0
        public BulletTest()
        {
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.position.Set(0.0f, 0.0f);
                b2Body body = m_world.CreateBody(bd);

                b2EdgeShape edge = new b2EdgeShape();

                edge.Set(new b2Vec2(-10.0f, 0.0f), new b2Vec2(10.0f, 0.0f));
                body.CreateFixture(edge, 0.0f);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.2f, 1.0f, new b2Vec2(0.5f, 1.0f), 0.0f);
                body.CreateFixture(shape, 0.0f);
            }

            {
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(0.0f, 4.0f);

                b2PolygonShape box = new b2PolygonShape();
                box.SetAsBox(2.0f, 0.1f);

                m_body = m_world.CreateBody(bd);
                m_body.CreateFixture(box, 1.0f);

                box.SetAsBox(0.25f, 0.25f);

                //m_x = RandomFloat(-1.0f, 1.0f);
                m_x = 0.20352793f;
                bd.position.Set(m_x, 10.0f);
                bd.bullet = true;

                m_bullet = m_world.CreateBody(bd);
                m_bullet.CreateFixture(box, 100.0f);

                m_bullet.LinearVelocity = new b2Vec2(0.0f, -50.0f);
            }
        }
Exemplo n.º 26
0
        public void CreateCircle()
        {
            float radius = 2.0f;
            b2CircleShape shape = new b2CircleShape();
            shape.Position = b2Vec2.Zero;
            shape.Radius = radius;

            b2FixtureDef fd = new b2FixtureDef();
            fd.shape = shape;
            fd.density = 1.0f;
            fd.friction = 0.0f;

            b2Vec2 p = new b2Vec2(Rand.RandomFloat(), 3.0f + Rand.RandomFloat());
            b2BodyDef bd  = new b2BodyDef();
            bd.type = b2BodyType.b2_dynamicBody;
            bd.position = p;
            //bd.allowSleep = false;
            b2Body body = m_world.CreateBody(bd);

            body.CreateFixture(fd);
        }
Exemplo n.º 27
0
        public Pyramid()
        {
            {
                b2BodyDef bd  = new b2BodyDef();
                b2Body ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                float a = 0.5f;
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(a, a);

                b2Vec2 x = new b2Vec2(-7.0f, 0.75f);
                b2Vec2 y;
                b2Vec2 deltaX = new b2Vec2(0.5625f, 1.25f);
                b2Vec2 deltaY = new b2Vec2(1.125f, 0.0f);

                for (int i = 0; i < e_count; ++i)
                {
                    y = x;

                    for (int j = i; j < e_count; ++j)
                    {
                        b2BodyDef bd  = new b2BodyDef();
                        bd.type = b2BodyType.b2_dynamicBody;
                        bd.position = y;
                        b2Body body = m_world.CreateBody(bd);
                        body.CreateFixture(shape, 5.0f);

                        y += deltaY;
                    }

                    x += deltaX;
                }
            }
        }
Exemplo n.º 28
0
        public Tumbler()
        {
            b2Body ground;
            {
                b2BodyDef bd  = new b2BodyDef();
                ground = m_world.CreateBody(bd);
            }

            {
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.allowSleep = false;
                bd.position.Set(0.0f, 10.0f);
                b2Body body = m_world.CreateBody(bd);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.5f, 10.0f, new b2Vec2(10.0f, 0.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);
                shape.SetAsBox(0.5f, 10.0f, new b2Vec2(-10.0f, 0.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);
                shape.SetAsBox(10.0f, 0.5f, new b2Vec2(0.0f, 10.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);
                shape.SetAsBox(10.0f, 0.5f, new b2Vec2(0.0f, -10.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);

                b2RevoluteJointDef jd = new b2RevoluteJointDef();
                jd.BodyA = ground;
                jd.BodyB = body;
                jd.localAnchorA.Set(0.0f, 10.0f);
                jd.localAnchorB.Set(0.0f, 0.0f);
                jd.referenceAngle = 0.0f;
                jd.motorSpeed = 0.05f * b2Settings.b2_pi;
                jd.maxMotorTorque = 1e8f;
                jd.enableMotor = true;
                m_joint = (b2RevoluteJoint) m_world.CreateJoint(jd);
            }

            m_count = 0;
        }
Exemplo n.º 29
0
        public BoxProp(
               b2World b2world,

             double[] size,
             double[] position

            )
        {
            /*
            static rectangle shaped prop
     
                pars:
                size - array [width, height]
                position - array [x, y], in world meters, of center
            */
            this.size = size;

            //initialize body
            var bdef = new b2BodyDef();
            bdef.position = new b2Vec2(position[0], position[1]);
            bdef.angle = 0;
            bdef.fixedRotation = true;
            this.body = b2world.CreateBody(bdef);

            //initialize shape
            var fixdef = new b2FixtureDef();

            var shape = new b2PolygonShape();
            fixdef.shape = shape;

            shape.SetAsBox(this.size[0] / 2, this.size[1] / 2);

            fixdef.restitution = 0.4; //positively bouncy!



            this.body.CreateFixture(fixdef);
        }
Exemplo n.º 30
0
        public ShapeEditing()
        {
            {
                b2BodyDef bd1  = new b2BodyDef();
                b2Body ground = m_world.CreateBody(bd1);

                b2EdgeShape shape1 = new b2EdgeShape();
                shape1.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape1, 0.0f);
            }

            b2BodyDef bd  = new b2BodyDef();
            bd.type = b2BodyType.b2_dynamicBody;
            bd.position.Set(0.0f, 10.0f);
            m_body = m_world.CreateBody(bd);

            b2PolygonShape shape = new b2PolygonShape();
            shape.SetAsBox(4.0f, 4.0f, new b2Vec2(0.0f, 0.0f), 0.0f);
            m_fixture1 = m_body.CreateFixture(shape, 10.0f);

            m_fixture2 = null;

            m_sensor = false;
        }