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 }
void FixedUpdate() { float dt = Time.fixedDeltaTime; Vector3 linearInputVec = Vector3.zero; if (Input.GetKey(KeyCode.W)) { linearInputVec += Vector3.forward; } if (Input.GetKey(KeyCode.S)) { linearInputVec += Vector3.back; } if (Input.GetKey(KeyCode.A)) { linearInputVec += Vector3.left; } if (Input.GetKey(KeyCode.D)) { linearInputVec += Vector3.right; } if (Input.GetKey(KeyCode.R)) { linearInputVec += Vector3.up; } if (Input.GetKey(KeyCode.F)) { linearInputVec += Vector3.down; } bool linearThrustOn = linearInputVec.sqrMagnitude > MathUtil.Epsilon; if (linearThrustOn) { linearInputVec = linearInputVec.normalized * LinearThrust; m_linearVelocity += linearInputVec * dt; m_linearVelocity = VectorUtil.ClampLength(m_linearVelocity, 0.0f, MaxLinearSpeed); } else { m_linearVelocity = VectorUtil.ClampLength(m_linearVelocity, 0.0f, Mathf.Max(0.0f, m_linearVelocity.magnitude - LinearDrag * dt)); } float speed = m_linearVelocity.magnitude; float tSpeed = speed * MathUtil.InvSafe(MaxLinearSpeed); Quaternion tiltRot = Quaternion.identity; float tHorizontal = 1.0f; float tHorizontalSpeed = 0.0f; if (speed > MathUtil.Epsilon) { Vector3 flatVel = m_linearVelocity; flatVel.y = 0.0f; tHorizontal = m_linearVelocity.magnitude > 0.01f ? 1.0f - Mathf.Clamp01(Mathf.Abs(m_linearVelocity.y) / m_linearVelocity.magnitude) : 0.0f; tHorizontalSpeed = Mathf.Min(1.0f, speed / Mathf.Max(MathUtil.Epsilon, MaxLinearSpeed)) * tHorizontal; Vector3 tiltAxis = Vector3.Cross(Vector3.up, flatVel).normalized; float tiltAngle = Tilt * MathUtil.Deg2Rad * tHorizontalSpeed; tiltRot = QuaternionUtil.AxisAngle(tiltAxis, tiltAngle); } float angularInput = 0.0f; if (Input.GetKey(KeyCode.Q)) { angularInput -= 1.0f; } if (Input.GetKey(KeyCode.E)) { angularInput += 1.0f; } bool largerMaxAngularSpeed = Input.GetKey(KeyCode.LeftControl); bool angularThurstOn = Mathf.Abs(angularInput) > MathUtil.Epsilon; if (angularThurstOn) { float maxAngularSpeed = MaxAngularSpeed * (largerMaxAngularSpeed ? 2.5f : 1.0f); angularInput *= AngularThrust * MathUtil.Deg2Rad; m_angularVelocity += angularInput * dt; m_angularVelocity = Mathf.Clamp(m_angularVelocity, -maxAngularSpeed * MathUtil.Deg2Rad, maxAngularSpeed * MathUtil.Deg2Rad); } else { m_angularVelocity -= Mathf.Sign(m_angularVelocity) * Mathf.Min(Mathf.Abs(m_angularVelocity), AngularDrag * MathUtil.Deg2Rad * dt); } m_yawAngle += m_angularVelocity * dt; Quaternion yawRot = QuaternionUtil.AxisAngle(Vector3.up, m_yawAngle); m_hoverCenter += m_linearVelocity * dt; m_hoverPhase += Time.deltaTime; Vector3 hoverVec = 0.05f * Mathf.Sin(1.37f * m_hoverPhase) * Vector3.right + 0.05f * Mathf.Sin(1.93f * m_hoverPhase + 1.234f) * Vector3.forward + 0.04f * Mathf.Sin(0.97f * m_hoverPhase + 4.321f) * Vector3.up; hoverVec *= Hover; Quaternion hoverQuat = Quaternion.FromToRotation(Vector3.up, hoverVec + Vector3.up); transform.position = m_hoverCenter + hoverVec; transform.rotation = tiltRot * yawRot * hoverQuat; if (Motor != null) { float motorAngularSpeedDeg = Mathf.Lerp(MotorBaseAngularSpeed, MotorMaxAngularSpeed, tHorizontalSpeed); m_motorAngle += motorAngularSpeedDeg * MathUtil.Deg2Rad * dt; Motor.localRotation = QuaternionUtil.AxisAngle(Vector3.up, m_motorAngle - m_yawAngle); } if (BubbleEmitter != null) { var emission = BubbleEmitter.emission; emission.rateOverTime = Mathf.Lerp(BubbleBaseEmissionRate, BubbleMaxEmissionRate, tSpeed); } if (Eyes != null) { m_blinkTimer -= dt; if (m_blinkTimer <= 0.0f) { bool doubleBlink = !m_lastBlinkWasDouble && Random.Range(0.0f, 1.0f) > 0.75f; m_blinkTimer = doubleBlink ? 0.2f : BlinkInterval + Random.Range(1.0f, 2.0f); m_lastBlinkWasDouble = doubleBlink; m_eyeScaleSpring.Value.y = 0.0f; m_eyePositionLsSpring.Value.y -= 0.025f; } Eyes.localScale = m_eyeScaleSpring.TrackDampingRatio(m_eyeInitScale, 30.0f, 0.8f, dt); Eyes.localPosition = m_eyePositionLsSpring.TrackDampingRatio(m_eyeInitPositionLs, 30.0f, 0.8f, dt); } }