Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rope created at runtime is longer than the distance between the two connection points
#1
[Image: img.png]

Code:
            const string STAND_PARTICLE_GROUP = "PointOnStand";
            const string TARGET_PARTICLE_GROUP = "PointOnTarget";

            int standFilter = Obi.ObiUtils.MakeFilter(Obi.ObiUtils.CollideWithEverything, 0);
            int targetFilter = Obi.ObiUtils.MakeFilter(Obi.ObiUtils.CollideWithNothing, 0);

            var localHit = pointOnStand.transform.InverseTransformPoint(pointOnTarget.position);

            var blueprint = ScriptableObject.CreateInstance<Obi.ObiRopeBlueprint>();
            blueprint.resolution = 0.5f;

            blueprint.path.Clear();
            blueprint.path.AddControlPoint(Vector3.zero, -localHit.normalized, localHit.normalized, Vector3.up, 0.1f, 0.1f, thickness, standFilter, UnityEngine.Color.white, STAND_PARTICLE_GROUP);
            blueprint.path.AddControlPoint(localHit, -localHit.normalized, localHit.normalized, Vector3.up, 0.1f, 0.1f, thickness, targetFilter, UnityEngine.Color.white, TARGET_PARTICLE_GROUP);
            blueprint.path.FlushEvents();

            var generate = blueprint.Generate();
            while (generate.MoveNext()) ;

            var ropeGameObject = new GameObject("Rope",
                typeof(Obi.ObiRope),
                typeof(Obi.ObiPathSmoother),
                typeof(Obi.ObiRopeChainRenderer));

            ropeGameObject.transform.SetParent(solver.transform);

            var ropeChainRenderer = ropeGameObject.GetComponent<Obi.ObiRopeChainRenderer>();
            ropeChainRenderer.linkPrefabs.Add(chainPrefab);
            ropeChainRenderer.sectionTwist = 90;
            ropeChainRenderer.linkScale = Vector3.one;

            var rope = ropeGameObject.GetComponent<Obi.ObiRope>();
            rope.ropeBlueprint = blueprint;

            if (!standEntity.ObiStandColliders.TryGetValue(pointOnStand, out var standObiColliderGO))
            {
                standObiColliderGO = GameObjectPool.For(m_StandObiColliderPrefab).Rent();
                standObiColliderGO.transform.SetParent(pointOnStand.parent);
                standObiColliderGO.transform.localScale = Vector3.one * 0.005f;
                standEntity.ObiStandColliders[pointOnStand] = standObiColliderGO;
            }

            standObiColliderGO.transform.position = pointOnStand.position;
            standObiColliderGO.transform.rotation = pointOnStand.rotation;
            standObiColliderGO.transform.localScale = pointOnStand.localScale;

            var standObiCollider = standObiColliderGO.GetComponent<Obi.ObiColliderBase>();

            var pinConstraints = rope.GetConstraintsByType(Oni.ConstraintType.Pin) as Obi.ObiConstraints<Obi.ObiPinConstraintsBatch>;
            pinConstraints.Clear();
            var batch = new Obi.ObiPinConstraintsBatch();
            batch.AddConstraint(rope.solverIndices[0], standObiCollider, Vector3.zero, Quaternion.identity, 0, 0, float.PositiveInfinity);
            batch.AddConstraint(
                rope.solverIndices[blueprint.activeParticleCount - 1],
                target.GetComponent<Obi.ObiColliderBase>(),
                target.InverseTransformPoint(pointOnTarget.position),
                Quaternion.identity,
                0,
                0,
                float.PositiveInfinity);
            batch.activeConstraintCount = 2;
            pinConstraints.AddBatch(batch);
            rope.SetConstraintsDirty(Oni.ConstraintType.Pin);

I'm basically trying to create a chain between these two objects at runtime. The length of the chain should be the distance between those two red X's which mark "mount points" to create the chain between. The circle object isn't "hanging" down because I currently have the rigidbody set to kinematic.

Any ideas?
Reply


Messages In This Thread
Rope created at runtime is longer than the distance between the two connection points - by NailEngine - 05-04-2022, 10:38 PM