예제 #1
0
        public void ShatterShape(cpPolyShape shape, float cellSize, cpVect focus)
        {
            space.RemoveShape(shape);
            space.RemoveBody(shape.GetBody());

            cpBB bb     = shape.bb;
            int  width  = (int)((bb.r - bb.l) / cellSize) + 1;
            int  height = (int)((bb.t - bb.b) / cellSize) + 1;
            //	printf("Splitting as %dx%d\n", width, height);
            WorleyContex context = new WorleyContex((int)RandomHelper.frand(), cellSize, width, height, bb, focus);

            for (int i = 0; i < context.width; i++)
            {
                for (int j = 0; j < context.height; j++)
                {
                    cpVect cell = WorleyPoint(i, j, ref context);

                    cpPointQueryInfo cp = null;
                    if (shape.PointQuery(cell, ref cp) < 0.0f)
                    {
                        ShatterCell(shape, cell, i, j, ref context);
                    }
                }
            }

            //cpBodyFree(cpShapeGetBody(shape));
            //cpShapeFree(shape);
        }
예제 #2
0
        public CCPhysicsSprite addGrossiniAtPosition(CCPoint location)
        {
            int posx, posy;

            posx = CCRandom.Next() * 200;
            posy = CCRandom.Next() * 200;

            posx = (Math.Abs(posx) % 4) * 85;
            posy = (Math.Abs(posy) % 3) * 121;

            CCPhysicsSprite sp = new CCPhysicsSprite(spriteTexture.Texture, new CCRect(posx, posy, 85, 121));

            cpBB verts = new cpBB(-24, -54, 24, 54);

            var body = new cpBody(1, cp.MomentForBox2(1, new cpBB(-24, -54, 24, 54)));             //);

            body.SetPosition(new cpVect(posx, posy));
            space.AddBody(body);

            var shape = cpPolyShape.BoxShape2(body, verts, 0);

            shape.e = .5f;
            shape.u = .5f;
            space.AddShape(shape);

            sp.Body = body;

            AddChild(sp);
            sp.Position = location;


            return(sp);
        }
예제 #3
0
 public static cpTransform Ortho(cpBB bb)
 {
     return(NewTranspose(
                2.0f / (bb.r - bb.l), 0.0f, -(bb.r + bb.l) / (bb.r - bb.l),
                0.0f, 2.0f / (bb.t - bb.b), -(bb.t + bb.b) / (bb.t - bb.b)
                ));
 }
예제 #4
0
 public void Draw(cpBB bb, cpColor color)
 {
     DrawPolygon(new cpVect[] {
         new cpVect(bb.r, bb.b),
         new cpVect(bb.r, bb.t),
         new cpVect(bb.l, bb.t),
         new cpVect(bb.l, bb.b)
     }, 4, color);
 }
예제 #5
0
 public WorleyContex(int seed, float cellSize, int width, int height, cpBB bb, cpVect focus)
 {
     // TODO: Complete member initialization
     this.seed     = seed;
     this.cellSize = cellSize;
     this.width    = width;
     this.height   = height;
     this.bb       = bb;
     this.focus    = focus;
 }
예제 #6
0
        cpBBWrapVect(cpBB bb, cpVect v)
        {
            double ix = System.Math.Abs(bb.r - bb.l);
            double modx = System.Math.Mod(v.x - bb.l, ix);
            double x = (modx > 0.0f) ? modx : modx + ix;

            double iy = System.Math.Abs(bb.t - bb.b);
            double mody = System.Math.Mod(v.y - bb.b, iy);
            double y = (mody > 0.0f) ? mody : mody + iy;

            return cpv(x + bb.l, y + bb.b);
        }
예제 #7
0
        /// Transform a cpBB.
        public static cpBB BB(cpTransform t, cpBB bb)
        {
            cpVect center = cpBB.Center(bb);
            float  hw     = (bb.r - bb.l) * 0.5f;
            float  hh     = (bb.t - bb.b) * 0.5f;

            float a = t.a * hw, b = t.c * hh, d = t.b * hw, e = t.d * hh;
            float hw_max = cp.cpfmax(cp.cpfabs(a + b), cp.cpfabs(a - b));
            float hh_max = cp.cpfmax(cp.cpfabs(d + e), cp.cpfabs(d - e));

            return(cpBB.NewForExtents(Point(t, center), hw_max, hh_max));
        }
예제 #8
0
        cpVect WorleyPoint(int i, int j, ref WorleyContex context)
        {
            float size   = context.cellSize;
            int   width  = context.width;
            int   height = context.height;
            cpBB  bb     = context.bb;

            //	cpVect fv = cpv(0.5, 0.5);
            cpVect fv = HashVect(i, j, context.seed);

            return(new cpVect(
                       cp.cpflerp(bb.l, bb.r, 0.5f) + size * (i + fv.x - width * 0.5f),
                       cp.cpflerp(bb.b, bb.t, 0.5f) + size * (j + fv.y - height * 0.5f)
                       ));
        }
예제 #9
0
        public override void Update(float dt)
        {
            base.Update(dt);

            space.Step(dt);

            if (CCMouse.Instance.rightclick || CCMouse.Instance.dblclick)
            {
                cpPointQueryInfo info = null;
                if (space.PointQueryNearest(CCMouse.Instance.Position, 0, GRAB_FILTER, ref info) != null)
                {
                    cpBB  bb        = info.shape.GetBB();            // cpShapeGetBB();
                    float cell_size = cp.cpfmax(bb.r - bb.l, bb.t - bb.b) / 5.0f;
                    if (cell_size > 5)
                    {
                        ShatterShape(info.shape as cpPolyShape, cell_size, CCMouse.Instance.Position);
                    }
                    else
                    {
                        //printf("Too small to splinter %f\n", cell_size);
                    }
                }
            }
        }
예제 #10
0
cpMomentForBox2(double m, cpBB box)
{
	double width = box.r - box.l;
	double height = box.t - box.b;
	cpVect offset = cpVect.Multiply(cpv(box.l + box.r, box.b + box.t), 0.5f);
	
	// TODO NaN when offset is 0 and m is double.PositiveInfinity
	return cpMomentForBox(m, width, height) + m*offset.LengthSQ;
}
예제 #11
0
 cpBoxShapeNew2(cpBody body, cpBB box)
 {
     return (cpShape)cpBoxShapeInit2(new cpPolyShape(), body, box);
 }
예제 #12
0
        cpBoxShapeInit2(cpPolyShape poly, cpBody body, cpBB box)
        {
            cpVect[] verts = new cpVect[] {
		cpv(box.l, box.b),
		cpv(box.l, box.t),
		cpv(box.r, box.t),
		cpv(box.r, box.b)
	};

            return cpPolyShapeInit(poly, body, 4, verts, cpvzero);
        }
예제 #13
0
 public void Draw(cpBB bb)
 {
     Draw(bb, cpColor.CyanBlue);
 }
예제 #14
0
 public override void DrawBB(cpBB bb, cpColor color)
 {
 }
예제 #15
0
        public override void OnEnter()
        {
            base.OnEnter();

            SetSubTitle("This unicycle is completely driven and balanced by a single cpSimpleMotor.\nMove the mouse to make the unicycle follow it.");

            space.SetIterations(30);
            space.SetGravity(new cpVect(0, -500));

            {
                cpShape shape      = null;
                cpBody  staticBody = space.GetStaticBody();

                shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-3200, -240), new cpVect(3200, -240), 0.0f));
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);
                shape.SetFilter(NOT_GRABBABLE_FILTER);

                shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(0, -200), new cpVect(240, -240), 0.0f));
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);
                shape.SetFilter(NOT_GRABBABLE_FILTER);

                shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-240, -240), new cpVect(0, -200), 0.0f));
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);
                shape.SetFilter(NOT_GRABBABLE_FILTER);
            }


            {
                float radius = 20.0f;
                float mass   = 1.0f;

                float moment = cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero);

                wheel_body = space.AddBody(new cpBody(mass, moment));
                wheel_body.SetPosition(new cpVect(0.0f, -160.0f + radius));

                cpShape shape = space.AddShape(new cpCircleShape(wheel_body, radius, cpVect.Zero));
                shape.SetFriction(0.7f);
                shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES));
            }

            {
                float cog_offset = 30.0f;

                cpBB bb1 = new cpBB(-5.0f, 0.0f - cog_offset, 5.0f, cog_offset * 1.2f - cog_offset);
                cpBB bb2 = new cpBB(-25.0f, bb1.t, 25.0f, bb1.t + 10.0f);

                float mass   = 3.0f;
                float moment = cp.MomentForBox2(mass, bb1) + cp.MomentForBox2(mass, bb2);

                balance_body = space.AddBody(new cpBody(mass, moment));
                balance_body.SetPosition(new cpVect(0.0f, wheel_body.GetPosition().y + cog_offset));

                cpShape shape = null;

                shape = space.AddShape(cpPolyShape.BoxShape2(balance_body, bb1, 0.0f));
                shape.SetFriction(1.0f);
                shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES));

                shape = space.AddShape(cpPolyShape.BoxShape2(balance_body, bb2, 0.0f));
                shape.SetFriction(1.0f);
                shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES));
            }

            cpVect anchorA  = balance_body.WorldToLocal(wheel_body.GetPosition());
            cpVect groove_a = cpVect.cpvadd(anchorA, new cpVect(0.0f, 30.0f));
            cpVect groove_b = cpVect.cpvadd(anchorA, new cpVect(0.0f, -10.0f));

            space.AddConstraint(new cpGrooveJoint(balance_body, wheel_body, groove_a, groove_b, cpVect.Zero));
            space.AddConstraint(new cpDampedSpring(balance_body, wheel_body, anchorA, cpVect.Zero, 0.0f, 6.0e2f, 30.0f));

            motor = space.AddConstraint(new cpSimpleMotor(wheel_body, balance_body, 0.0f));
            motor.SetPreSolveFunc((s) => motor_preSolve(motor, s));

            {
                float width  = 100.0f;
                float height = 20.0f;
                float mass   = 3.0f;

                cpBody boxBody = space.AddBody(new cpBody(mass, cp.MomentForBox(mass, width, height)));
                boxBody.SetPosition(new cpVect(200, -100));

                cpShape shape = space.AddShape(cpPolyShape.BoxShape(boxBody, width, height, 0.0f));
                shape.SetFriction(0.7f);
            }


            Schedule();
        }
예제 #16
0
hashHandle(cpSpaceHash *hash, cpHandle *hand, cpBB bb)
{
	// Find the dimensions in cell coordinates.
	double dim = hash.celldim;
	int l = floor_int(bb.l/dim); // Fix by ShiftZ
	int r = floor_int(bb.r/dim);
	int b = floor_int(bb.b/dim);
	int t = floor_int(bb.t/dim);
	
	int n = hash.numcells;
	for(int i=l; i<=r; i++){
		for(int j=b; j<=t; j++){
			cpHashValue idx = hash_func(i,j,n);
			cpSpaceHashBin *bin = hash.table[idx];
			
			// Don't add an object twice to the same cell.
			if(containsHandle(bin, hand)) continue;

			cpHandleRetain(hand);
			// Insert a new bin for the handle in this cell.
			cpSpaceHashBin *newBin = getEmptyBin(hash);
			newBin.handle = hand;
			newBin.next = bin;
			hash.table[idx] = newBin;
		}
	}
}
예제 #17
0
 public static CCRect cpbb2rect(cpBB bb)
 {
     return(new CCRect((float)bb.l, (float)bb.b, (float)(bb.r - bb.l), (float)(bb.t - bb.b)));
 }
예제 #18
0
        public override void OnEnter()
        {
            base.OnEnter();
            space.SetIterations(30);
            space.SetGravity(new cpVect(0, -500));
            space.SetSleepTimeThreshold(0.5f);
            space.SetCollisionSlop(0.5f);
            var staticBody = space.GetStaticBody();

            // Create segments around the edge of the screen.
            var shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-320, -240), new cpVect(-320, 240), 0.0f));

            shape.SetElasticity(1.0f);
            shape.SetFriction(1.0f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(320, -240), new cpVect(320, 240), 0.0f));
            shape.SetElasticity(1.0f);
            shape.SetFriction(1.0f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-320, -240), new cpVect(320, -240), 0.0f));
            shape.SetElasticity(1.0f);
            shape.SetFriction(1.0f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-320, 240), new cpVect(320, 240), 0.0f));
            shape.SetElasticity(1.0f);
            shape.SetFriction(1.0f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            {
                // Add the edges of the bucket
                var bb     = new cpBB(-300, -200, 100, 0);
                var radius = 5.0f;

                shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(bb.l, bb.b), new cpVect(bb.l, bb.t), radius));
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);
                shape.SetFilter(NOT_GRABBABLE_FILTER);

                shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(bb.r, bb.b), new cpVect(bb.r, bb.t), radius));
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);
                shape.SetFilter(NOT_GRABBABLE_FILTER);

                shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(bb.l, bb.b), new cpVect(bb.r, bb.b), radius));
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);
                shape.SetFilter(NOT_GRABBABLE_FILTER);

                // Add the sensor for the water.
                shape = space.AddShape(cpPolyShape.BoxShape2(staticBody, bb, 0.0f));
                shape.SetSensor(true);
                shape.SetCollisionType(1);
            }
            {
                float width  = 200.0f;
                float height = 50.0f;
                float mass   = 0.3f * FLUID_DENSITY * width * height;
                var   moment = cp.MomentForBox(mass, width, height);

                cpBody body = space.AddBody(new cpBody(mass, moment));
                body.SetPosition(new cpVect(-50, -100));
                body.SetVelocity(new cpVect(0, -100));
                body.SetAngularVelocity(1);

                shape = space.AddShape(cpPolyShape.BoxShape(body, width, height, 0.0f));
                shape.SetFriction(0.8f);
            }

            {
                float width  = 40.0f;
                float height = width * 2;
                float mass   = 0.3f * FLUID_DENSITY * width * height;
                float moment = cp.MomentForBox(mass, width, height);

                cpBody body = space.AddBody(new cpBody(mass, moment));
                body.SetPosition(new cpVect(-200, -50));
                body.SetVelocity(new cpVect(0, -100));
                body.SetAngularVelocity(1);

                shape = space.AddShape(cpPolyShape.BoxShape(body, width, height, 0.0f));
                shape.SetFriction(0.8f);
            }

            cpCollisionHandler handler = space.AddCollisionHandler(1, 0);

            handler.preSolveFunc = waterPreSolve;
            Schedule();
        }