Ejemplo n.º 1
0
        public static CollisionHull CreateCylinder(WorldBase world, int shapeID, double radius, double height, Matrix3D?offsetMatrix)
        {
            float[] newtOffsetMatrix = null;            // null means no offset
            if (offsetMatrix != null)
            {
                newtOffsetMatrix = new NewtonMatrix(offsetMatrix.Value).Matrix;
            }

            IntPtr handle = Newton.NewtonCreateCylinder(world.Handle, Convert.ToSingle(radius), Convert.ToSingle(height), shapeID, newtOffsetMatrix);

            return(new CollisionHull(handle, world, newtOffsetMatrix, CollisionShapeType.Cylinder));
        }
Ejemplo n.º 2
0
        public static CollisionHull CreateBox(WorldBase world, int shapeID, Vector3D size, Matrix3D?offsetMatrix)
        {
            float[] newtOffsetMatrix = null;            // null means no offset
            if (offsetMatrix != null)
            {
                newtOffsetMatrix = new NewtonMatrix(offsetMatrix.Value).Matrix;
            }

            IntPtr handle = Newton.NewtonCreateBox(world.Handle, Convert.ToSingle(size.X), Convert.ToSingle(size.Y), Convert.ToSingle(size.Z), shapeID, newtOffsetMatrix);

            return(new CollisionHull(handle, world, newtOffsetMatrix, CollisionShapeType.Box));
        }
Ejemplo n.º 3
0
        public static CollisionHull CreateCapsule(WorldBase world, int shapeID, double radius, double height, Matrix3D?offsetMatrix)
        {
            //NOTE:  Height must be >= diameter.  If you want less, use ChamferCylinder

            float[] newtOffsetMatrix = null;            // null means no offset
            if (offsetMatrix != null)
            {
                newtOffsetMatrix = new NewtonMatrix(offsetMatrix.Value).Matrix;
            }

            IntPtr handle = Newton.NewtonCreateCapsule(world.Handle, Convert.ToSingle(radius), Convert.ToSingle(height), shapeID, newtOffsetMatrix);

            return(new CollisionHull(handle, world, newtOffsetMatrix, CollisionShapeType.Capsule));
        }
Ejemplo n.º 4
0
        public static CollisionHull CreateConvexHull(WorldBase world, int shapeID, IEnumerable<Point3D> verticies, Matrix3D? offsetMatrix = null, double tolerance = .002d)
        {
            //NOTE:  If the mesh passed in is concave, newton will make it convex.  If you require concave, build up a complex collision out of convex primitives
            //NOTE:  Must have at least 4 verticies

            #region Tolerance Comments

            // Got this from here:
            //http://newtondynamics.com/wiki/index.php5?title=NewtonCreateConvexHull

            // dFloat tolerance - the vertex optimization tolerance. A higher number means the hull can be simplified where there are multiple vertices with distance lower than
            // the tolerance; this is useful when generating simpler convex hulls from highly detailed meshes.


            // It's new in Newton 2 and can speed up the performance by reducing the count of vertices of a convex hull.
            // Here is the description by Julio Jerez (author of Newton):

            // The convex hull tolerance does not always apply and it is hard to predict, say for example you have a box, then the parameter will do nothing because adjacent plane
            // bend 90 degree.
            //
            // Say some body have a cube with bevel edges, the at the corner you will have many points that are very close to each other this result on a convex hull that is very dense
            // on the edges and the vertex.
            //
            // In that case the tolerance parameter does make a difference. What is does is that after the Hull is made, for each vertex, an average plane is created by fanning of all the
            // vertices that can be reach from a direct edge going out of that vertex.
            //
            // If the distance from each vertex to the average plane is very small, 
            // And the distance from the center vertex to the plane is smaller than the tolerance, 
            // Then the center vertex can be extracted from the hull and the hull can be reconstructed, 
            // The resulting shape will not be too different from the ideal one. 
            //
            // It continues doing that until not more vertex can be subtracted from the original shape.
            //
            // Basically what is does is that is remove lots of vertices that are too close and make the shep to dense for collision.
            //
            // A tolerance of 0.002 is good candidate especially when you have large cloud of vertices because it will eliminate points that are a 2 millimeter of less from the ideal hull,
            // It leads to a great speed up in collision time.
            //
            // (additional note: this value depends to the kind of application and their dimensions)
            // Since Newton 2.25 the tolerance depend of the diagonal of the cloud point (or bounding box). Ex: A prism with large and small dimension (580 x 0.5 x 580) will have
            // a large diagonal (820). If you set tolerance at 0.001 then the function will eliminate point that are less close of 820*0.001=0.8 units. So your prism become a plane and
            // the function return NULL. To resolve this error just set a lower tolerance. This change was made to get same shape for hull with different scale.

            #endregion

            // Offset Matrix
            float[] newtOffsetMatrix = null;		// null means no offset
            if (offsetMatrix != null)
            {
                newtOffsetMatrix = new NewtonMatrix(offsetMatrix.Value).Matrix;
            }

            int vertexCount = verticies.Count();

            // Verticies
            float[,] vertexArray = new float[vertexCount, 3];

            int i = 0;
            foreach (Point3D vertex in verticies)
            {
                vertexArray[i, 0] = (float)vertex.X;
                vertexArray[i, 1] = (float)vertex.Y;
                vertexArray[i, 2] = (float)vertex.Z;
                i++;
            }

            // Create in newton
            IntPtr handle = Newton.NewtonCreateConvexHull(world.Handle, vertexCount, vertexArray, sizeof(float) * 3, Convert.ToSingle(tolerance), shapeID, newtOffsetMatrix);

            // Exit Function
            return new CollisionHull(handle, world, newtOffsetMatrix, CollisionShapeType.ConvexHull);
        }
Ejemplo n.º 5
0
        public static CollisionHull CreateChamferCylinder(WorldBase world, int shapeID, double radius, double height, Matrix3D? offsetMatrix)
        {
            //TODO: Figure out what is wrong when this is tall and skinny

            float[] newtOffsetMatrix = null;		// null means no offset
            if (offsetMatrix != null)
            {
                newtOffsetMatrix = new NewtonMatrix(offsetMatrix.Value).Matrix;
            }

            IntPtr handle = Newton.NewtonCreateChamferCylinder(world.Handle, Convert.ToSingle(radius), Convert.ToSingle(height), shapeID, newtOffsetMatrix);

            return new CollisionHull(handle, world, newtOffsetMatrix, CollisionShapeType.ChamferCylinder);
        }
Ejemplo n.º 6
0
        public static CollisionHull CreateCapsule(WorldBase world, int shapeID, double radius, double height, Matrix3D? offsetMatrix)
        {
            //NOTE:  Height must be >= diameter.  If you want less, use ChamferCylinder

            float[] newtOffsetMatrix = null;		// null means no offset
            if (offsetMatrix != null)
            {
                newtOffsetMatrix = new NewtonMatrix(offsetMatrix.Value).Matrix;
            }

            IntPtr handle = Newton.NewtonCreateCapsule(world.Handle, Convert.ToSingle(radius), Convert.ToSingle(height), shapeID, newtOffsetMatrix);

            return new CollisionHull(handle, world, newtOffsetMatrix, CollisionShapeType.Capsule);
        }
Ejemplo n.º 7
0
        public static CollisionHull CreateBox(WorldBase world, int shapeID, Vector3D size, Matrix3D? offsetMatrix)
        {
            float[] newtOffsetMatrix = null;		// null means no offset
            if (offsetMatrix != null)
            {
                newtOffsetMatrix = new NewtonMatrix(offsetMatrix.Value).Matrix;
            }

            IntPtr handle = Newton.NewtonCreateBox(world.Handle, Convert.ToSingle(size.X), Convert.ToSingle(size.Y), Convert.ToSingle(size.Z), shapeID, newtOffsetMatrix);

            return new CollisionHull(handle, world, newtOffsetMatrix, CollisionShapeType.Box);
        }
Ejemplo n.º 8
0
        public static CollisionHull CreateConvexHull(WorldBase world, int shapeID, IEnumerable <Point3D> verticies, Matrix3D?offsetMatrix = null, double tolerance = .002d)
        {
            //NOTE:  If the mesh passed in is concave, newton will make it convex.  If you require concave, build up a complex collision out of convex primitives
            //NOTE:  Must have at least 4 verticies

            #region Tolerance Comments

            // Got this from here:
            //http://newtondynamics.com/wiki/index.php5?title=NewtonCreateConvexHull

            // dFloat tolerance - the vertex optimization tolerance. A higher number means the hull can be simplified where there are multiple vertices with distance lower than
            // the tolerance; this is useful when generating simpler convex hulls from highly detailed meshes.


            // It's new in Newton 2 and can speed up the performance by reducing the count of vertices of a convex hull.
            // Here is the description by Julio Jerez (author of Newton):

            // The convex hull tolerance does not always apply and it is hard to predict, say for example you have a box, then the parameter will do nothing because adjacent plane
            // bend 90 degree.
            //
            // Say some body have a cube with bevel edges, the at the corner you will have many points that are very close to each other this result on a convex hull that is very dense
            // on the edges and the vertex.
            //
            // In that case the tolerance parameter does make a difference. What is does is that after the Hull is made, for each vertex, an average plane is created by fanning of all the
            // vertices that can be reach from a direct edge going out of that vertex.
            //
            // If the distance from each vertex to the average plane is very small,
            // And the distance from the center vertex to the plane is smaller than the tolerance,
            // Then the center vertex can be extracted from the hull and the hull can be reconstructed,
            // The resulting shape will not be too different from the ideal one.
            //
            // It continues doing that until not more vertex can be subtracted from the original shape.
            //
            // Basically what is does is that is remove lots of vertices that are too close and make the shep to dense for collision.
            //
            // A tolerance of 0.002 is good candidate especially when you have large cloud of vertices because it will eliminate points that are a 2 millimeter of less from the ideal hull,
            // It leads to a great speed up in collision time.
            //
            // (additional note: this value depends to the kind of application and their dimensions)
            // Since Newton 2.25 the tolerance depend of the diagonal of the cloud point (or bounding box). Ex: A prism with large and small dimension (580 x 0.5 x 580) will have
            // a large diagonal (820). If you set tolerance at 0.001 then the function will eliminate point that are less close of 820*0.001=0.8 units. So your prism become a plane and
            // the function return NULL. To resolve this error just set a lower tolerance. This change was made to get same shape for hull with different scale.

            #endregion

            // Offset Matrix
            float[] newtOffsetMatrix = null;            // null means no offset
            if (offsetMatrix != null)
            {
                newtOffsetMatrix = new NewtonMatrix(offsetMatrix.Value).Matrix;
            }

            int vertexCount = verticies.Count();

            // Verticies
            float[,] vertexArray = new float[vertexCount, 3];

            int i = 0;
            foreach (Point3D vertex in verticies)
            {
                vertexArray[i, 0] = (float)vertex.X;
                vertexArray[i, 1] = (float)vertex.Y;
                vertexArray[i, 2] = (float)vertex.Z;
                i++;
            }

            // Create in newton
            IntPtr handle = Newton.NewtonCreateConvexHull(world.Handle, vertexCount, vertexArray, sizeof(float) * 3, Convert.ToSingle(tolerance), shapeID, newtOffsetMatrix);

            // Exit Function
            return(new CollisionHull(handle, world, newtOffsetMatrix, CollisionShapeType.ConvexHull));
        }