Ejemplo n.º 1
0
        private bool OnContact(RigidBody body1, RigidBody body2, JVector point1, JVector point2, JVector normal,
                               JVector[] triangle, float penetration)
        {
            // By design, all physics objects have entities attached, with the exception of static parts of the map. In
            // the case of map collisions, it's unknown which body comes first as an argument (as of writing this
            // comment, anyway), which is why both entities are checked for null.
            Entity entity1 = body1.Tag as Entity;
            Entity entity2 = body2.Tag as Entity;

            vec3 p1 = point1.ToVec3();
            vec3 p2 = point2.ToVec3();

            // The normal needs to be flipped based on how Jitter handles triangle winding.
            //vec3 n = Utilities.Normalize(-normal.ToVec3());
            vec3 n = Utilities.Normalize(normal.ToVec3());

            // A triangle will only be given in the case of collisions with a triangle mesh (or terrain).
            if (triangle != null)
            {
                var entity = entity1 ?? entity2;
                var point  = entity1 != null ? p2 : p1;
                var tArray = triangle.Select(t => t.ToVec3()).ToArray();

                return(entity.OnContact(point, n, tArray));
            }

            bool b1 = entity1?.OnContact(entity2, body2, p1, -n) ?? true;
            bool b2 = entity2?.OnContact(entity1, body1, p2, n) ?? true;

            // Either entity can negate the contact.
            return(b1 && b2);
        }
Ejemplo n.º 2
0
        private static bool RaycastInternal(World world, RigidBody body, vec3 start, vec3 ray,
                                            out RaycastResults results)
        {
            JVector jStart = start.ToJVector();

            // Note that Jitter's Raycast signature below calls one of its parameters "rayDirection", but that vector
            // isn't meant to be normalized (meaning that it's actually just a ray from start to end).
            JVector jDirection = ray.ToJVector();
            JVector normal;

            JVector[] triangle;

            var system = world.CollisionSystem;

            float fraction;

            bool success = body != null
                                ? system.Raycast(body, jStart, jDirection, out normal, out fraction, out triangle)
                                : system.Raycast(jStart, jDirection, (b, n, f) => true, out body, out normal,
                                                 out fraction, out triangle);

            if (!success || fraction > 1)
            {
                results = null;

                return(false);
            }

            vec3[] tVectors = null;

            // Triangle will only be set if a triangle mesh was hit.
            if (triangle != null)
            {
                tVectors = new vec3[3];

                for (int i = 0; i < 3; i++)
                {
                    tVectors[i] = triangle[i].ToVec3();
                }
            }

            results = new RaycastResults(body, start + jDirection.ToVec3() * fraction,
                                         Utilities.Normalize(normal.ToVec3()), tVectors);

            return(true);
        }