Exemplo n.º 1
0
        static public cpSpace BouncyTerrainCircles_500(cpSpace space)
        {
            //cpSpace space = BENCH_SPACE_NEW();
            SetSubTitle("BouncyTerrainCircles 500");
            space.SetIterations(10);

            cpVect offset = new cpVect(-320, -240);

            for (int i = 0; i < (bouncy_terrain_verts.Length - 1); i++)
            {
                cpVect  a = bouncy_terrain_verts[i], b = bouncy_terrain_verts[i + 1];
                cpShape shape = space.AddShape(new cpSegmentShape(space.GetStaticBody(), cpVect.cpvadd(a, offset), cpVect.cpvadd(b, offset), 0.0f));
                shape.SetElasticity(1.0f);
            }

            for (int i = 0; i < 500; i++)
            {
                float  radius = 5.0f;
                float  mass   = radius * radius;
                cpBody body   = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero)));
                body.SetPosition(cpVect.cpvadd(cpVect.cpvmult(cp.frand_unit_circle(), 130.0f), cpVect.Zero));
                body.SetVelocity(cpVect.cpvmult(cp.frand_unit_circle(), 50.0f));

                cpShape shape = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero));
                shape.SetElasticity(1.0f);
            }

            return(space);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            NSApplication.Init();
            NSApplication.SharedApplication.ActivationPolicy = NSApplicationActivationPolicy.Regular;

            var xPos       = NSScreen.MainScreen.Frame.Width / 2;      // NSWidth([[window screen] frame])/ 2 - NSWidth([window frame])/ 2;
            var yPos       = NSScreen.MainScreen.Frame.Height / 2;     // NSHeight([[window screen] frame])/ 2 - NSHeight([window frame])/ 2;
            var mainWindow = new NSWindow(new CGRect(xPos, yPos, 600, 368), NSWindowStyle.Titled | NSWindowStyle.Resizable | NSWindowStyle.Closable, NSBackingStore.Buffered, false)
            {
                ContentView = new SpaceView()
            };

            var space = new cpSpace {
                CollisionEnabled = true
            };

            #region Scene

            var example = new Game1(space);
            example.Initialize(mainWindow.ContentView);
            example.StartAsync(loop: true);

            #endregion

            mainWindow.Title = "Example Debug Xamarin.Mac";

            //mainWindow.MakeKeyWindow();
            mainWindow.MakeKeyAndOrderFront(null);
            NSApplication.SharedApplication.ActivateIgnoringOtherApps(true);
            NSApplication.SharedApplication.Run();
            //mainWindow.Dispose();
        }
Exemplo n.º 3
0
 public SliceContext(cpVect a, cpVect b, cpSpace space)
 {
     // TODO: Complete member initialization
     this.a     = a;
     this.b     = b;
     this.space = space;
 }
Exemplo n.º 4
0
        void motor_preSolve(cpConstraint motor, cpSpace space)
        {
            float dt = space.GetCurrentTimeStep();

            float target_x = CCMouse.Instance.Position.x;

            paint = new shape
            {
                point1 = new cpVect(target_x, -1000.0f),
                point2 = new cpVect(target_x, 1000.0f),
            };

            float max_v      = 500.0f;
            float target_v   = cp.cpfclamp(cp.bias_coef(0.5f, dt / 1.2f) * (target_x - balance_body.GetPosition().x) / dt, -max_v, max_v);
            float error_v    = (target_v - balance_body.GetVelocity().x);
            float target_sin = 3.0e-3f * cp.bias_coef(0.1f, dt) * error_v / dt;

            float max_sin = cp.cpfsin(0.6f);

            balance_sin = cp.cpfclamp(balance_sin - 6.0e-5f * cp.bias_coef(0.2f, dt) * error_v / dt, -max_sin, max_sin);
            float target_a     = (float)Math.Asin(cp.cpfclamp(-target_sin + balance_sin, -max_sin, max_sin));
            float angular_diff = (float)Math.Asin(cpVect.cpvcross(balance_body.GetRotation(), cpVect.cpvforangle(target_a)));
            float target_w     = cp.bias_coef(0.1f, dt / 0.4f) * (angular_diff) / dt;

            float max_rate = 50.0f;
            float rate     = cp.cpfclamp(wheel_body.GetAngularVelocity() + balance_body.GetAngularVelocity() - target_w, -max_rate, max_rate);

            motor.SetRate(cp.cpfclamp(rate, -max_rate, max_rate));
            motor.SetMaxForce(8.0e4f);
        }
Exemplo n.º 5
0
cpSpaceActivateBody(cpSpace space, cpBody body)
{
	// cpAssertHard(!cpBodyIsRogue(body), "Internal error: Attempting to activate a rogue body.");
		
	if(space.locked){
		// cpSpaceActivateBody() is called again once the space is unlocked
		if(!cpArrayContains(space.rousedBodies, body)) cpArrayPush(space.rousedBodies, body);
	} else {
		// cpAssertSoft(body.node.root == null && body.node.next == null, "Internal error: Activating body non-null node pointers.");
		cpArrayPush(space.bodies, body);

		CP_BODY_FOREACH_SHAPE(body, shape){
			cpSpatialIndexRemove(space.staticShapes, shape, shape.hashid);
			cpSpatialIndexInsert(space.activeShapes, shape, shape.hashid);
		}
		
		CP_BODY_FOREACH_ARBITER(body, arb){
			cpBody bodyA = arb.body_a;
			
			// Arbiters are shared between two bodies that are always woken up together.
			// You only want to restore the arbiter once, so bodyA is arbitrarily chosen to own the arbiter.
			// The edge case is when static bodies are involved as the static bodies never actually sleep.
			// If the static body is bodyB then all is good. If the static body is bodyA, that can easily be checked.
			if(body == bodyA || cpBodyIsStatic(bodyA)){
				int numContacts = arb.numContacts;
				cpContact contacts = arb.contacts;
				
				// Restore contact values back to the space's contact buffer memory
				arb.contacts = cpContactBufferGetArray(space);
				memcpy(arb.contacts, contacts, numContacts*sizeof(cpContact));
				cpSpacePushContacts(space, numContacts);
				
				// Reinsert the arbiter into the arbiter cache
				cpShape a = arb.a, *b = arb.b;
				cpShape shape_pair[] = {a, b};
Exemplo n.º 6
0
        static public cpSpace ComplexTerrainCircles_1000(cpSpace space)
        {
            space.SetIterations(10);
            space.SetGravity(new cpVect(0, -100));
            space.SetCollisionSlop(0.5f);

            cpVect offset = new cpVect(-320, -240);

            for (int i = 0; i < (complex_terrain_verts.Length - 1); i++)
            {
                cpVect a = complex_terrain_verts[i], b = complex_terrain_verts[i + 1];
                space.AddShape(new cpSegmentShape(space.GetStaticBody(), cpVect.cpvadd(a, offset), cpVect.cpvadd(b, offset), 0.0f));
            }

            for (int i = 0; i < 1000; i++)
            {
                float  radius = 5.0f;
                float  mass   = radius * radius;
                cpBody body   = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero)));
                body.SetPosition(cpVect.cpvadd(cpVect.cpvmult(cp.frand_unit_circle(), 180.0f), new cpVect(0.0f, 300.0f)));
                cpShape shape = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero));
                shape.SetElasticity(0.0f); shape.SetFriction(0.0f);
            }

            return(space);
        }
Exemplo n.º 7
0
 public Game1(cpSpace space)
 {
     spc = space;
     spc.SetGravity(new cpVect(0, 1f));
     spc.AddDefaultCollisionHandler().preSolveFunc = new Func <cpArbiter, cpSpace, object, bool> ((arg1, arg2, arg3) => { Console.WriteLine("ij"); return(true); });
     spc.AddDefaultCollisionHandler().beginFunc    = new Func <cpArbiter, cpSpace, object, bool> ((arg1, arg2, arg3) => { Console.WriteLine("ij"); return(true); });
 }
Exemplo n.º 8
0
cpSpaceGetPostStepCallback(cpSpace space, object key)
{
	cpArray *arr = space.postStepCallbacks;
	for(int i=0; i<arr.num; i++){
		cpPostStepCallback *callback = (cpPostStepCallback *)arr.arr[i];
		if(callback && callback.key == key) return callback;
	}
	
	return null;
}
Exemplo n.º 9
0
 static public cpSpace SimpleTerrainBoxes_100(cpSpace space)
 {
     SetSubTitle("SimpleTerrainBoxes_100");
     space = SetupSpace_simpleTerrain(space);
     for (int i = 0; i < 100; i++)
     {
         add_box(space, i, 10.0f);
     }
     return(space);
 }
Exemplo n.º 10
0
 static public cpSpace SimpleTerrainVHexagons_200(cpSpace space)
 {
     SetSubTitle("SimpleTerrainVHexagons_200");
     space = SetupSpace_simpleTerrain(space);
     for (int i = 0; i < 200; i++)
     {
         add_hexagon(space, i, 5.0f * rand_size());
     }
     return(space);
 }
Exemplo n.º 11
0
 static public cpSpace SimpleTerrainHexagons_100(cpSpace space)
 {
     SetSubTitle("SimpleTerrainHexagons_100");
     space = SetupSpace_simpleTerrain(space);
     for (int i = 0; i < 100; i++)
     {
         add_hexagon(space, i, 5.0f);
     }
     return(space);
 }
Exemplo n.º 12
0
        public static void CollisionSeparateCallbackFunc(cpArbiter arb, cpSpace space, CCPhysicsWorld world)
        {
            CCPhysicsContact contact = (CCPhysicsContact)(arb.data);

            if (contact != null)
            {
                world.CollisionSeparateCallback(contact);
            }
            //delete contact;
        }
Exemplo n.º 13
0
 static public cpSpace SimpleTerrainCircles_500(cpSpace space)
 {
     SetSubTitle("SimpleTerrainCircles_500");
     space = SetupSpace_simpleTerrain(space);
     for (int i = 0; i < 500; i++)
     {
         add_circle(space, i, 5.0f);
     }
     return(space);
 }
Exemplo n.º 14
0
        static public void add_circle(cpSpace space, int index, float radius)
        {
            float  mass = radius * radius / 25.0f;
            cpBody body = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero)));

            //	cpBody body = cpSpaceAddBody(space, cpBodyInit(&bodies[i], mass, cpMomentForCircle(mass, 0.0f, radius, cpVect.Zero)));
            body.SetPosition(cpVect.cpvmult(cp.frand_unit_circle(), 180.0f));

            cpShape shape = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero));

            //	cpShape shape = cpSpaceAddShape(space, cpCircleShapeInit(&circles[i], body, radius, cpVect.Zero));
            shape.SetElasticity(0.0f); shape.SetFriction(0.9f);
        }
Exemplo n.º 15
0
        public void ClipPoly(cpSpace space, cpShape shp, cpVect n, float dist)
        {
            cpPolyShape shape = (cpPolyShape)shp;

            cpBody body = shape.GetBody();


            int count        = shape.Count;
            int clippedCount = 0;

            cpVect[] clipped = new cpVect[count + 1];

            for (int i = 0, j = count - 1; i < count; j = i, i++)
            {
                cpVect a      = body.LocalToWorld(shape.GetVert(j));
                float  a_dist = cpVect.cpvdot(a, n) - dist;

                if (a_dist < 0)
                {
                    clipped[clippedCount] = a;
                    clippedCount++;
                }

                cpVect b      = body.LocalToWorld(shape.GetVert(i));
                float  b_dist = cpVect.cpvdot(b, n) - dist;

                if (a_dist * b_dist < 0)
                {
                    float t = cp.cpfabs(a_dist) / (cp.cpfabs(a_dist) + cp.cpfabs(b_dist));

                    clipped[clippedCount] = cpVect.cpvlerp(a, b, t);
                    clippedCount++;
                }
            }

            cpVect centroid = cp.CentroidForPoly(clippedCount, clipped);
            float  mass     = cp.AreaForPoly(clippedCount, clipped, 0) * DENSITY;
            float  moment   = cp.MomentForPoly(mass, clippedCount, clipped, cpVect.cpvneg(centroid), 0);

            cpBody new_body = space.AddBody(new cpBody(mass, moment));

            new_body.SetPosition(centroid);
            new_body.SetVelocity(body.GetVelocityAtWorldPoint(centroid));
            new_body.SetAngularVelocity(body.GetAngularVelocity());

            cpTransform transform = cpTransform.Translate(cpVect.cpvneg(centroid));
            cpShape     new_shape = space.AddShape(new cpPolyShape(new_body, clippedCount, clipped, transform, 0));

            // Copy whatever properties you have set on the original shape that are important
            new_shape.SetFriction(shape.GetFriction());
        }
Exemplo n.º 16
0
        static public void add_box(cpSpace space, int index, float size)
        {
            float mass = size * size / 100.0f;

            cpBody body = space.AddBody(new cpBody(mass, cp.MomentForBox(mass, size, size)));

            //	cpBody body = cpSpaceAddBody(space, cpBodyInit(&bodies[i], mass, cpMomentForBox(mass, size, size)));
            body.SetPosition(cpVect.cpvmult(cp.frand_unit_circle(), 180.0f));
            cpPolyShape shape = space.AddShape(cpPolyShape.BoxShape(body, size - bevel * 2f, size - bevel * 2f, 0f)) as cpPolyShape;

            shape.SetRadius(bevel);
            shape.SetElasticity(0.0f);
            shape.SetFriction(0.9f);
        }
Exemplo n.º 17
0
        static cpSpace NoCollide(cpSpace space)
        {
            space.SetIterations(10);

            var handler = space.AddCollisionHandler(2, 2);

            //, (a, s, o) => NoCollide_begin(a, s, o), null, null, null);
            handler.beginFunc = NoCollide_begin;
            float radius     = 4.5f;
            var   staticBody = space.GetStaticBody();

            space.AddShape(new cpSegmentShape(staticBody, new cpVect(-330 - radius, -250 - radius), new cpVect(330 + radius, -250 - radius), 0.0f)).SetElasticity(1.0f);
            space.AddShape(new cpSegmentShape(staticBody, new cpVect(330 + radius, 250 + radius), new cpVect(330 + radius, -250 - radius), 0.0f)).SetElasticity(1.0f);
            space.AddShape(new cpSegmentShape(staticBody, new cpVect(330 + radius, 250 + radius), new cpVect(-330 - radius, 250 + radius), 0.0f)).SetElasticity(1.0f);
            space.AddShape(new cpSegmentShape(staticBody, new cpVect(-330 - radius, -250 - radius), new cpVect(-330 - radius, 250 + radius), 0.0f)).SetElasticity(1.0f);

            for (int x = -320; x <= 320; x += 20)
            {
                for (int y = -240; y <= 240; y += 20)
                {
                    space.AddShape(new cpCircleShape(staticBody, radius, new cpVect(x, y)));
                }
            }

            for (int y = 10 - 240; y <= 240; y += 40)
            {
                float  mass = 7.0f;
                cpBody body = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero)));
                body.SetPosition(new cpVect(-320.0f, y));
                body.SetVelocity(new cpVect(100.0f, 0.0f));

                cpShape shape = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero));
                shape.SetElasticity(1.0f);
                shape.SetCollisionType(2);
            }

            for (int x = 30 - 320; x <= 320; x += 40)
            {
                float  mass = 7.0f;
                cpBody body = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero)));

                body.SetPosition(new cpVect(x, -240.0f));
                body.SetVelocity(new cpVect(0.0f, 100.0f));

                cpShape shape = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero));
                shape.SetElasticity(1.0f);
                shape.SetCollisionType(2);
            }
            return(space);
        }
Exemplo n.º 18
0
        private static bool OnPreSolve(cpArbiter arbiter, cpSpace space, object userData)
        {
            var firstBlob  = arbiter.body_a.GetUserData() as Blob;
            var secondBlob = arbiter.body_b.GetUserData() as Blob;

            if (firstBlob == null || secondBlob == null)
            {
                return(true);
            }

            var firstCollisionResult  = firstBlob.OnCollision(secondBlob);
            var secondCollisionResult = secondBlob.OnCollision(firstBlob);

            return(firstCollisionResult || secondCollisionResult);
        }
Exemplo n.º 19
0
        /// <summary>
        /// Creates a copy of the registered world to run physics simulations in
        /// </summary>
        /// <param name="world">The world to create a copy of</param>
        public static void RegisterWorld(World world)
        {
            if (Worlds.ContainsKey(world))
            {
                throw new System.InvalidOperationException("Tried to register physics world when one is already registered!");
            }
            else
            {
                //Create the space
                cpSpace space = new cpSpace();
                space.SetGravity(new cpVect(0, 10));

                Worlds.Add(world, space);
            }
        }
Exemplo n.º 20
0
        public cpBody add_ball(cpSpace space, cpVect pos)
        {
            cpBody body = space.AddBody(new cpBody(1.0f, cp.MomentForCircle(1.0f, 30, 0, cpVect.Zero)));

            body.SetPosition(pos);

            cpShape shape = space.AddShape(new cpCircleShape(body, 30, cpVect.Zero));


            shape.SetElasticity(0.0f);
            shape.SetFriction(0.5f);


            return(body);
        }
Exemplo n.º 21
0
        static bool preSolve(cpArbiter arb, cpSpace space, object o)
        {
            cpShape a, b;

            arb.GetShapes(out a, out b);

            OneWayPlatform platform = (OneWayPlatform)a.userData;            // (OneWayPlatform*)cpShapeGetUserData(a);

            if (cpVect.cpvdot(arb.GetNormal(), platform.n) < 0)
            {
                arb.Ignore();                // cpArbiterIgnore(arb);
                return(false);
            }

            return(true);
        }
Exemplo n.º 22
0
        static public cpSpace SetupSpace_simpleTerrain(cpSpace space)
        {
            SetSubTitle("SetupSpace_simpleTerrain");
            space.SetIterations(10);
            space.SetGravity(new cpVect(0, -100));
            space.SetCollisionSlop(0.5f);

            cpVect offset = new cpVect(-320, -240);

            for (int i = 0; i < (simple_terrain_verts.Length - 1); i++)
            {
                cpVect a = simple_terrain_verts[i], b = simple_terrain_verts[i + 1];
                space.AddShape(new cpSegmentShape(space.GetStaticBody(), cpVect.cpvadd(a, offset), cpVect.cpvadd(b, offset), 0.0f));
            }
            return(space);
        }
Exemplo n.º 23
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.

            var space = new cpSpace {
                CollisionEnabled = true
            };

            #region Scene

            var example = new Game1(space);
            example.Initialize(View);
            example.StartAsync(loop: true);

            #endregion
        }
Exemplo n.º 24
0
        public void SliceShapePostStep(cpSpace space, cpShape shape, SliceContext context)
        {
            cpVect a = context.a;
            cpVect b = context.b;

            // Clipping plane normal and distance.
            cpVect n    = cpVect.cpvnormalize(cpVect.cpvperp(cpVect.cpvsub(b, a)));
            float  dist = cpVect.cpvdot(a, n);

            ClipPoly(space, shape, n, dist);
            ClipPoly(space, shape, cpVect.cpvneg(n), -dist);

            cpBody body = shape.GetBody();

            space.RemoveShape(shape);
            space.RemoveBody(body);
        }
Exemplo n.º 25
0
    private void update(cpSpace space, float dt)
    {
        bool jumpState = (key.y > 0.0f);

        if (jumpState && !lastJumpState && grounded)
        {
            float jump_v = cp.cpfsqrt((float)2.0 * JUMP_HEIGHT * GRAVITY);
            mPlayerBody.v = cpVect.cpvadd(mPlayerBody.v, new cpVect(0.0f, jump_v));

            remainingBoost = JUMP_BOOST_HEIGHT / jump_v;
        }

        space.Step(dt);

        remainingBoost -= dt;
        lastJumpState   = jumpState;
    }
Exemplo n.º 26
0
cpSpaceAddPostStepCallback(cpSpace space, cpPostStepFunc func, object key, object data)
{
	// cpAssertWarn(space.locked,
		"Adding a post-step callback when the space is not locked is unnecessary. "
		"Post-step callbacks will not called until the end of the next call to cpSpaceStep() or the next query.");
	
	if(!cpSpaceGetPostStepCallback(space, key)){
		cpPostStepCallback *callback = (cpPostStepCallback *)cpcalloc(1, sizeof(cpPostStepCallback));
		callback.func = (func ? func : PostStepDoNothing);
		callback.key = key;
		callback.data = data;
		
		cpArrayPush(space.postStepCallbacks, callback);
		return true;
	} else {
		return false;
	}
}
Exemplo n.º 27
0
        bool HookCrate(cpArbiter arb, cpSpace space, object data)
        {
            if (hookJoint == null)
            {
                // Get pointers to the two bodies in the collision pair and define local variables for them.
                // Their order matches the order of the collision types passed
                // to the collision handler this function was defined for
                cpBody hook  = arb.body_a;
                cpBody crate = arb.body_b;

                // additions and removals can't be done in a normal callback.
                // Schedule a post step callback to do it.
                // Use the hook as the key and pass along the arbiter.
                //cpSpaceAddPostStepCallback(space, (cpPostStepFunc)AttachHook, hook, crate);
                space.AddPostStepCallback((s, o1, o2) => AttachHook(o1 as cpBody, o2 as cpBody), hook, crate);
            }
            return(true); // return value is ignored for sensor callbacks anyway
        }
Exemplo n.º 28
0
        static public void add_hexagon(cpSpace space, int index, float radius)
        {
            cpVect[] hexagon = new cpVect[6];
            for (int i = 0; i < 6; i++)
            {
                float angle = -(float)Math.PI * 2.0f * i / 6.0f;
                hexagon[i] = cpVect.cpvmult(cpVect.cpv(cp.cpfcos(angle), cp.cpfsin(angle)), radius - bevel);
            }

            float mass = radius * radius;

            cpBody body = space.AddBody(new cpBody(mass, cp.MomentForPoly(mass, 6, hexagon, cpVect.Zero, 0.0f)));

            body.SetPosition(cpVect.cpvmult(cp.frand_unit_circle(), 180.0f));

            cpPolyShape shape = space.AddShape(new cpPolyShape(body, 6, hexagon, cpTransform.Identity, bevel)) as cpPolyShape;

            shape.SetElasticity(0.0f);
            shape.SetFriction(0.9f);
        }
Exemplo n.º 29
0
        public static bool CollisionBeginCallbackFunc(cpArbiter arb, cpSpace space, CCPhysicsWorld world)
        {
            cpShape a, b;

            arb.GetShapes(out a, out b);

            CCPhysicsShapeInfo ita = null, itb = null;

            cp.AssertWarn(CCPhysicsShapeInfo.Map.TryGetValue(a, out ita) && CCPhysicsShapeInfo.Map.TryGetValue(b, out itb));
            if (a != null || b != null)
            {
                return(false);
            }

            CCPhysicsContact contact = new CCPhysicsContact(ita.getShape(), itb.getShape());

            arb.data             = contact;
            contact._contactInfo = arb;

            return(world.CollisionBeginCallback(contact));
        }
Exemplo n.º 30
0
    static void Main(string[] args)
    {
        cpSpace space = new cpSpace();

        space.SetGravity(new cpVect(0, -100));

        cpShape shape = cpShape.NewSegment(space.StaticBody, new cpVect(-20, 5), new cpVect(20, -5), 0);

        shape.SetFriction(1);

        space.AddShape(shape);

        double radius = 5;
        double mass   = 1;
        double moment = cpUtil.MomentForCircle(mass, 0, radius, cpVect.Zero);

        cpBody ballBody = new cpBody(mass, moment);

        space.AddBody(ballBody);
        ballBody.Position = new cpVect(0, 15);

        cpShape ballShape = cpShape.NewCircle(ballBody, radius, cpVect.Zero);

        space.AddShape(ballShape);
        ballShape.SetFriction(0.7);

        double timeStep = 1 / 60d;

        for (double time = 0; time < 2; time += timeStep)
        {
            cpVect pos = ballBody.Position;
            cpVect vel = ballBody.Velocity;
            Console.WriteLine("Time is {0:F2}. ballBody is at ({1:F2}, {2:F2}). It's velocity is ({3:F2}, {4:F2})",
                              time, pos.X, pos.Y, vel.X, vel.Y
                              );
            space.Step(timeStep);
        }

        Console.ReadKey();
    }
Exemplo n.º 31
0
        public PhysicObject(UIImage firstText, int radius, cpSpace space, bool isKinematic = false) : base(firstText)
        {
            trsf = new cpTransform();
            collCount++;
            physic = new cpBody(rnd.Next(500, 1000), cp.PHYSICS_INFINITY);

            if (isKinematic)
            {
                physic.SetBodyType(cpBodyType.KINEMATIC);
            }
            shp = new cpCircleShape(physic, radius, cpVect.Zero);
            shp.Active();
            shp.SetSensor(true);
            shp.SetCollisionType(1);
            physic.SetPosition(new cpVect((float)Frame.Location.X, (float)Frame.Location.Y));
            if (space != null)
            {
                space.AddBody(physic);
                space.AddShape(shp);
                this.space = space;
            }
        }
Exemplo n.º 32
0
        public void Start(int worldSize)
        {
            lock (UpdateLock)
            {
                _worldSize = worldSize;

                _space = new cpSpace {
                    CollisionEnabled = true
                };
                _space.SetGravity(cpVect.Zero);

                var boxBody = cpBody.NewStatic();

                var leftShape   = new cpSegmentShape(boxBody, new cpVect(-worldSize, -worldSize), new cpVect(-worldSize, worldSize), 5);
                var topShape    = new cpSegmentShape(boxBody, new cpVect(-worldSize, worldSize), new cpVect(worldSize, worldSize), 5);
                var rightShape  = new cpSegmentShape(boxBody, new cpVect(worldSize, worldSize), new cpVect(worldSize, -worldSize), 5);
                var bottomShape = new cpSegmentShape(boxBody, new cpVect(worldSize, -worldSize), new cpVect(-worldSize, -worldSize), 5);

                leftShape.SetCollisionType(GenerateCollisionType <cpSpace>());
                topShape.SetCollisionType(GenerateCollisionType <cpSpace>());
                rightShape.SetCollisionType(GenerateCollisionType <cpSpace>());
                bottomShape.SetCollisionType(GenerateCollisionType <cpSpace>());

                _space.AddShape(leftShape);
                _space.AddShape(topShape);
                _space.AddShape(rightShape);
                _space.AddShape(bottomShape);

                var defaultCollisionHandler = _space.AddDefaultCollisionHandler();
                defaultCollisionHandler.preSolveFunc = (arbiter, space, userData) => false;

                var playerCollisionHandler = _space.AddCollisionHandler(GenerateCollisionType <PlayerBlob>(), GenerateCollisionType <PlayerBlob>());
                playerCollisionHandler.preSolveFunc = OnPreSolve;

                _reindexStatic = true;
            }
        }
Exemplo n.º 33
0
cpSpaceUnlock(cpSpace space, bool runPostStep)
{
	space.locked--;
	// cpAssertHard(space.locked >= 0, "Internal Error: Space lock underflow.");
	
	if(space.locked == 0){
		cpArray *waking = space.rousedBodies;
		
		for(int i=0, count=waking.num; i<count; i++){
			cpSpaceActivateBody(space, (cpBody )waking.arr[i]);
			waking.arr[i] = null;
		}
		
		waking.num = 0;
		
		if(space.locked == 0 && runPostStep && !space.skipPostStep){
			space.skipPostStep = true;
			
			cpArray *arr = space.postStepCallbacks;
			for(int i=0; i<arr.num; i++){
				cpPostStepCallback *callback = (cpPostStepCallback *)arr.arr[i];
				cpPostStepFunc func = callback.func;
				
				// Mark the func as null in case calling it calls cpSpaceRunPostStepCallbacks() again.
				// TODO need more tests around this case I think.
				callback.func = null;
				if(func) func(space, callback.key, callback.data);
				
				arr.arr[i] = null;
				cpfree(callback);
			}
			
			arr.num = 0;
			space.skipPostStep = false;
		}
	}
}
Exemplo n.º 34
0
        static public cpSpace BouncyTerrainHexagons_500(cpSpace space)
        {
            SetSubTitle("BouncyTerrainHexagons 500");
            //cpSpace space = BENCH_SPACE_NEW();
            space.SetIterations(10);

            cpVect offset = new cpVect(-320, -240);

            for (int i = 0; i < (bouncy_terrain_verts.Length - 1); i++)
            {
                cpVect  a = bouncy_terrain_verts[i], b = bouncy_terrain_verts[i + 1];
                cpShape shape = space.AddShape(new cpSegmentShape(space.GetStaticBody(), cpVect.cpvadd(a, offset), cpVect.cpvadd(b, offset), 0.0f));
                shape.SetElasticity(1.0f);
            }

            float radius = 5.0f;

            cpVect[] hexagon = new cpVect[6];
            for (int i = 0; i < 6; i++)
            {
                float angle = -(float)Math.PI * 2.0f * i / 6.0f;
                hexagon[i] = cpVect.cpvmult(cpVect.cpv(cp.cpfcos(angle), cp.cpfsin(angle)), radius - bevel);
            }

            for (int i = 0; i < 500; i++)
            {
                float  mass = radius * radius;
                cpBody body = space.AddBody(new cpBody(mass, cp.MomentForPoly(mass, 6, hexagon, cpVect.Zero, 0.0f)));
                body.SetPosition(cpVect.cpvadd(cpVect.cpvmult(cp.frand_unit_circle(), 130.0f), cpVect.Zero));
                body.SetVelocity(cpVect.cpvmult(cp.frand_unit_circle(), 50.0f));

                cpShape shape = space.AddShape(new cpPolyShape(body, 6, hexagon, cpTransform.Identity, bevel));
                shape.SetElasticity(1.0f);
            }

            return(space);
        }
Exemplo n.º 35
0
cpSpaceInit(cpSpace space)
{
Exemplo n.º 36
0
cpSpacePointQuery(cpSpace space, cpVect point, cpLayers layers, cpGroup group, cpSpacePointQueryFunc func, object data)
{
Exemplo n.º 37
0
static void PostStepDoNothing(cpSpace space, object obj, object data){}
Exemplo n.º 38
0
//MARK: Misc Helper Funcs

// Default collision functions.
static bool alwaysCollide(cpArbiter arb, cpSpace space, object data){return 1;}
Exemplo n.º 39
0
cpSpaceLock(cpSpace space)
{
	space.locked++;
}
Exemplo n.º 40
0
static void nothing(cpArbiter arb, cpSpace space, object data){}