/// Reset the force and torque applied to this body. public void ResetForces() { if (_handle != IntPtr.Zero) { CP.cpBodyResetForces(_handle); } }
protected override void UpdateConstraint() { base.UpdateConstraint(); CP._cpGearJointSetPhase(_handle, _phase * Mathf.Deg2Rad); CP.cpGearJointSetRatio(_handle, _ratio); }
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 + ")"); }
/// 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); } }
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)); }
protected override void UpdateConstraint() { base.UpdateConstraint(); CP._cpRotaryLimitJointSetMin(_handle, _min * Mathf.Deg2Rad); CP._cpRotaryLimitJointSetMax(_handle, _max * Mathf.Deg2Rad); }
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(); } }
/// Force a body to wake up if it has fallen asleep. public void Activate() { if (_handle != IntPtr.Zero) { CP.cpBodyActivate(_handle); } }
/// 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); } }
protected override void UpdateConstraint() { base.UpdateConstraint(); CP._cpDampedRotarySpringSetRestAngle(_handle, _restAngle * Mathf.Deg2Rad); CP._cpDampedRotarySpringSetStiffness(_handle, _stiffness * Mathf.Deg2Rad); CP._cpDampedRotarySpringSetDamping(_handle, _damping); }
protected override void UpdateConstraint() { base.UpdateConstraint(); CP._cpRatchetJointSetAngle(_handle, _angle * Mathf.Deg2Rad); CP._cpRatchetJointSetPhase(_handle, _phase * Mathf.Deg2Rad); CP._cpRatchetJointSetRatchet(_handle, _ratchet * Mathf.Deg2Rad); }
/// 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); } }
public override ChipmunkBody _UpdatedTransform() { CP.cpBodySetPos(_handle, transform.position); CP.cpBodySetAngle(_handle, transform.eulerAngles.z * Mathf.Deg2Rad); _transform = base.transform; return(this); }
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)); }
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)); }
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)); }
public static ChipmunkShape _FromHandle(IntPtr handle) { if (handle == IntPtr.Zero) { return(null); } else { return(GCHandle.FromIntPtr(CP._cpShapeGetUserData(handle)).Target as ChipmunkShape); } }
public ChipmunkSpace() { #if UNITY_EDITOR _handle = CP.cpSpaceNew(); #elif UNITY_IPHONE _handle = cpHastySpaceNew(); #else _handle = CP.cpSpaceNew(); #endif _staticBody = CP.cpBodyNewStatic(); }
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; }
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)); }
/// 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); } }
/// 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); } }
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); }
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)); }
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; }
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); } }
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)); }
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); }
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(); }
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(); } } }