Пример #1
0
 /// Reset the force and torque applied to this body.
 public void ResetForces()
 {
     if (_handle != IntPtr.Zero)
     {
         CP.cpBodyResetForces(_handle);
     }
 }
Пример #2
0
    protected override void UpdateConstraint()
    {
        base.UpdateConstraint();

        CP._cpGearJointSetPhase(_handle, _phase * Mathf.Deg2Rad);
        CP.cpGearJointSetRatio(_handle, _ratio);
    }
Пример #3
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 + ")");
    }
Пример #4
0
 /// Force a body to fall asleep immediately even if it's in motion or unsupported.
 public void Sleep()
 {
     if (_handle != IntPtr.Zero)
     {
         CP.cpBodySleep(_handle);
     }
 }
Пример #5
0
    protected override void Awake()
    {
        if (_handle != IntPtr.Zero)
        {
            return;
        }
        base.Awake();

        // in case no vertices have been set yet
        if (_verts == null)
        {
            _verts = defaultVerts;
        }

        Vector2[] transformed = MakeVerts();
        _handle = CP.NewConvexPolyShapeWithVerts(transformed.Length, transformed);
        CP.cpPolyShapeSetRadius(_handle, _maxScale * _radius);
        if (body != null)
        {
            body._AddMassForShape(this);
        }

        GCHandle gch = GCHandle.Alloc(this);

        CP._cpShapeSetUserData(_handle, GCHandle.ToIntPtr(gch));
    }
Пример #6
0
    protected override void UpdateConstraint()
    {
        base.UpdateConstraint();

        CP._cpRotaryLimitJointSetMin(_handle, _min * Mathf.Deg2Rad);
        CP._cpRotaryLimitJointSetMax(_handle, _max * Mathf.Deg2Rad);
    }
Пример #7
0
    public void _Remove(ChipmunkBody obj)
    {
        if (obj._handle == IntPtr.Zero)
        {
            Debug.LogError("ChipmunkBody handle is NULL");
            return;
        }

        if (!CP.cpSpaceContainsBody(_handle, obj._handle))
        {
            Debug.LogError("Space does not contain ChipmunkBody.");
            return;
        }

        PostStepFunc del = delegate(){
            //		Debug.Log("Removing body.");
            bodies.Remove(obj);
            CP.cpSpaceRemoveBody(_handle, obj._handle);
        };

        if (locked)
        {
            _AddPostStepCallback(_handle, obj.handle, del);
        }
        else
        {
            del();
        }
    }
Пример #8
0
 /// Force a body to wake up if it has fallen asleep.
 public void Activate()
 {
     if (_handle != IntPtr.Zero)
     {
         CP.cpBodyActivate(_handle);
     }
 }
Пример #9
0
 /// Apply (accumulate) a force on the body at the given point in the transform's local coords.
 public void ApplyForce(Vector2 force, Vector2 localPosition)
 {
     if (_handle != IntPtr.Zero)
     {
         Vector3 offset = _transform.TransformDirection(localPosition);
         CP.cpBodyApplyForce(_handle, force, offset);
     }
 }
Пример #10
0
    protected override void UpdateConstraint()
    {
        base.UpdateConstraint();

        CP._cpDampedRotarySpringSetRestAngle(_handle, _restAngle * Mathf.Deg2Rad);
        CP._cpDampedRotarySpringSetStiffness(_handle, _stiffness * Mathf.Deg2Rad);
        CP._cpDampedRotarySpringSetDamping(_handle, _damping);
    }
Пример #11
0
    protected override void UpdateConstraint()
    {
        base.UpdateConstraint();

        CP._cpRatchetJointSetAngle(_handle, _angle * Mathf.Deg2Rad);
        CP._cpRatchetJointSetPhase(_handle, _phase * Mathf.Deg2Rad);
        CP._cpRatchetJointSetRatchet(_handle, _ratchet * Mathf.Deg2Rad);
    }
Пример #12
0
 /// Apply an impulse on the body at the given point in the transform's local coords.
 public void ApplyImpulse(Vector2 impulse, Vector2 localPosition)
 {
     if (_handle != IntPtr.Zero)
     {
         var offset = _transform.TransformDirection(localPosition);
         CP.cpBodyApplyImpulse(_handle, impulse, offset);
     }
 }
Пример #13
0
    public override ChipmunkBody _UpdatedTransform()
    {
        CP.cpBodySetPos(_handle, transform.position);
        CP.cpBodySetAngle(_handle, transform.eulerAngles.z * Mathf.Deg2Rad);

        _transform = base.transform;

        return(this);
    }
Пример #14
0
    protected override void UpdateConstraint()
    {
        base.UpdateConstraint();

        Vector2 p = transform.TransformPoint(pivot);

        CP._cpPivotJointSetAnchr1(_handle, CP._cpBodyWorld2Local(handleA, p));
        CP._cpPivotJointSetAnchr2(_handle, CP._cpBodyWorld2Local(handleB, p));
    }
Пример #15
0
    protected override void Awake()
    {
        if(_handle != IntPtr.Zero) return;

        _handle = CP.cpPinJointNew(IntPtr.Zero, IntPtr.Zero, Vector2.zero, Vector2.zero);

        var gch = GCHandle.Alloc(this);
        CP._cpConstraintSetUserData(_handle, GCHandle.ToIntPtr(gch));
    }
Пример #16
0
    protected override void UpdateConstraint()
    {
        base.UpdateConstraint();

        Vector2 p1 = transform.TransformPoint(anchr1);
        Vector2 p2 = transform.TransformPoint(anchr2);

        CP._cpPinJointSetAnchr1(_handle, CP._cpBodyWorld2Local(handleA, p1));
        CP._cpPinJointSetAnchr2(_handle, CP._cpBodyWorld2Local(handleB, p2));
        CP._cpPinJointSetDist(_handle, Vector2.Distance(p1, p2));
    }
Пример #17
0
 public static ChipmunkShape _FromHandle(IntPtr handle)
 {
     if (handle == IntPtr.Zero)
     {
         return(null);
     }
     else
     {
         return(GCHandle.FromIntPtr(CP._cpShapeGetUserData(handle)).Target as ChipmunkShape);
     }
 }
Пример #18
0
    public ChipmunkSpace()
    {
#if UNITY_EDITOR
        _handle = CP.cpSpaceNew();
#elif UNITY_IPHONE
        _handle = cpHastySpaceNew();
#else
        _handle = CP.cpSpaceNew();
#endif

        _staticBody = CP.cpBodyNewStatic();
    }
Пример #19
0
    public void _Step(float dt)
    {
        locked = true; {
#if UNITY_EDITOR
            CP.cpSpaceStep(_handle, dt);
#elif UNITY_IPHONE
            cpHastySpaceStep(_handle, dt);
#else
            CP.cpSpaceStep(_handle, dt);
#endif
        } locked = false;
    }
Пример #20
0
    protected override void UpdateConstraint()
    {
        base.UpdateConstraint();

        Vector2 p1 = transform.TransformPoint(grooveA);
        Vector2 p2 = transform.TransformPoint(grooveB);
        Vector2 p3 = transform.TransformPoint(anchr2);

        CP.cpGrooveJointSetGrooveA(_handle, CP._cpBodyWorld2Local(handleA, p1));
        CP.cpGrooveJointSetGrooveB(_handle, CP._cpBodyWorld2Local(handleA, p2));
        CP._cpGrooveJointSetAnchr2(_handle, CP._cpBodyWorld2Local(handleB, p3));
    }
Пример #21
0
 /// Get the penetration depth (negative value) of the ith contact point.
 public float GetDepth(int i)
 {
     if (0 <= i && i < contactCount)
     {
         return(CP.cpArbiterGetDepth(_handle, i));
     }
     else
     {
         Debug.LogError("Index out of bounds");
         return(0f);
     }
 }
Пример #22
0
 /// Get the normal of the ith contact point.
 public Vector2 GetNormal(int i)
 {
     if (0 <= i && i < contactCount)
     {
         return(CP.cpArbiterGetNormal(_handle, i));
     }
     else
     {
         Debug.LogError("Index out of bounds");
         return(Vector2.zero);
     }
 }
Пример #23
0
    protected override void UpdateConstraint()
    {
        base.UpdateConstraint();

        Vector2 p1 = transform.TransformPoint(anchr1);
        Vector2 p2 = transform.TransformPoint(anchr2);

        CP._cpSlideJointSetAnchr1(_handle, CP._cpBodyWorld2Local(handleA, p1));
        CP._cpSlideJointSetAnchr2(_handle, CP._cpBodyWorld2Local(handleB, p2));
        CP._cpSlideJointSetMin(_handle, min);
        CP._cpSlideJointSetMax(_handle, max);
    }
Пример #24
0
    protected override void Awake()
    {
        if (_handle != IntPtr.Zero)
        {
            return;
        }

        _handle = CP.cpDampedRotarySpringNew(IntPtr.Zero, IntPtr.Zero, 0f, 0f, 0f);

        var gch = GCHandle.Alloc(this);

        CP._cpConstraintSetUserData(_handle, GCHandle.ToIntPtr(gch));
    }
Пример #25
0
    protected void UpdateParentBody()
    {
        ChipmunkBody body = this.GetComponentUpwards <ChipmunkBody>();

        if (body != this.body && space != null)
        {
            space._Remove(this);
            CP.cpShapeSetBody(_handle, body == null ? space._staticBody : body.handle);
            space._Add(this);
        }

        this.body = body;
    }
Пример #26
0
    public void OnDrawGizmosSelected()
    {
        Gizmos.color = GizmoColor;

        if(_handle == IntPtr.Zero){
            Vector2 p1 = transform.TransformPoint(anchr1);
            Vector2 p2 = transform.TransformPoint(anchr2);
            Gizmos.DrawLine(p1, p2);
        } else {
            Vector2 p1 = CP._cpBodyLocal2World(handleA, CP._cpPinJointGetAnchr1(_handle));
            Vector2 p2 = CP._cpBodyLocal2World(handleB, CP._cpPinJointGetAnchr2(_handle));
            Gizmos.DrawLine(p1, p2);
        }
    }
Пример #27
0
    protected override void Awake()
    {
        if (_handle != IntPtr.Zero)
        {
            return;
        }

        _handle = CP.cpDampedSpringNew(IntPtr.Zero, IntPtr.Zero, Vector2.zero, Vector2.zero, 10f, 10f, 10f);
//		_handle = CP.cpDampedSpringNew(IntPtr.Zero, IntPtr.Zero, new Vector2(10f, 10f), new Vector2(10f, 10f), 0f, 1f, 0f);

        var gch = GCHandle.Alloc(this);

        CP._cpConstraintSetUserData(_handle, GCHandle.ToIntPtr(gch));
    }
Пример #28
0
    protected override void UpdateConstraint()
    {
        base.UpdateConstraint();

        Vector2 p1 = transform.TransformPoint(anchr1);
        Vector2 p2 = transform.TransformPoint(anchr2);

//		CP._cpDampedSpringSetAnchr1(_handle, new Vector2(10f, 10f));
//		CP._cpDampedSpringSetAnchr2(_handle, new Vector2(0f, 10f));
        CP._cpDampedSpringSetAnchr1(_handle, CP._cpBodyWorld2Local(handleA, p1));
        CP._cpDampedSpringSetAnchr2(_handle, CP._cpBodyWorld2Local(handleB, p2));
        CP._cpDampedSpringSetRestLength(_handle, _restLength);
        CP._cpDampedSpringSetStiffness(_handle, _stiffness);
        CP._cpDampedSpringSetDamping(_handle, _damping);
    }
Пример #29
0
    protected override void Awake()
    {
        if (_handle != IntPtr.Zero)
        {
            return;
        }
//		Debug.Log ("ChipmunkBody Awake: " + gameObject.name);

        _handle = CP.cpBodyNew(0f, 0f);
        var gch = GCHandle.Alloc(this);

        CP._cpBodySetUserData(_handle, GCHandle.ToIntPtr(gch));

        _UpdatedTransform();
    }
Пример #30
0
    protected void UpdateVerts()
    {
        if (_handle != IntPtr.Zero)
        {
            var transformed = MakeVerts();
            CP.UpdateConvexPolyShapeWithVerts(_handle, transformed.Length, transformed);
            CP.cpPolyShapeSetRadius(_handle, _maxScale * _radius);
            CP.cpSpaceReindexShape(space._handle, _handle);

            if (body)
            {
                body.Activate();
            }
        }
    }