コード例 #1
0
    protected void RescaleMass()
    {
        // Rescale the mass back to the user value.
        float calculated = CP._cpBodyGetMass(_handle);

        CP.cpBodySetMass(_handle, _mass);

//		Debug.Log("Rescaling body mass. (mass: " + calculated + " moment: " + CP._cpBodyGetMoment(_handle) + ")");

        if (calculated > 0f && momentCalculationMode == ChipmunkBodyMomentMode.CalculateFromShapes)
        {
            float moment = CP._cpBodyGetMoment(_handle);
            this.moment = moment * _mass / calculated;
        }
        else if (momentCalculationMode == ChipmunkBodyMomentMode.Override)
        {
            this.moment = _customMoment;
        }
        else
        {
            this.moment = Mathf.Infinity;
        }

        // Cache the calculated moment.
        _customMoment = this.moment;
        if (_isKinematic)
        {
            CP.cpBodySetMass(_handle, Mathf.Infinity);
            CP.cpBodySetMoment(_handle, Mathf.Infinity);
        }

//		Debug.Log("Rescaled body mass. (mass: " + this.mass + " moment: " + this.moment + ")");
    }
コード例 #2
0
    public void _RecalculateMass()
    {
        CP.cpBodySetMass(_handle, 0f);
        CP.cpBodySetMoment(_handle, 0f);
        this.cogOffset = Vector2.zero;

        foreach (var shape in GetComponentsInChildren <ChipmunkShape>())
        {
            // Only add the mass contribution if the shape is actuall attached to this body.
            if (this == shape.body)
            {
                _AddMassForShape(shape);
            }
        }

        RescaleMass();
    }