Example #1
0
        public override Space LoadContent()
        {
            space            = ChipmunkDemoGame.CreateSpace();
            space.Iterations = 5;
            space.Gravity    = new Vect(0, -100);

            Body  body;
            Body  staticBody = space.StaticBody;
            Shape shape;

            // Create the static triangles.
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 6; j++)
                {
                    double stagger = (j % 2) * 40;
                    var    offset  = new Vect(i * 80 - 320 + stagger, j * 70 - 240);
                    shape = new Polygon(staticBody, _tris, Transform.CreateTranslation(offset), 0.0);
                    space.AddShape(shape);
                    shape.Elasticity = 1.0f;
                    shape.Friction   = 1.0f;
                    shape.Filter     = ChipmunkDemoGame.NotGrabbableFilter;
                }
            }

            // Create vertexes for a pentagon shape.
            Vect [] verts = new Vect[NumVertices];

            for (int i = 0; i < NumVertices; i++)
            {
                double angle = -2.0 * Math.PI * i / NumVertices;
                verts[i] = new Vect(10 * Math.Cos(angle), 10 * Math.Sin(angle));
            }

            pentagonMass   = 1.0;
            pentagonMoment = Polygon.MomentForPolygon(1.0, verts, Vect.Zero, 0.0f);

            // Add lots of pentagons.
            for (int i = 0; i < 300; i++)
            {
                body = new Body(pentagonMass, pentagonMoment);
                space.AddBody(body);

                double x = random.NextDouble() * 640 - 320;
                body.Position = new Vect(x, 350);

                shape = new Polygon(body, verts, Transform.Identity, 0.0);
                space.AddShape(shape);

                shape.Elasticity = 0.0;
                shape.Friction   = 0.4;
            }

            return(space);
        }
Example #2
0
        public override Space LoadContent()
        {
            space                    = ChipmunkDemoGame.CreateSpace();
            space.Iterations         = 30;
            space.Gravity            = new Vect(0, -300);
            space.SleepTimeThreshold = 0.5f;
            space.CollisionSlop      = 0.5;

            // Add a floor.
            Shape shape = new Segment(space.StaticBody, new Vect(-600, -240), new Vect(600, -240), 0.0);

            space.AddShape(shape);

            shape.Elasticity = 1.0;
            shape.Friction   = 1.0;
            shape.Filter     = ChipmunkDemoGame.NotGrabbableFilter;


            // Add the dominoes.
            int n = 12;

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < (n - i); j++)
                {
                    var offset = new Vect((j - (n - 1 - i) * 0.5) * 1.5 * Height, (i + 0.5) * (Height + 2 * Width) - Width - 240);
                    AddDomino(offset, false);
                    Vect pos = offset + new Vect(0, (Height + Width) / 2.0);

                    AddDomino(pos, true);

                    if (j == 0)
                    {
                        pos = offset + new Vect(0.5 * (Width - Height), Height + Width);
                        AddDomino(pos, false);
                    }

                    if (j != n - i - 1)
                    {
                        AddDomino(offset + new Vect(Height * 0.75f, (Height + 3 * Width) / 2.0f), true);
                    }
                    else
                    {
                        AddDomino(offset + new Vect(0.5 * (Height - Width), Height + Width), false);
                    }
                }
            }

            return(space);
        }
Example #3
0
        public override Space LoadContent()
        {
            space            = ChipmunkDemoGame.CreateSpace();
            space.Iterations = 1;

            // The space will contain a very large number of similary sized objects.
            // This is the perfect candidate for using the spatial hash.
            // Generally you will never need to do this.
            space.UseSpatialHash(2.0, 10000);

            Body  body;
            Shape shape;

            for (int y = 0; y < ImageHeight; y++)
            {
                for (int x = 0; x < ImageWidth; x++)
                {
                    if (GetPixel(x, y) == 0)
                    {
                        continue;
                    }

                    double x_jitter = 0.05 * random.NextDouble();
                    double y_jitter = 0.05 * random.NextDouble();

                    shape = MakeBall(2 * (x - ImageWidth / 2 + x_jitter), 2 * (ImageHeight / 2 - y + y_jitter));
                    space.AddBody(shape.Body);
                    space.AddShape(shape);
                }
            }

            body = new Body(1e9, double.PositiveInfinity);
            space.AddBody(body);

            body.Position = new Vect(-1000, -10);
            body.Velocity = new Vect(400, 0);

            shape = new Circle(body, 8.0);
            space.AddShape(shape);

            shape.Elasticity = 0.0;
            shape.Friction   = 0.0;
            shape.Filter     = ChipmunkDemoGame.NotGrabbableFilter;

            bodies = space.Bodies.ToArray();

            return(space);
        }
Example #4
0
        public override Space LoadContent()
        {
            space                    = ChipmunkDemoGame.CreateSpace();
            space.Iterations         = 30;
            space.Gravity            = new Vect(0, -500);
            space.SleepTimeThreshold = 0.5;
            space.CollisionSlop      = 0.5;

            Body body;
            Body staticBody = space.StaticBody;

            Shape shape;

            shape = new Segment(staticBody, new Vect(-1000, -230), new Vect(1000, -230), 0.0);
            space.AddShape(shape);

            shape.Elasticity = 1.0;
            shape.Friction   = 1.0;
            shape.Filter     = ChipmunkDemoGame.NotGrabbableFilter;

            double width  = 200;
            double height = 300;
            double mass   = width * height * Density;
            double moment = Box.MomentForBox(mass, width, height);

            body = new Body(mass, moment);
            space.AddBody(body);

            Vect[] vects =
            {
                new Vect(width / 2,  height / 2),
                new Vect(-width / 2, height / 2),
                new Vect(-width / 2, -height / 2),
                new Vect(width / 2,  -height / 2),
            };

            shape = new Polygon(body, vects, 0.0);
            space.AddShape(shape);
            shape.Friction = 0.6;

            return(space);
        }
Example #5
0
        public override Space LoadContent()
        {
            space            = ChipmunkDemoGame.CreateSpace();
            space.Iterations = 20;

            planetBody = new Body(BodyType.Kinematic);

            space.AddBody(planetBody);
            planetBody.AngularVelocity = 0.2;

            for (int i = 0; i < 30; i++)
            {
                AddBox(space);
            }

            Shape shape = new Circle(planetBody, 70.0, Vect.Zero);

            space.AddShape(shape);
            shape.Elasticity = 1.0;
            shape.Friction   = 1.0;
            shape.Filter     = ChipmunkDemoGame.NotGrabbableFilter;

            return(space);
        }
Example #6
0
        public override Space LoadContent()
        {
            space                    = ChipmunkDemoGame.CreateSpace();
            space.Iterations         = 10;
            space.SleepTimeThreshold = 0.5;

            Body  staticBody = space.StaticBody;
            Shape shape      = new Segment(staticBody, new Vect(-320, -240), new Vect(-320, 240), 0.0);

            // Create segments around the edge of the screen.
            space.AddShape(shape);

            shape.Elasticity = 1.0;
            shape.Friction   = 1.0;
            shape.Filter     = ChipmunkDemoGame.NotGrabbableFilter;

            shape = new Segment(staticBody, new Vect(320, -240), new Vect(320, 240), 0.0);
            space.AddShape(shape);
            shape.Elasticity = 1.0;
            shape.Friction   = 1.0;
            shape.Filter     = ChipmunkDemoGame.NotGrabbableFilter;

            shape = new Segment(staticBody, new Vect(-320, -240), new Vect(320, -240), 0.0);
            space.AddShape(shape);
            shape.Elasticity = 1.0;
            shape.Friction   = 1.0;
            shape.Filter     = ChipmunkDemoGame.NotGrabbableFilter;

            shape = new Segment(staticBody, new Vect(-320, 240), new Vect(320, 240), 0.0);
            space.AddShape(shape);
            shape.Elasticity = 1.0;
            shape.Friction   = 1.0;

            shape.Filter = ChipmunkDemoGame.NotGrabbableFilter;

            for (int i = 0; i < 50; i++)
            {
                Body body = AddBox(20, 1);

                Constraint p = new PivotJoint(staticBody, body, Vect.Zero, Vect.Zero);
                space.AddConstraint(p);
                p.MaxBias  = 0;      // disable joint correction
                p.MaxForce = 1000.0; // emulate linear friction

                Constraint g = new GearJoint(staticBody, body, 0.0, 1.0);
                space.AddConstraint(g);
                g.MaxBias  = 0;      // disable joint correction
                g.MaxForce = 5000.0; // emulate angular friction
            }

            // We joint the tank to the control body and control the tank indirectly by modifying the control body.
            tankControlBody = new Body(BodyType.Kinematic);
            space.AddBody(tankControlBody);
            tankBody = AddBox(30, 10);

            Constraint pivot = new PivotJoint(tankControlBody, tankBody, Vect.Zero, Vect.Zero);

            space.AddConstraint(pivot);
            pivot.MaxBias  = 0;       // disable joint correction
            pivot.MaxForce = 10000.0; // emulate linear friction

            Constraint gear = new GearJoint(tankControlBody, tankBody, 0.0, 1.0);

            space.AddConstraint(gear);
            gear.ErrorBias = 0;       // attempt to fully correct the joint each step
            gear.MaxBias   = 1.2;     // but limit it's angular correction rate
            gear.MaxForce  = 50000.0; // emulate angular friction

            return(space);
        }
        public override Space LoadContent()
        {
            space                    = ChipmunkDemoGame.CreateSpace();
            space.Iterations         = 30;
            space.Gravity            = new Vect(0, -100);
            space.SleepTimeThreshold = 0.5f;
            space.CollisionSlop      = 0.5f;

            Body staticBody = space.StaticBody;

            Shape shape = new Segment(staticBody, new Vect(-320, -240), new Vect(-320, 240), 0.0f);

            space.AddShape(shape);

            shape.Elasticity = 1.0f;
            shape.Friction   = 1.0f;
            shape.Filter     = ChipmunkDemoGame.NotGrabbableFilter;

            shape = new Segment(staticBody, new Vect(320, -240), new Vect(320, 240), 0.0f);
            space.AddShape(shape);

            shape.Elasticity = 1.0f;
            shape.Friction   = 1.0f;
            shape.Filter     = ChipmunkDemoGame.NotGrabbableFilter;

            shape = new Segment(staticBody, new Vect(-320, -240), new Vect(320, -240), 0.0f);
            space.AddShape(shape);

            shape.Elasticity = 1.0f;
            shape.Friction   = 1.0f;
            shape.Filter     = ChipmunkDemoGame.NotGrabbableFilter;

            // Add lots of boxes.
            for (int i = 0; i < 14; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    var body = new Body(1.0f, Box.MomentForBox(1.0f, 30.0f, 30.0f));
                    space.AddBody(body);

                    body.Position = new Vect(j * 32 - i * 16, 300 - i * 32);
                    shape         = new Box(body, 30.0f, 30.0f, 0.5f);
                    space.AddShape(shape);

                    shape.Elasticity = 0.0f;
                    shape.Friction   = 0.8f;
                }
            }


            // Add a ball to make things more interesting
            double radius     = 15.0f;
            var    circleBody = new Body(10.0f, Circle.MomentForCircle(10.0f, 0.0f, radius, Vect.Zero));

            space.AddBody(circleBody);

            circleBody.Position = new Vect(0, -240 + radius + 5);

            shape = new Circle(circleBody, radius);
            space.AddShape(shape);

            shape.Elasticity = 0.0f;
            shape.Friction   = 0.9f;

            return(space);
        }
Example #8
0
        public override Space LoadContent()
        {
            space         = ChipmunkDemoGame.CreateSpace();
            space.Gravity = new Vect(0, -600);

            _kinematicBoxBody = new Body(BodyType.Kinematic);

            space.AddBody(_kinematicBoxBody);

            _kinematicBoxBody.AngularVelocity = 0.4;

            // Set up the static box.
            var a = new Vect(-200, -200);
            var b = new Vect(-200, 200);
            var c = new Vect(200, 200);
            var d = new Vect(200, -200);

            Shape shape = new Segment(_kinematicBoxBody, a, b, 0.0);

            space.AddShape(shape);
            shape.Elasticity = 1.0;
            shape.Friction   = 1.0;
            shape.Filter     = ChipmunkDemoGame.NotGrabbableFilter;

            shape = new Segment(_kinematicBoxBody, b, c, 0.0);
            space.AddShape(shape);
            shape.Elasticity = 1.0;
            shape.Friction   = 1.0;
            shape.Filter     = ChipmunkDemoGame.NotGrabbableFilter;

            shape = new Segment(_kinematicBoxBody, c, d, 0.0);
            space.AddShape(shape);
            shape.Elasticity = 1.0;
            shape.Friction   = 1.0;
            shape.Filter     = ChipmunkDemoGame.NotGrabbableFilter;

            shape = new Segment(_kinematicBoxBody, d, a, 0.0);
            space.AddShape(shape);
            shape.Elasticity = 1.0;
            shape.Friction   = 1.0;
            shape.Filter     = ChipmunkDemoGame.NotGrabbableFilter;

            double mass   = 1;
            double width  = 30;
            double height = width * 2;

            // Add the bricks.
            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    var pos = new Vect(i * width - 150, j * height - 150);

                    int type = random.Next(3000) / 1000;

                    if (type == 0)
                    {
                        AddBox(pos, mass, width, height);
                    }
                    else if (type == 1)
                    {
                        AddSegment(pos, mass, width, height);
                    }
                    else
                    {
                        AddCircle(pos + new Vect(0.0, (height - width) / 2.0), mass, width / 2.0);
                        AddCircle(pos + new Vect(0.0, (width - height) / 2.0), mass, width / 2.0);
                    }
                }
            }

            return(space);
        }
Example #9
0
 static void Main()
 {
     using (var game = new ChipmunkDemoGame())
         game.Run();
 }