// for BoingBehavior & BoingReactor public void PrepareExecute(ref Params p, Vector3 position, Quaternion rotation, float scale, bool accumulateEffectors) { PositionTarget = PositionOrigin = position; RotationTarget = RotationOrigin = QuaternionUtil.ToVector4(rotation); if (accumulateEffectors) { // make relative PositionTarget = Vector3.zero; RotationTarget = Vector4.zero; m_numEffectors = 0; m_upWs = p.Bits.IsBitSet(ReactorFlags.GlobalReactionUpVector) ? p.RotationReactionUp : rotation *VectorUtil.NormalizeSafe(p.RotationReactionUp, Vector3.up); m_scale = scale; } else { m_numEffectors = -1; m_upWs = Vector3.zero; } }
public Params(BoingEffector effector) { Bits = new Bits32(); Bits.SetBit(BoingWork.EffectorFlags.ContinuousMotion, effector.ContinuousMotion); float speedEffectRatio = effector.MaxImpulseSpeed > MathUtil.Epsilon ? Mathf.Min(1.0f, effector.LinearSpeed / effector.MaxImpulseSpeed) : 1.0f; PrevPosition = effector.m_prevPosition; CurrPosition = effector.m_currPosition; LinearVelocityDir = VectorUtil.NormalizeSafe(effector.LinearVelocity, Vector3.zero); Radius = effector.Radius; FullEffectRadius = Radius * effector.FullEffectRadiusRatio; MoveDistance = effector.MoveDistance; LinearImpulse = speedEffectRatio * effector.LinearImpulse; RotateAngle = effector.RotationAngle * MathUtil.Deg2Rad; AngularImpulse = speedEffectRatio * effector.AngularImpulse * MathUtil.Deg2Rad; m_padding0 = 0.0f; m_padding1 = 0.0f; m_padding2 = 0.0f; m_padding3 = 0; }
// for BoingReactorFied public void PrepareExecute(ref Params p, Vector3 gridCenter, Quaternion gridRotation, Vector3 cellOffset) { PositionOrigin = gridCenter + cellOffset; RotationOrigin = QuaternionUtil.ToVector4(Quaternion.identity); // make relative PositionTarget = Vector3.zero; RotationTarget = Vector4.zero; m_numEffectors = 0; m_upWs = p.Bits.IsBitSet(ReactorFlags.GlobalReactionUpVector) ? p.RotationReactionUp : gridRotation *VectorUtil.NormalizeSafe(p.RotationReactionUp, Vector3.up); m_scale = 1.0f; }
public static bool SphereSphereInverse(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB, out Vector3 push) { push = Vector3.zero; Vector3 vec = centerB - centerA; float dd = vec.sqrMagnitude; float r = radiusB - radiusA; if (dd <= r * r) { return(false); } float d = Mathf.Sqrt(dd); push = VectorUtil.NormalizeSafe(vec, Vector3.zero) * (d - r); return(true); }
public void Execute(BoingBones bones, float dt) { float maxCollisionResolutionPushLen = bones.MaxCollisionResolutionSpeed * dt; for (int iChain = 0; iChain < bones.BoneData.Length; ++iChain) { var chain = bones.BoneChains[iChain]; var aBone = bones.BoneData[iChain]; if (aBone == null) { continue; } Vector3 gravityDt = chain.Gravity * dt; // execute boing work for (int iBone = 0; iBone < aBone.Length; ++iBone) // skip root { var bone = aBone[iBone]; // no gravity on root if (iBone > 0) { bone.Instance.PositionSpring.Velocity += gravityDt; } if (chain.ParamsOverride == null) { bone.Instance.Execute(ref bones.Params, dt); } else { bone.Instance.Execute(ref chain.ParamsOverride.Params, dt); } } var rootBone = aBone[0]; rootBone.ScaleWs = rootBone.BlendedScaleLs = rootBone.CachedScaleLs; rootBone.UpdateBounds(); chain.Bounds = rootBone.Bounds; Vector3 rootAnimPos = rootBone.Transform.position; // apply length stiffness & volume preservation for (int iBone = 1; iBone < aBone.Length; ++iBone) // skip root { var bone = aBone[iBone]; var parentBone = aBone[bone.ParentIndex]; float tLengthStiffness = 1.0f - Mathf.Pow(1.0f - bone.LengthStiffness, 30.0f * dt); // a factor of 30.0f is what makes 0.5 length stiffness looks like 50% stiffness Vector3 toParentVec = parentBone.Instance.PositionSpring.Value - bone.Instance.PositionSpring.Value; Vector3 toParentDir = VectorUtil.NormalizeSafe(toParentVec, Vector3.zero); float fullyStiffToParentLen = (parentBone.Transform.position - bone.Transform.position).magnitude; float toParentLen = toParentVec.magnitude; float fullyStiffLenDelta = toParentLen - fullyStiffToParentLen; float toParentAdjustLen = tLengthStiffness * fullyStiffLenDelta; // length stiffness { bone.Instance.PositionSpring.Value += toParentAdjustLen * toParentDir; Vector3 velocityInParentAdjustDir = Vector3.Project(bone.Instance.PositionSpring.Velocity, toParentDir); bone.Instance.PositionSpring.Velocity -= tLengthStiffness * velocityInParentAdjustDir; } // bend angle cap if (bone.BendAngleCap < MathUtil.Pi - MathUtil.Epsilon) { Vector3 animPos = bone.Transform.position; Vector3 posDelta = bone.Instance.PositionSpring.Value - rootAnimPos; posDelta = VectorUtil.ClampBend(posDelta, animPos - rootAnimPos, bone.BendAngleCap); bone.Instance.PositionSpring.Value = rootAnimPos + posDelta; } // volume preservation if (bone.SquashAndStretch > 0.0f) { float toParentLenRatio = toParentLen * MathUtil.InvSafe(fullyStiffToParentLen); float volumePreservationScale = Mathf.Sqrt(1.0f / toParentLenRatio); volumePreservationScale = Mathf.Clamp(volumePreservationScale, 1.0f / Mathf.Max(1.0f, chain.MaxStretch), Mathf.Max(1.0f, chain.MaxSquash)); Vector3 volumePreservationScaleVec = VectorUtil.ComponentWiseDivSafe(volumePreservationScale * Vector3.one, parentBone.ScaleWs); bone.BlendedScaleLs = Vector3.Lerp ( Vector3.Lerp ( bone.CachedScaleLs, volumePreservationScaleVec, bone.SquashAndStretch ), bone.CachedScaleLs, bone.AnimationBlend ); } else { bone.BlendedScaleLs = bone.CachedScaleLs; } bone.ScaleWs = VectorUtil.ComponentWiseMult(parentBone.ScaleWs, bone.BlendedScaleLs); bone.UpdateBounds(); chain.Bounds.Encapsulate(bone.Bounds); } chain.Bounds.Expand(0.2f * Vector3.one); // Boing Kit colliders if (chain.EnableBoingKitCollision) { foreach (var collider in bones.BoingColliders) { if (collider == null) { continue; } if (!chain.Bounds.Intersects(collider.Bounds)) { continue; } foreach (var bone in aBone) { if (!bone.Bounds.Intersects(collider.Bounds)) { continue; } Vector3 push; bool collided = collider.Collide(bone.Instance.PositionSpring.Value, bones.MinScale * bone.CollisionRadius, out push); if (!collided) { continue; } bone.Instance.PositionSpring.Value += VectorUtil.ClampLength(push, 0.0f, maxCollisionResolutionPushLen); bone.Instance.PositionSpring.Velocity -= Vector3.Project(bone.Instance.PositionSpring.Velocity, push); } } } // Unity colliders var sharedSphereCollider = BoingManager.SharedSphereCollider; if (chain.EnableUnityCollision && sharedSphereCollider != null) { sharedSphereCollider.enabled = true; foreach (var collider in bones.UnityColliders) { if (collider == null) { continue; } if (!chain.Bounds.Intersects(collider.bounds)) { continue; } foreach (var bone in aBone) { if (!bone.Bounds.Intersects(collider.bounds)) { continue; } sharedSphereCollider.center = bone.Instance.PositionSpring.Value; sharedSphereCollider.radius = bone.CollisionRadius; Vector3 pushDir; float pushDist; bool collided = Physics.ComputePenetration ( sharedSphereCollider, Vector3.zero, Quaternion.identity, collider, collider.transform.position, collider.transform.rotation, out pushDir, out pushDist ); if (!collided) { continue; } bone.Instance.PositionSpring.Value += VectorUtil.ClampLength(pushDir * pushDist, 0.0f, maxCollisionResolutionPushLen); bone.Instance.PositionSpring.Velocity -= Vector3.Project(bone.Instance.PositionSpring.Velocity, pushDir); } } sharedSphereCollider.enabled = false; } // self collision if (chain.EnableInterChainCollision) { foreach (var bone in aBone) { for (int iOtherChain = iChain + 1; iOtherChain < bones.BoneData.Length; ++iOtherChain) { var otherChain = bones.BoneChains[iOtherChain]; var aOtherBone = bones.BoneData[iOtherChain]; if (aOtherBone == null) { continue; } if (!otherChain.EnableInterChainCollision) { continue; } if (!chain.Bounds.Intersects(otherChain.Bounds)) { continue; } foreach (var otherBone in aOtherBone) { Vector3 push; bool collided = Collision.SphereSphere ( bone.Instance.PositionSpring.Value, bones.MinScale * bone.CollisionRadius, otherBone.Instance.PositionSpring.Value, bones.MinScale * otherBone.CollisionRadius, out push ); if (!collided) { continue; } push = VectorUtil.ClampLength(push, 0.0f, maxCollisionResolutionPushLen); float pushRatio = otherBone.CollisionRadius * MathUtil.InvSafe(bone.CollisionRadius + otherBone.CollisionRadius); bone.Instance.PositionSpring.Value += pushRatio * push; otherBone.Instance.PositionSpring.Value -= (1.0f - pushRatio) * push; bone.Instance.PositionSpring.Velocity -= Vector3.Project(bone.Instance.PositionSpring.Velocity, push); otherBone.Instance.PositionSpring.Velocity -= Vector3.Project(otherBone.Instance.PositionSpring.Velocity, push); } } } } } // end: foreach bone chain }
public void PullResults(BoingBones bones) { for (int iChain = 0; iChain < bones.BoneData.Length; ++iChain) { var chain = bones.BoneChains[iChain]; var aBone = bones.BoneData[iChain]; if (aBone == null) { continue; } // must cache before manipulating bone transforms // otherwise, we'd cache delta propagated down from parent bones foreach (var bone in aBone) { bone.CachedPositionWs = bone.Transform.position; bone.CachedPositionLs = bone.Transform.localPosition; bone.CachedRotationWs = bone.Transform.rotation; bone.CachedRotationLs = bone.Transform.localRotation; } // blend bone position for (int iBone = 0; iBone < aBone.Length; ++iBone) { var bone = aBone[iBone]; // skip fixed root if (iBone == 0 && !chain.LooseRoot) { bone.BlendedPositionWs = bone.CachedPositionWs; continue; } bone.BlendedPositionWs = Vector3.Lerp ( bone.Instance.PositionSpring.Value, bone.CachedPositionWs, bone.AnimationBlend ); } // rotation delta back-propagation if (bones.EnableRotationEffect) { for (int iBone = 0; iBone < aBone.Length; ++iBone) { var bone = aBone[iBone]; // skip fixed root if (iBone == 0 && !chain.LooseRoot) { bone.BlendedRotationWs = bone.CachedRotationWs; continue; } if (bone.ChildIndices == null) { if (bone.ParentIndex >= 0) { var parentBone = aBone[bone.ParentIndex]; bone.BlendedRotationWs = parentBone.BlendedRotationWs * (parentBone.RotationInverseWs * bone.CachedRotationWs); } continue; } Vector3 bonePos = bone.CachedPositionWs; Vector3 boneBlendedPos = bone.BlendedPositionWs; Quaternion boneRot = bone.CachedRotationWs; Quaternion boneRotInv = Quaternion.Inverse(boneRot); Vector4 childRotDeltaPsVecAccumulator = Vector3.zero; float totalWeight = 0.0f; foreach (int iChild in bone.ChildIndices) { var childBone = aBone[iChild]; Vector3 childPos = childBone.CachedPositionWs; Vector3 childPosDelta = childPos - bonePos; Vector3 childPosDeltaDir = VectorUtil.NormalizeSafe(childPosDelta, Vector3.zero); Vector3 childBlendedPos = childBone.BlendedPositionWs; Vector3 childBlendedPosDelta = childBlendedPos - boneBlendedPos; Vector3 childBlendedPosDeltaDir = VectorUtil.NormalizeSafe(childBlendedPosDelta, Vector3.zero); Quaternion childRotDelta = Quaternion.FromToRotation(childPosDeltaDir, childBlendedPosDeltaDir); Quaternion childRotDeltaPs = boneRotInv * childRotDelta; Vector4 childRotDeltaPsVec = QuaternionUtil.ToVector4(childRotDeltaPs); float weight = Mathf.Max(MathUtil.Epsilon, chain.MaxLengthFromRoot - childBone.LengthFromRoot); childRotDeltaPsVecAccumulator += weight * childRotDeltaPsVec; totalWeight += weight; } if (totalWeight > 0.0f) { Vector4 avgChildRotDeltaPsVec = childRotDeltaPsVecAccumulator / totalWeight; bone.RotationBackPropDeltaPs = QuaternionUtil.FromVector4(avgChildRotDeltaPsVec); bone.BlendedRotationWs = (boneRot * bone.RotationBackPropDeltaPs) * boneRot; } else if (bone.ParentIndex >= 0) { var parentBone = aBone[bone.ParentIndex]; bone.BlendedRotationWs = parentBone.BlendedRotationWs * (parentBone.RotationInverseWs * bone.CachedRotationWs); } } } // write blended position & adjusted rotation into final transforms for (int iBone = 0; iBone < aBone.Length; ++iBone) { var bone = aBone[iBone]; // skip fixed root if (iBone == 0 && !chain.LooseRoot) { bone.Instance.PositionSpring.Reset(bone.CachedPositionWs); bone.Instance.RotationSpring.Reset(bone.CachedRotationWs); continue; } bone.Transform.position = bone.BlendedPositionWs; bone.Transform.rotation = bone.BlendedRotationWs; bone.Transform.localScale = bone.BlendedScaleLs; } } }
public void AccumulateTarget(ref Params p, ref BoingEffector.Params effector) { Vector3 effectRefPos = effector.Bits.IsBitSet(BoingWork.EffectorFlags.ContinuousMotion) ? VectorUtil.GetClosestPointOnSegment(PositionOrigin, effector.PrevPosition, effector.CurrPosition) : effector.CurrPosition; Vector3 deltaPos = PositionOrigin - effectRefPos; Vector3 deltaPos3D = deltaPos; if (p.Bits.IsBitSet(ReactorFlags.TwoDDistanceCheck)) { switch (p.TwoDPlane) { case TwoDPlaneEnum.XY: deltaPos.z = 0.0f; break; case TwoDPlaneEnum.XZ: deltaPos.y = 0.0f; break; case TwoDPlaneEnum.YZ: deltaPos.x = 0.0f; break; } } bool inRange = Mathf.Abs(deltaPos.x) <= effector.Radius && Mathf.Abs(deltaPos.y) <= effector.Radius && Mathf.Abs(deltaPos.z) <= effector.Radius && deltaPos.sqrMagnitude <= effector.Radius * effector.Radius; if (!inRange) { return; } float deltaDist = deltaPos.magnitude; float tDeltaDist = effector.Radius - effector.FullEffectRadius > MathUtil.Epsilon ? 1.0f - Mathf.Clamp01((deltaDist - effector.FullEffectRadius) / (effector.Radius - effector.FullEffectRadius)) : 1.0f; Vector3 upWsPos = m_upWs; Vector3 upWsRot = m_upWs; Vector3 deltaDirPos = VectorUtil.NormalizeSafe(deltaPos3D, m_upWs); Vector3 deltaDirRot = deltaDirPos; if (p.Bits.IsBitSet(ReactorFlags.TwoDPositionInfluence)) { switch (p.TwoDPlane) { case TwoDPlaneEnum.XY: deltaDirPos.z = 0.0f; upWsPos.z = 0.0f; break; case TwoDPlaneEnum.XZ: deltaDirPos.y = 0.0f; upWsPos.y = 0.0f; break; case TwoDPlaneEnum.YZ: deltaDirPos.x = 0.0f; upWsPos.x = 0.0f; break; } if (upWsPos.sqrMagnitude < MathUtil.Epsilon) { switch (p.TwoDPlane) { case TwoDPlaneEnum.XY: upWsPos = Vector3.up; break; case TwoDPlaneEnum.XZ: upWsPos = Vector3.forward; break; case TwoDPlaneEnum.YZ: upWsPos = Vector3.up; break; } } else { upWsPos.Normalize(); } deltaDirPos = VectorUtil.NormalizeSafe(deltaDirPos, upWsPos); } if (p.Bits.IsBitSet(ReactorFlags.TwoDRotationInfluence)) { switch (p.TwoDPlane) { case TwoDPlaneEnum.XY: deltaDirRot.z = 0.0f; upWsRot.z = 0.0f; break; case TwoDPlaneEnum.XZ: deltaDirRot.y = 0.0f; upWsRot.y = 0.0f; break; case TwoDPlaneEnum.YZ: deltaDirRot.x = 0.0f; upWsRot.x = 0.0f; break; } if (upWsRot.sqrMagnitude < MathUtil.Epsilon) { switch (p.TwoDPlane) { case TwoDPlaneEnum.XY: upWsRot = Vector3.up; break; case TwoDPlaneEnum.XZ: upWsRot = Vector3.forward; break; case TwoDPlaneEnum.YZ: upWsRot = Vector3.up; break; } } else { upWsRot.Normalize(); } deltaDirRot = VectorUtil.NormalizeSafe(deltaDirRot, upWsRot); } if (p.Bits.IsBitSet(ReactorFlags.EnablePositionEffect)) { Vector3 moveVec = tDeltaDist * p.MoveReactionMultiplier * effector.MoveDistance * deltaDirPos; PositionTarget += moveVec; PositionSpring.Velocity += tDeltaDist * p.LinearImpulseMultiplier * effector.LinearImpulse * effector.LinearVelocityDir; } if (p.Bits.IsBitSet(ReactorFlags.EnableRotationEffect)) { Vector3 rotAxis = VectorUtil.NormalizeSafe(Vector3.Cross(upWsRot, deltaDirRot), VectorUtil.FindOrthogonal(upWsRot)); Vector3 rotVec = tDeltaDist * p.RotationReactionMultiplier * effector.RotateAngle * rotAxis; RotationTarget += QuaternionUtil.ToVector4(QuaternionUtil.FromAngularVector(rotVec)); Vector3 angularImpulseDir = VectorUtil.NormalizeSafe(Vector3.Cross(effector.LinearVelocityDir, deltaDirRot - 0.01f * Vector3.up), rotAxis); float angularImpulseMag = tDeltaDist * p.AngularImpulseMultiplier * effector.AngularImpulse; Vector4 angularImpulseDirQuat = QuaternionUtil.ToVector4(QuaternionUtil.FromAngularVector(angularImpulseDir)); RotationSpring.VelocityVec += angularImpulseMag * angularImpulseDirQuat; } ++m_numEffectors; }
public static bool SphereBox(Vector3 centerOffsetA, float radiusA, Vector3 halfExtentB, out Vector3 push) { push = Vector3.zero; Vector3 closestOnB = new Vector3 ( Mathf.Clamp(centerOffsetA.x, -halfExtentB.x, halfExtentB.x), Mathf.Clamp(centerOffsetA.y, -halfExtentB.y, halfExtentB.y), Mathf.Clamp(centerOffsetA.z, -halfExtentB.z, halfExtentB.z) ); Vector3 vec = centerOffsetA - closestOnB; float dd = vec.sqrMagnitude; if (dd > radiusA * radiusA) { return(false); } int numInBoxAxes = ((centerOffsetA.x <-halfExtentB.x || centerOffsetA.x> halfExtentB.x) ? 0 : 1) + ((centerOffsetA.y <-halfExtentB.y || centerOffsetA.y> halfExtentB.y) ? 0 : 1) + ((centerOffsetA.z <-halfExtentB.z || centerOffsetA.z> halfExtentB.z) ? 0 : 1); switch (numInBoxAxes) { case 0: // hit corner case 1: // hit edge case 2: // hit face { push = VectorUtil.NormalizeSafe(vec, Vector3.right) * (radiusA - Mathf.Sqrt(dd)); } break; case 3: // inside { Vector3 penetration = new Vector3 ( halfExtentB.x - Mathf.Abs(centerOffsetA.x) + radiusA, halfExtentB.y - Mathf.Abs(centerOffsetA.y) + radiusA, halfExtentB.z - Mathf.Abs(centerOffsetA.z) + radiusA ); if (penetration.x < penetration.y) { if (penetration.x < penetration.z) { push = new Vector3(Mathf.Sign(centerOffsetA.x) * penetration.x, 0.0f, 0.0f); } else { push = new Vector3(0.0f, 0.0f, Mathf.Sign(centerOffsetA.z) * penetration.z); } } else { if (penetration.y < penetration.z) { push = new Vector3(0.0f, Mathf.Sign(centerOffsetA.y) * penetration.y, 0.0f); } else { push = new Vector3(0.0f, 0.0f, Mathf.Sign(centerOffsetA.z) * penetration.z); } } } break; } return(true); }