public void solveRotationVerlet(ref RigidbodyTransform parent, ref RigidbodyTransform child) { var diff = transform_.position_ - child.transform_.position_; if (diff.sqrMagnitude > 0f) { var up = parent.transform_.rotation_ * CV.Vector3Up; transform_.rotation_ = Quaternion.LookRotation(diff, up); } }
public void update(float dt, double update_time, ref RigidbodyTransform head_rigidbody) { for (var i = 0; i < nodes_.Length; ++i) { if (i == 0) { nodes_[i].update(dt, update_time, ref head_rigidbody /* dummy */, ref head_rigidbody, ref nodes_[i + 1].rigidbody_, true /* head */, false /* tail */); } else if (i == 1) { nodes_[i].update(dt, update_time, ref head_rigidbody, ref nodes_[i - 1].rigidbody_, ref nodes_[i + 1].rigidbody_, false /* head */, false /* tail */); } else if (i <= nodes_.Length - 2) { nodes_[i].update(dt, update_time, ref nodes_[i - 2].rigidbody_, ref nodes_[i - 1].rigidbody_, ref nodes_[i + 1].rigidbody_, false /* head */, false /* tail */); } else { nodes_[i].update(dt, update_time, ref nodes_[i - 2].rigidbody_, ref nodes_[i - 1].rigidbody_, ref nodes_[i - 1].rigidbody_ /* dummy */, false /* head */, true /* tail */); } } // lightball_.update(dt); }
public void restrictPositionVerlet(ref RigidbodyTransform grand_parent, ref RigidbodyTransform parent, float length, float max_degree) { var diff = parent.transform_.position_ - transform_.position_; // restriction for angle var rot_i = Utility.Inverse(ref grand_parent.transform_.rotation_); var local_diff = rot_i * diff; if (local_diff.z <= 0f) { return; } // var rad2 = local_diff.x*local_diff.x + local_diff.y*local_diff.y; // if (rad2 <= 0f) { // return; // } // var rad = Mathf.Sqrt(rad2); // var restrict = local_diff.z * Mathf.Tan(max_degree * Mathf.Deg2Rad); // if (rad > restrict) { var restrict = Mathf.Cos(max_degree * Mathf.Deg2Rad) * length; if (local_diff.z < restrict) { // local_diff.z = Mathf.Cos(max_degree * Mathf.Deg2Rad) * length; local_diff.z = restrict; var diff0 = grand_parent.transform_.rotation_ * local_diff; var new_pos = parent.transform_.position_ - diff0; transform_.position_ = new_pos; } // restriction for strech var len = diff.magnitude; if (len > 0f) { diff *= (1f - length / len); transform_.position_ += diff * 0.5f; parent.transform_.position_ -= diff * 0.5f; } }
public void update(float dt, double update_time, ref RigidbodyTransform grand_parent_rigidbody, ref RigidbodyTransform parent_rigidbody, ref RigidbodyTransform child_rigidbody, bool head, bool tail) { #if false if (MyCollider.getHitOpponentForEnemy(collider_) != MyCollider.Type.None) { rigidbody_.addTorque(MyRandom.Range(-20f, 20f), MyRandom.Range(-20f, 20f), MyRandom.Range(-20f, 20f)); } rigidbody_.addSpringTorque(ref parent_transform.rotation_, 10f /* torque_level */); rigidbody_.transform_.position_ = parent_transform.transformPosition(ref locator_); rigidbody_.update(dt); #else if (head) { rigidbody_.updateVerlet(dt, 0.8f /* damper */); } else { rigidbody_.updateVerlet(dt, 0.8f /* damper */); } if (head) { var head_offset = new Vector3(0f, 0f, -3f); var pos = parent_rigidbody.transform_.transformPosition(ref head_offset); rigidbody_.restrictPositionVerletFixed(ref pos, 0f /* length */); } else { rigidbody_.restrictPositionVerlet(ref grand_parent_rigidbody, ref parent_rigidbody, 3f /* length */, 60f /* max_degree */); } if (!tail) { rigidbody_.solveRotationVerlet(ref parent_rigidbody, ref child_rigidbody); } #endif MyCollider.updateEnemy(collider_, ref rigidbody_.transform_.position_); MyCollider.updateEnemyHoming(collider_homing_, ref rigidbody_.transform_.position_); if (-2f < rigidbody_.transform_.position_.y && rigidbody_.transform_.position_.y < 2f) { WaterSurface.Instance.makeBump(ref rigidbody_.transform_.position_, -0.025f /* value */, 1f /* size */); var wpos = rigidbody_.transform_.position_; wpos.y = -1.5f; var th = MyRandom.Range(0f, Mathf.PI * 2f); var cos = Mathf.Cos(th); var sin = Mathf.Sin(th); wpos.x += cos * 1.25f; wpos.z += sin * 1.25f; var vel = new Vector3(cos * 0.8f, MyRandom.Range(5f, 8f), sin * 0.8f); WaterSplash.Instance.spawn(ref wpos, ref vel, update_time); } if (MyRandom.Probability(0.1f)) { var pos = rigidbody_.transform_.position_; pos.x += MyRandom.Range(-1f, 1f); pos.z += MyRandom.Range(-1f, 1f); WaterSplash.Instance.spawn(ref pos, ref CV.Vector3Zero, update_time - 0.2f); } }