08-01-2024, 03:53 PM
(This post was last modified: 08-01-2024, 04:03 PM by Alexander34.)
Greetings. I have not found a normal and stable way to copy ropes. I need to have two ropes in the same solver, one of them is always on. When I turn off one and turn on the other, I need to copy the position and number of particles. Visually it should look like one rope is replaced by another, but remains in the same state as the previous one. I don't want to use the ObiParticleRenderer solution, I want to copy all positions of the previous rope to the one we are going to turn on. As far as I know, the positions are accessed via solver.positions, but if I understand correctly, these are the positions of both ropes being simulated.
Here's the code I use to create a rope between father and son in real time. It is created in a straight line from son to father. I also have a second rope with a different physical behavior that gravitates to points of interest in zero gravity. At some point I turn off the zero gravity thread and load other settings into solver, while turning on the second thread. I need in some cases for the thread with gravity to replicate the thread without gravity, but at the same time, sometimes I need it the way it already works.
Code:
public void AttachRope()
{
_blueprint = ScriptableObject.CreateInstance<ObiRopeBlueprint>();
var fatherConnectionPoint = _playersContainer[PlayerCharacter.Father].ConnectionPoint;
var sonConnectionPoint = _playersContainer[PlayerCharacter.Son].ConnectionPoint;
Vector3 father = ObiRope.transform.InverseTransformPoint(fatherConnectionPoint.position);
Vector3 son = ObiRope.transform.InverseTransformPoint(sonConnectionPoint.position);
int allLayers = (1 << 16) - 1;
int excludeLayer15 = ~(1 << 15);
int finalMask = allLayers & excludeLayer15;
int filter = ObiUtils.MakeFilter(finalMask, 1);
_blueprint.path.Clear();
_blueprint.path.AddControlPoint(father, -father.normalized, father.normalized, Vector3.zero, 0.1f, 0.1f, 1, filter, Color.white, "start");
_blueprint.path.AddControlPoint(son, -son.normalized, son.normalized, Vector3.zero, 0.1f, 0.1f, 1, filter, Color.white, "end");
_blueprint.path.FlushEvents();
_blueprint.Generate();
ObiRope.ropeBlueprint = _blueprint;
var pinConstraints = ObiRope.GetConstraintsByType(Oni.ConstraintType.Pin) as ObiConstraints<ObiPinConstraintsBatch>;
pinConstraints.Clear();
var batch = new ObiPinConstraintsBatch();
batch.AddConstraint(ObiRope.solverIndices[0], _fatherConnectionPointObiCollider, Vector3.zero, Quaternion.identity, 0, 0, float.PositiveInfinity);
batch.AddConstraint(ObiRope.solverIndices[_blueprint.activeParticleCount - 1], _sonConnectionPointObiCollider, Vector3.zero, Quaternion.identity, 0, 0, float.PositiveInfinity);
batch.activeConstraintCount = 2;
pinConstraints.AddBatch(batch);
ObiRope.SetConstraintsDirty(Oni.ConstraintType.Pin);
}