public PhysicsContactTestItem(int collisionFilterGroup, int collisionFilterMask, ModeEnum mode, Cylinder cylinder)
        {
            //cylinder works good without additional check for position of point on B.

            //var geometry = cylinder;
            //var offset = geometry.GetDirection() * 0.001;
            //geometry.Point1 -= offset;
            //geometry.Point2 += offset;
            //geometry.Radius += 0.001;

            var collisionObject = new CollisionObject();

            if (cylinder.Point1.ToVector2().Equals(cylinder.Point2.ToVector2(), 0.0001))
            {
                collisionObject.CollisionShape = new BulletSharp.CylinderShapeZ(cylinder.Radius, cylinder.Radius, cylinder.GetLength() * 0.5);
                collisionObject.WorldTransform = BulletPhysicsUtility.Convert(Matrix4.FromTranslate(cylinder.GetCenter()));
            }
            else
            {
                collisionObject.CollisionShape = new BulletSharp.CylinderShapeX(cylinder.GetLength() * 0.5, cylinder.Radius, cylinder.Radius);
                collisionObject.WorldTransform = BulletPhysicsUtility.Convert(new Matrix4(Quaternion.FromDirectionZAxisUp(cylinder.GetDirection()).ToMatrix3(), cylinder.GetCenter()));
            }

            Construct(collisionFilterGroup, collisionFilterMask, mode, collisionObject, true, null);              // point => geometry.Contains( point ) );
        }
Exemple #2
0
        protected internal override CollisionShape CreateShape()
        {
            switch (Axis.Value)
            {
            case 1: return(new BulletSharp.ConeShape(BulletPhysicsUtility.Convert(Radius), BulletPhysicsUtility.Convert(Height)));

            case 2: return(new BulletSharp.ConeShapeZ(BulletPhysicsUtility.Convert(Radius), BulletPhysicsUtility.Convert(Height)));

            default: return(new BulletSharp.ConeShapeX(BulletPhysicsUtility.Convert(Radius), BulletPhysicsUtility.Convert(Height)));
            }
        }
        public PhysicsContactTestItem(int collisionFilterGroup, int collisionFilterMask, ModeEnum mode, Bounds bounds)
        {
            var geometry = bounds;

            geometry.Expand(0.001);

            var collisionObject = new CollisionObject();

            collisionObject.CollisionShape = new BulletSharp.BoxShape(BulletPhysicsUtility.Convert(bounds.GetSize() * 0.5));
            collisionObject.WorldTransform = BulletPhysicsUtility.Convert(Matrix4.FromTranslate(bounds.GetCenter()));
            Construct(collisionFilterGroup, collisionFilterMask, mode, collisionObject, true, point => geometry.Contains(point));
        }
        public PhysicsContactTestItem(int collisionFilterGroup, int collisionFilterMask, ModeEnum mode, Box box)
        {
            var geometry = box;

            geometry.Extents += new Vector3(0.001, 0.001, 0.001);

            var collisionObject = new CollisionObject();

            collisionObject.CollisionShape = new BulletSharp.BoxShape(BulletPhysicsUtility.Convert(box.Extents));
            collisionObject.WorldTransform = BulletPhysicsUtility.Convert(new Matrix4(box.Axis, box.Center));
            Construct(collisionFilterGroup, collisionFilterMask, mode, collisionObject, true, point => geometry.Contains(point));
        }
        public PhysicsContactTestItem(int collisionFilterGroup, int collisionFilterMask, ModeEnum mode, Sphere sphere)
        {
            var geometry = sphere;

            geometry.Radius += 0.001;

            var collisionObject = new CollisionObject();

            collisionObject.CollisionShape = new BulletSharp.SphereShape(sphere.Radius);
            collisionObject.WorldTransform = Matrix.Translation(BulletPhysicsUtility.Convert(sphere.Origin));
            Construct(collisionFilterGroup, collisionFilterMask, mode, collisionObject, true, point => geometry.Contains(point));
        }
        protected internal override CollisionShape CreateShape()
        {
            BulletSharp.Math.Vector3 v = BulletPhysicsUtility.Convert(new Vector3(Radius, Radius, Height * .5));

            switch (Axis.Value)
            {
            case 1: return(new BulletSharp.CylinderShape(v));

            case 2: return(new BulletSharp.CylinderShapeZ(v));

            default: return(new BulletSharp.CylinderShapeX(v));
            }
        }
        public override void DrawLine(ref BulletSharp.Math.Vector3 from, ref BulletSharp.Math.Vector3 to, ref BulletSharp.Math.Vector3 color)
        {
            if (verticesRenderedCounterLimit != -1 && verticesRenderedCounter > verticesRenderedCounterLimit)
            {
                return;
            }

            renderer.SetColor(new ColorValue(BulletPhysicsUtility.Convert(color)));
            renderer.AddLineThin(BulletPhysicsUtility.Convert(from), BulletPhysicsUtility.Convert(to));

            unchecked
            {
                verticesRenderedCounter += 2;
            }
        }
        public PhysicsConvexSweepTestItem(Matrix4 from, Matrix4 to, int collisionFilterGroup, int collisionFilterMask, ModeEnum mode, Box box)
        {
            this.originalFrom         = from;
            this.originalTo           = to;
            this.CollisionFilterGroup = collisionFilterGroup;
            this.CollisionFilterMask  = collisionFilterMask;
            this.Mode = mode;

            Matrix4 offset = new Matrix4(box.Axis, box.Center);

            transformedFrom = originalFrom * offset;
            transformedTo   = originalTo * offset;

            Shape            = new BulletSharp.BoxShape(BulletPhysicsUtility.Convert(box.Extents));
            ShapeAutoDispose = true;
        }
        public PhysicsContactTestItem(int collisionFilterGroup, int collisionFilterMask, ModeEnum mode, Capsule capsule)
        {
            var geometry = capsule;

            geometry.Radius += 0.001;

            var collisionObject = new CollisionObject();

            if (capsule.Point1.ToVector2().Equals(capsule.Point2.ToVector2(), 0.0001))
            {
                collisionObject.CollisionShape = new BulletSharp.CapsuleShapeZ(capsule.Radius, capsule.GetLength());
                collisionObject.WorldTransform = BulletPhysicsUtility.Convert(Matrix4.FromTranslate(capsule.GetCenter()));
            }
            else
            {
                collisionObject.CollisionShape = new BulletSharp.CapsuleShapeX(capsule.Radius, capsule.GetLength());
                collisionObject.WorldTransform = BulletPhysicsUtility.Convert(new Matrix4(Quaternion.FromDirectionZAxisUp(capsule.GetDirection()).ToMatrix3(), capsule.GetCenter()));
            }

            Construct(collisionFilterGroup, collisionFilterMask, mode, collisionObject, true, point => geometry.Contains(point));
        }
Exemple #10
0
        public PhysicsConvexSweepTestItem(Matrix4 from, Matrix4 to, int collisionFilterGroup, int collisionFilterMask, ModeEnum mode, Bounds bounds)
        {
            this.originalFrom         = from;
            this.originalTo           = to;
            this.CollisionFilterGroup = collisionFilterGroup;
            this.CollisionFilterMask  = collisionFilterMask;
            this.Mode = mode;

            transformedFrom = originalFrom;
            transformedTo   = originalTo;

            Vector3 offset = bounds.GetCenter();

            if (!offset.Equals(Vector3.Zero, MathEx.Epsilon))
            {
                transformedFrom.SetTranslation(transformedFrom.GetTranslation() + offset);
                transformedTo.SetTranslation(transformedTo.GetTranslation() + offset);
            }

            var halfSize = bounds.GetSize() / 2;

            Shape            = new BulletSharp.BoxShape(BulletPhysicsUtility.Convert(halfSize));
            ShapeAutoDispose = true;
        }
        protected internal override CollisionShape CreateShape()
        {
            var epsilon = 0.0001f;

            //clear data
            processedVertices = null;
            processedIndices  = null;
            processedTrianglesToSourceIndex = null;

            //get source geometry
            if (!GetSourceData(out var sourceVertices, out var sourceIndices))
            {
                return(null);
            }

            //check valid data
            if (CheckValidData)
            {
                if (!MathAlgorithms.CheckValidVertexIndexBuffer(sourceVertices.Length, sourceIndices, false))
                {
                    Log.Info("Component_CollisionShape_Mesh: CreateShape: Invalid source data.");
                    return(null);
                }
            }

            //process geometry
            if (MergeEqualVerticesRemoveInvalidTriangles)
            {
                //!!!!slowly. later use cached precalculated bullet shape.
                MathAlgorithms.MergeEqualVerticesRemoveInvalidTriangles(sourceVertices, sourceIndices, epsilon, out processedVertices, out processedIndices, out processedTrianglesToSourceIndex);
            }
            else
            {
                processedVertices = sourceVertices;
                processedIndices  = sourceIndices;
            }

            //create bullet shape

            if (ShapeType.Value == ShapeTypeEnum.Auto && ParentRigidBody.MotionType.Value == Component_RigidBody.MotionTypeEnum.Dynamic && MathAlgorithms.IsMeshConvex(processedVertices, processedIndices, epsilon) || ShapeType.Value == ShapeTypeEnum.Convex)
            {
                if (MathAlgorithms.IsPlaneMesh(processedVertices, processedIndices, epsilon))
                {
                    Log.Info("Component_CollisionShape_Mesh: CreateShape: Unable to create shape as convex hull. All vertices on the one plane.");
                    return(null);
                }

                //!!!!тут иначе? возможно лучше получить результирующие processed данные из буллета. как получить processedTrianglesToSourceIndex - это вопрос. возможно ли?
                //если нельзя то processedTrianglesToSourceIndex = new int[ 0 ]; - что означает нельзя сконвертировать.
                //если processedTrianglesToSourceIndex == null, то конвертация 1:1.

                try
                {
                    ConvexHullAlgorithm.Create(processedVertices.ToVector3Array(), processedIndices, out var processedVertices2, out processedIndices);
                    processedVertices = processedVertices2.ToVector3FArray();

                    //var convex = ConvexHullAlgorithm.Create( processedVertices, processedIndices );

                    //var vlist = new List<Vec3F>( convex.Faces.Length * 3 );
                    //foreach( var f in convex.Faces )
                    //	for( int v = 0; v < f.Vertices.Length; v++ )
                    //		vlist.Add( f.Vertices[ v ].ToVec3F() );

                    //processedVertices = vlist.ToArray();
                    //processedIndices = null;

                    //BulletUtils.GetHullVertices( processedVertices.ToVec3Array(), processedIndices, out var processedVertices2, out processedIndices );
                    //processedVertices = processedVertices2.ToVec3FArray();
                    //BulletUtils.GetHullVertices( processedVertices, processedIndices, out processedVertices, out processedIndices );

                    //если нельзя то processedTrianglesToSourceIndex = new int[ 0 ]; - что означает нельзя сконвертировать.
                    processedTrianglesToSourceIndex = Array.Empty <int>();
                }
                catch (Exception e)
                {
                    Log.Info("Component_CollisionShape_Mesh: CreateShape: Unable to create shape as convex hull. " + e.Message);
                    return(null);
                }

                //!!!!
                var processedVerticesBullet = BulletPhysicsUtility.Convert(processedVertices);

                return(new ConvexHullShape(processedVerticesBullet));
            }
            else
            {
                //!!!проверки на ошибки данных

                //!!!!can create without making of Vector3[] array. IntPtr constructor? internally the memory will copied?
                indexVertexArrays = new TriangleIndexVertexArray(processedIndices, BulletPhysicsUtility.Convert(processedVertices));

                //indexVertexArrays = new TriangleIndexVertexArray();
                //var indexedMesh = new IndexedMesh();
                //indexedMesh.Allocate( totalTriangles, totalVerts, triangleIndexStride, vertexStride );
                //indexedMesh SetData( ICollection<int> triangles, ICollection<Vector3> vertices );
                //indexVertexArrays.AddIndexedMesh( indexedMesh );


                //!!!!расшаривать данные которые тут. одинаковые в разных объектах

                //!!!!определять когда не считать кеш

                //It is better to use "useQuantizedAabbCompression=true", because it makes the tree data structure 4 times smaller: sizeof( btOptimizedBvhNode ) = 64 and sizeof( btQuantizedBvhNode ) = 16 bytes.Note that the number of AABB tree nodes is twice the number of triangles.

                //Instead of creating the tree on the XBox 360 console, it is better to deserialize it directly from disk to memory. See btOptimizedBvh::deSerializeInPlace in Demos/ConcaveDemo/ConcavePhysicsDemo.cpp

                //без useQuantizedAabbCompression в три раза быстрее создается

                //!!!!enable when cache support
                bool useQuantizedAabbCompression = false;
                //bool useQuantizedAabbCompression = true;
                bool buildBvh = true;

                //!!!!в другом конструкторе можно еще указать какие-то bound min max
                //public BvhTriangleMeshShape( StridingMeshInterface meshInterface, bool useQuantizedAabbCompression, Vector3 bvhAabbMin, Vector3 bvhAabbMax, bool buildBvh = true );

                return(new BvhTriangleMeshShape(indexVertexArrays, useQuantizedAabbCompression, buildBvh));
            }
        }
Exemple #12
0
 protected internal override CollisionShape CreateShape()
 {
     return(new BulletSharp.SphereShape(BulletPhysicsUtility.Convert(Radius)));
 }
Exemple #13
0
 protected internal override CollisionShape CreateShape()
 {
     return(new BulletSharp.BoxShape(BulletPhysicsUtility.Convert(Dimensions.Value / 2)));
 }