Ejemplo n.º 1
0
        //NOTE: Taking in token was added so that if a body needs to change size, a new one can be built using
        //the same token.  In general, always pass null to guarantee unique tokens for each body
        public Body(CollisionHull hull, Matrix3D offsetMatrix, double mass, Visual3D[] visuals = null, long?token = null)
        {
            _world       = hull.World;
            _worldHandle = _world.Handle;
            this.Visuals = visuals;

            //NOTE:  The collision hull can take a null offset matrix, but the body can't
            _handle = Newton.NewtonCreateBody(_worldHandle, hull.Handle, new NewtonMatrix(offsetMatrix).Matrix);

            IntPtr hullHandlePost = Newton.NewtonBodyGetCollision(_handle);

            if (hullHandlePost != hull.Handle)
            {
                // For some reason, this is coming back with a new handle for compound collision hulls.  So store this second one as well
                ObjectStorage.Instance.AddCollisionHull(hullHandlePost, hull.Clone(hullHandlePost));

                // Testing shows the old one to still be out there.  I should probably clone the hull with this new handle using a special overload of
                // the constructor.  But as long as the user doesn't manipulate hulls after they are bound to a body, everything should be fine
                //int test = Newton.NewtonCollisionIsTriggerVolume(hull.Handle);
            }

            // Listen to the move event (the invoke method is a newton callback that converts the newton stuff into c# friendly stuff)
            _newtonBodyMoved = new Newton.NewtonSetTransform(InvokeBodyMoved);
            Newton.NewtonBodySetTransformCallback(_handle, _newtonBodyMoved);
            this.OffsetMatrix = offsetMatrix;           // calling this explicitly, because it call OnBodyMoved (making sure the visual is synced)

            // Store the default mass matrix
            this.Mass = mass;           // letting the property set build the mass matrix

            if (token == null)
            {
                this.Token = TokenGenerator.NextToken();
            }
            else
            {
                this.Token = token.Value;
            }

            ObjectStorage.Instance.AddBody(_handle, this);

            _world.BodyCreated(this);
        }