void BuildConvexData()
        {
            //stan hull
            Vector3[] vIn1 = new Vector3[vertsCount];
            for (int i = 0; i < vertsCount; i++)
            {
                vIn1[i] = verts[i];
            }

            ParallelQHullData2 qhullData2 = Parallel3D.ConvextHull3D2(vIn1, (UInt32)vertsCount, (int)_limit);

            convexData2 = qhullData2;

            ParallelIntTriangle[] t = new ParallelIntTriangle[convexData2.triCount];
            Array.Copy(convexData2.tris, 0, t, 0, convexData2.triCount);
            convexData2.tris = t;

            //new convex hull
            Fix64Vec3[] vIn = new Fix64Vec3[_limit];
            for (int i = 0; i < _limit; i++)
            {
                vIn[i] = (Fix64Vec3)convexData2.vertices[i];
            }

            float rad = angle * Mathf.Deg2Rad;

            ParallelQHullData qhullData = Parallel3D.ConvextHull3D(vIn, (UInt32)_limit, _simplified, (Fix64)rad);

            convexData = qhullData;

            Fix64Vec3[] v = new Fix64Vec3[convexData.vertexCount];
            Array.Copy(convexData.vertices, 0, v, 0, convexData.vertexCount);
            convexData.vertices = v;

            string output = "";

            output += $"b3Vec3 verts[{convexData.vertexCount}] = {{}};\n";
            //Debug.Log($"b3Vec3 verts[{convexData.vertexCount}] = {{}};");
            for (int i = 0; i < convexData.vertexCount; i++)
            {
                Vector3 vec3 = (Vector3)convexData.vertices[i];
                output += $"b3Vec3({vec3.x}, {vec3.y}, {vec3.z}),\n";
                //Debug.Log($"verts[{i}] = b3Vec3({vec3.x}, {vec3.y}, {vec3.z});");
            }
            //Debug.Log(output);

            ParallelEdge[] e = new ParallelEdge[convexData.edgeCount];
            Array.Copy(convexData.edges, 0, e, 0, convexData.edgeCount);
            convexData.edges = e;

            ParallelFace[] f = new ParallelFace[convexData.faceCount];
            Array.Copy(convexData.faces, 0, f, 0, convexData.faceCount);
            convexData.faces = f;

            ParallelPlane[] p = new ParallelPlane[convexData.faceCount];
            Array.Copy(convexData.planes, 0, p, 0, convexData.faceCount);
            convexData.planes = p;

            return;
        }
Beispiel #2
0
 public void ReceiveFixture(PFixture3D fixture)
 {
     _fixture = fixture;
     _shape   = Parallel3D.GetShapeOfFixture(fixture);
     Parallel3D.SetLayer(fixture, gameObject.layer, false);
     Parallel3D.SetFixtureProperties(fixture, isTrigger, _friction, _bounciness);
 }
Beispiel #3
0
        public bool LoadSavedExport(UInt32 step)
        {
            if (step > _maxStep)
            {
                return(false);
            }

            if (step < _minStep)
            {
                return(false);
            }

            int index = CalculateExportIndex(step);

            PBodyExport3D export = bodyExports[index];

            linearVelocity  = export.linearVelocity;
            angularVelocity = export.angularVelocity;
            position        = export.position;
            orientation     = export.orientation;
            orientation0    = export.orientation0;
            awake           = export.awake;
            sleepTime       = export.sleepTime;

            Parallel3D.UpdateBodyTransformForRollback(this, position, orientation, orientation0);
            Parallel3D.UpdateBodyVelocity(this, linearVelocity, angularVelocity);
            Parallel3D.SetAwakeForRollback(this, awake, sleepTime);
            return(true);
        }
        protected override void UpdateShape(GameObject root)
        {
            Fix64     h  = CalculateHeight();
            Fix64     r  = CalculateRadius();
            Fix64Vec3 p1 = Fix64Vec3.zero;
            Fix64Vec3 p2 = Fix64Vec3.zero;

            CalculatePoints(h, r, ref p1, ref p2);

            if (p1 == Fix64Vec3.zero || p2 == Fix64Vec3.zero)
            {
                Debug.LogError("Invalid Size");
                return;
            }

            Fix64Vec3 center   = Fix64Vec3.zero;
            Fix64Quat rotation = Fix64Quat.identity;

            if (gameObject != root)
            {
                center   = _pTransform.localPosition;
                rotation = _pTransform.localRotation;
            }

            point1 = p1;
            point2 = p2;
            radius = r;
            height = h;
            Parallel3D.UpdateCapsule(_shape, _fixture, p1, p2, radius, center, rotation);
        }
        public override PShape3D CreateShape(GameObject root)
        {
            Fix64 r = CalculateRadius();

            if (r > Fix64.zero)
            {
                Fix64Vec3 center = Fix64Vec3.zero;

                if (gameObject != root)
                {
                    center = _pTransform.localPosition;
                }

                _shape = Parallel3D.CreateSphere(r, center);

                if (createUnityPhysicsCollider)
                {
                    var collider = gameObject.AddComponent <SphereCollider>();
                    collider.radius = (float)radius;
                }

                return(_shape);
            }
            else
            {
                Debug.LogError("Invalid Size");
                return(null);
            }
        }
        public void AddToWorldForPathFinding()
        {
            if (_bodyType != BodyType.Static)
            {
                return;
            }

            ParallelCollider3D[] colliders = GetComponentsInChildren <ParallelCollider3D>();
            PBody3D body = Parallel3D.AddBody(
                (int)bodyType,
                pTransform.position,
                pTransform.rotation,
                linearDamping,
                angularDamping,
                gravityScale,
                fixedRotationX,
                fixedRotationY,
                fixedRotationZ,
                this);

            foreach (ParallelCollider3D collider in colliders)
            {
                PShape3D shape = collider.CreateShape(gameObject);

                if (shape == null)
                {
                    Debug.LogError("Failed to create collider shape");
                    continue;
                }

                Parallel3D.AddFixture(body, shape, (Fix64)1);
            }
        }
        public void WriteNative()
        {
            if (_bodyDirty)
            {
                _bodyDirty = false;
                Parallel3D.UpdateBodyProperties(
                    _body3D,
                    (int)bodyType,
                    linearDamping,
                    angularDamping,
                    gravityScale,
                    fixedRotationX,
                    fixedRotationY,
                    fixedRotationZ);
            }

            foreach (ParallelCollider3D collider in colliders)
            {
                collider.UpdateNativeShapeIfNecessary(gameObject);
            }

            if (_velocityDirty)
            {
                _velocityDirty = false;
                Parallel3D.UpdateBodyVelocity(_body3D, _tempLinearVelocity, _tempAngularVelocity);
            }
        }
 public void ExcuteUserCallbacks()
 {
     //using (new SProfiler($"==========ExcuteUserCallbacks========"))
     {
         Parallel3D.ExcuteUserCallbacks(fixedUpdateTime);
     }
 }
        public override PShape3D CreateShape(GameObject root)
        {
            Fix64Vec3 s = CalculateSize();

            if (s != Fix64Vec3.zero)
            {
                _currentSize = s;

                Fix64Vec3 center   = Fix64Vec3.zero;
                Fix64Quat rotation = Fix64Quat.identity;

                if (gameObject != root)
                {
                    center   = _pTransform.localPosition;
                    rotation = _pTransform.localRotation;
                }

                _shape = Parallel3D.CreatePolyhedron(convexData, s, center, rotation);

                if (createUnityPhysicsCollider)
                {
                    var collider = gameObject.AddComponent <MeshCollider>();
                    collider.convex = true;
                }
                return(_shape);
            }
            else
            {
                return(null);
            }
        }
Beispiel #10
0
        public override PShape3D CreateShape(GameObject root)
        {
            Fix64Vec3 s = CalculateSize();

            if (s != Fix64Vec3.zero)
            {
                Fix64Vec3 center   = Fix64Vec3.zero;
                Fix64Quat rotation = Fix64Quat.identity;

                if (gameObject != root)
                {
                    center   = _pTransform.localPosition;
                    rotation = _pTransform.localRotation;
                }

                _shape = Parallel3D.CreateCube(s.x, s.y, s.z, center, rotation);

                if (createUnityPhysicsCollider)
                {
                    var collider = gameObject.AddComponent <BoxCollider>();
                    collider.size = (Vector3)size;
                }

                return(_shape);
            }
            else
            {
                return(null);
            }
        }
 public void InitIfNecessary()
 {
     if (!_initialized)
     {
         _initialized          = true;
         Parallel3D.gravity    = gravity;
         Parallel3D.allowSleep = allowSleep;
         Parallel3D.warmStart  = warmStart;
         Parallel3D.SetLoggingLevel(LoggingLevel);
         Parallel3D.bodyExportSize = bodyExportSize;
     }
 }
        protected override void UpdateShape(GameObject root)
        {
            Fix64 r = CalculateRadius();

            if (r > Fix64.zero)
            {
                Fix64Vec3 center = Fix64Vec3.zero;

                if (gameObject != root)
                {
                    center = _pTransform.localPosition;
                }

                Parallel3D.UpdateSphere(_shape, _fixture, r, center);
            }
        }
Beispiel #13
0
        protected override void UpdateShape(GameObject root)
        {
            Fix64Vec3 s = CalculateSize();

            if (s != Fix64Vec3.zero)
            {
                Fix64Vec3 center   = Fix64Vec3.zero;
                Fix64Quat rotation = Fix64Quat.identity;

                if (gameObject != root)
                {
                    center   = _pTransform.localPosition;
                    rotation = _pTransform.localRotation;
                }

                Parallel3D.UpdateCube(_shape, _fixture, s.x, s.y, s.z, center, rotation);
            }
        }
        protected override void UpdateShape(GameObject root)
        {
            Fix64Vec3 s = CalculateSize();

            if (s == Fix64Vec3.zero)
            {
                return;
            }

            if (s == _currentSize)
            {
                return;
            }

            Fix64Vec3 scale = s / _currentSize;

            _currentSize = s;

            Parallel3D.UpdateMesh(_shape, _fixture, scale);
        }
        public override PShape3D CreateShape(GameObject root)
        {
            Fix64     h  = CalculateHeight();
            Fix64     r  = CalculateRadius();
            Fix64Vec3 p1 = Fix64Vec3.zero;
            Fix64Vec3 p2 = Fix64Vec3.zero;

            CalculatePoints(h, r, ref p1, ref p2);

            if (p1 == Fix64Vec3.zero || p2 == Fix64Vec3.zero)
            {
                Debug.LogError("Invalid Size");
                return(null);
            }
            else
            {
                Fix64Vec3 center   = Fix64Vec3.zero;
                Fix64Quat rotation = Fix64Quat.identity;

                if (gameObject != root)
                {
                    center   = _pTransform.localPosition;
                    rotation = _pTransform.localRotation;
                }

                point1 = p1;
                point2 = p2;
                radius = r;
                height = h;
                _shape = Parallel3D.CreateCapsule(p1, p2, radius, center, rotation);

                if (createUnityPhysicsCollider)
                {
                    var collider = gameObject.AddComponent <CapsuleCollider>();
                    collider.height = (float)h;
                    collider.radius = (float)radius;
                }

                return(_shape);
            }
        }
        public override PShape3D CreateShape(GameObject root)
        {
            Fix64Vec3 s = CalculateSize();

            if (s != Fix64Vec3.zero)
            {
                _currentSize = s;
                _shape       = Parallel3D.CreateMesh(meshData, s);

                if (createUnityPhysicsCollider)
                {
                    gameObject.AddComponent <MeshCollider>();
                }

                return(_shape);
            }
            else
            {
                return(null);
            }
        }
Beispiel #17
0
        void UpdateRigidbodyTransform()
        {
            if (_rigidbody2D == null)
            {
                _rigidbody2D = GetComponent <ParallelRigidbody2D>();
            }

            if (_rigidbody3D == null)
            {
                _rigidbody3D = GetComponent <ParallelRigidbody3D>();
            }

            if (_rigidbody2D != null)
            {
                Parallel2D.UpdateBodyTransForm(_rigidbody2D._body2D, (Fix64Vec2)_localPosition, Fix64.DegToRad(_internalLocalEularAngles.z));
            }

            if (_rigidbody3D != null)
            {
                Parallel3D.UpdateBodyTransForm(_rigidbody3D._body3D, _localPosition, _internalLocalRotation);
            }
        }
 public void AddWarmStart(PInternalState3D state)
 {
     Parallel3D.AddExternalContactWarmStartData(state);
 }
 public void ExportEngineState(PInternalState3D state)
 {
     Parallel3D.ExportEngineInternalState(state);
 }
 public void PrepareExternalContactData()
 {
     Parallel3D.PrepareExternalContactData();
 }
 public void UpdateContacts()
 {
     Parallel3D.UpdateContacts();
 }
 /// Apply a torque. This affects the angular velocity
 /// without affecting the linear velocity of the center of mass.
 /// z-axis (out of the screen)
 public void ApplyTorque(Fix64Vec3 torque)
 {
     Parallel3D.ApplyTorque(_body3D, torque);
 }
 public void Step()
 {
     Parallel3D.Step(fixedUpdateTime, velocityIteration, positionIteration);
 }
 /// Apply an angular impulse. This immediately modifies the angular velocity
 public void ApplyAngularImpulse(Fix64Vec3 impulse)
 {
     Parallel3D.ApplyAngularImpulse(_body3D, impulse);
 }
 public void AddConvexCache(PInternalState3D state)
 {
     Parallel3D.AddExternalConvexCache(state);
 }
        //============================== Unity Events ==============================
        void Awake()
        {
            ParallelPhysicsController3D pSettings = FindObjectOfType <ParallelPhysicsController3D>();

            if (pSettings == null)
            {
                return;
            }

            pSettings.InitIfNecessary();

            parallelFixedUpdates = GetComponents <IParallelFixedUpdate>();
            parallelCollisions   = GetComponents <IParallelCollision3D>();
            parallelTriggers     = GetComponents <IParallelTrigger3D>();

            pTransform.ImportFromUnity();

            colliders = GetComponentsInChildren <ParallelCollider3D>();

            _body3D = Parallel3D.AddBody(
                (int)bodyType,
                pTransform.position,
                pTransform.rotation,
                linearDamping,
                angularDamping,
                gravityScale,
                fixedRotationX,
                fixedRotationY,
                fixedRotationZ,
                this);

            _bodyID = _body3D.BodyID;

            foreach (ParallelCollider3D collider in colliders)
            {
                PShape3D shape = collider.CreateShape(gameObject);

                if (shape == null)
                {
                    Debug.LogError("Failed to create collider shape");
                    continue;
                }

                PFixture3D fixture = Parallel3D.AddFixture(_body3D, shape, (Fix64)1);

                collider.ReceiveFixture(fixture);
            }

            if (_overideMassData)
            {
                //Parallel3D.UpdateMassData(_body3D, _mass, _centerOfMass);
                if (_centerOfMass != null)
                {
                    Fix64Vec3 com = _centerOfMass;
                    //Debug.Log(com);
                    Parallel3D.UpdateMassData(_body3D, _mass, com);
                }
                else
                {
                    Parallel3D.UpdateMass(_body3D, _mass);
                }
            }
        }
 /// Apply an impulse at a point. This immediately modifies the velocity.
 /// It also modifies the angular velocity if the point of application
 /// is not at the center of mass.
 public void ApplyLinearImpluse(Fix64Vec3 impluse, Fix64Vec3 worldPoint)
 {
     Parallel3D.ApplyLinearImpulse(_body3D, worldPoint, impluse);
 }
 public void PrepareExternalConvexCacheData()
 {
     Parallel3D.PrepareExternalConvexCacheData();
 }
 //Apply an impulse to the center of mass. This immediately modifies the velocity.
 public void ApplyLinearImpulse(Fix64Vec3 impluse)
 {
     Parallel3D.ApplyLinearImpulseToCenter(_body3D, impluse);
 }
 private void OnDestroy()
 {
     Parallel3D.CleanUp();
 }