예제 #1
0
        /// Test if a point lies within a shape.
        public static cpContactPointSet Collide(cpShape a, cpShape b, ref List <cpContact> contacts)
        {
            //cpContact[] contacts = new cpContact[cpArbiter.CP_MAX_CONTACTS_PER_ARBITER];
            cpCollisionInfo info = cpCollision.cpCollide(a, b, 0, ref contacts);

            cpContactPointSet set = new cpContactPointSet();

            set.count = info.count;

            set.points = new PointsDistance[set.count];

            // cpCollideShapes() may have swapped the contact order. Flip the normal.
            bool swapped = (a != info.a);

            set.normal = (swapped ? cpVect.cpvneg(info.n) : info.n);

            for (int i = 0; i < info.count; i++)
            {
                cpVect p1 = contacts[i].r1;
                cpVect p2 = contacts[i].r2;

                set.points[i]          = new PointsDistance();
                set.points[i].pointA   = (swapped ? p2 : p1);
                set.points[i].pointB   = (swapped ? p1 : p2);
                set.points[i].distance = cpVect.cpvdot(cpVect.cpvsub(p2, p1), set.normal);
            }

            return(set);
        }
 public static void InfoPushContact(ref cpCollisionInfo info, cpVect p1, cpVect p2, ulong hash)
 {
     cp.AssertSoft(info.count <= cpArbiter.CP_MAX_CONTACTS_PER_ARBITER, "Internal error: Tried to push too many contacts.");
     info.arr.Add(new cpContact(p1, p2, hash));
     //info.arr[info.count] = ;
     info.count++;            // = count++;
 }
예제 #3
0
        public ulong CollideShapes(cpShape a, cpShape b, ulong id)
        {
            // It would be nicer to use .bind() or something, but this is faster.
            //return new Action<object, object>((obj1, obj2) =>
            //{// Reject any of the simple cases
            if (QueryReject(a, b))
            {
                return(id);
            }

            //contactsBuffer.Clear();

            List <cpContact> contacts = new List <cpContact>();

            // Narrow-phase collision detection.
            //int numContacts = cpCollideShapes(a, b, contacts);
            cpCollisionInfo info = cpCollision.cpCollide(a, b, id, ref contacts);

            if (info.count == 0)
            {
                return(info.id);                // Shapes are not colliding.
            }
            // Get an arbiter from space.arbiterSet for the two shapes.
            // This is where the persistant contact magic comes from.
            var arbHash = cp.CP_HASH_PAIR(info.a.hashid, info.b.hashid);

            cpArbiter arb;

            if (!cachedArbiters.TryGetValue(arbHash, out arb))
            {
                arb = new cpArbiter(a, b);
                cachedArbiters.Add(arbHash, arb);
            }

            arb.Update(info, this);

            cpCollisionHandler handler = arb.handler;              //LookupHandler(a.type, b.type, defaultHandler);


            // Call the begin function first if it's the first step
            if (arb.state == cpArbiterState.FirstCollision && !handler.beginFunc(arb, this, null))
            {
                arb.Ignore();                 // permanently ignore the collision until separation
            }

            if (
                // Ignore the arbiter if it has been flagged
                (arb.state != cpArbiterState.Ignore) &&
                // Call preSolve
                handler.preSolveFunc(arb, this, handler.userData) &&
                !(a.sensor || b.sensor) &&
                // Process, but don't add collisions for sensors.
                !(a.body.m == cp.Infinity && b.body.m == cp.Infinity)
                )
            {
                this.arbiters.Add(arb);
            }
            else
            {
                //cpSpacePopContacts(space, numContacts);

                arb.contacts.Clear();

                // Normally arbiters are set as used after calling the post-solve callback.
                // However, post-solve callbacks are not called for sensors or arbiters rejected from pre-solve.
                if (arb.state != cpArbiterState.Ignore)
                {
                    arb.state = cpArbiterState.Normal;
                }
            }

            // Time stamp the arbiter so we know it was used recently.
            arb.stamp = this.stamp;
            //	});
            return(info.id);
        }
예제 #4
0
        public void Update(cpCollisionInfo info, cpSpace space)
        {
            cpShape a = info.a, b = info.b;

            // For collisions between two similar primitive types, the order could have been swapped since the last frame.
            this.a = a; this.body_a = a.body;
            this.b = b; this.body_b = b.body;

            // Iterate over the possible pairs to look for hash value matches.
            for (int i = 0; i < info.count; i++)
            {
                cpContact con = info.arr[i];

                // r1 and r2 store absolute offsets at init time.
                // Need to convert them to relative offsets.
                con.r1 = cpVect.cpvsub(con.r1, a.body.p);
                con.r2 = cpVect.cpvsub(con.r2, b.body.p);

                // Cached impulses are not zeroed at init time.
                con.jnAcc = con.jtAcc = 0.0f;

                for (int j = 0; j < this.Count; j++)
                {
                    cpContact old = this.contacts[j];

                    // This could trigger false positives, but is fairly unlikely nor serious if it does.
                    if (con.hash == old.hash)
                    {
                        // Copy the persistant contact information.
                        con.jnAcc = old.jnAcc;
                        con.jtAcc = old.jtAcc;
                    }
                }
            }
            //TODO: revise
            this.contacts = info.arr;
            //this.count = info.count;
            this.n = info.n;

            this.e = a.e * b.e;
            this.u = a.u * b.u;

            cpVect surface_vr = cpVect.cpvsub(b.surfaceV, a.surfaceV);

            this.surface_vr = cpVect.cpvsub(surface_vr, cpVect.cpvmult(info.n, cpVect.cpvdot(surface_vr, info.n)));

            ulong typeA = info.a.type, typeB = info.b.type;
            cpCollisionHandler defaultHandler = space.defaultHandler;
            cpCollisionHandler handler        = this.handler = space.LookupHandler(typeA, typeB, defaultHandler);

            // Check if the types match, but don't swap for a default handler which use the wildcard for type A.
            bool swapped = this.swapped = (typeA != handler.typeA && handler.typeA != cp.WILDCARD_COLLISION_TYPE);

            if (handler != defaultHandler || space.usesWildcards)
            {
                // The order of the main handler swaps the wildcard handlers too. Uffda.
                this.handlerA = space.LookupHandler(swapped ? typeB : typeA, cp.WILDCARD_COLLISION_TYPE, cpCollisionHandler.cpCollisionHandlerDoNothing);
                this.handlerB = space.LookupHandler(swapped ? typeA : typeB, cp.WILDCARD_COLLISION_TYPE, cpCollisionHandler.cpCollisionHandlerDoNothing);
            }

            // mark it as new if it's been cached
            if (this.state == cpArbiterState.Cached)
            {
                this.state = cpArbiterState.FirstCollision;
            }
        }