Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Obi Stitcher issue with RemoveStitch
#1
Hi, sorry I have yet another question.

I have been trying out the ObiStitcher component, for similar use as this thread:
http://obi.virtualmethodstudio.com/forum...-3838.html

Was able to add stitches by script without issue.
I am having problems removing stitches though.

Snippet of code used to RemoveStitch one by one
Code:
int initStitchCount = stitcher.StitchCount;

for (int i = 0; i < initStitchCount - 1; i++)
{
    stitcher.RemoveStitch(i);
        stitcher.PushDataToSolver();
}

Issue 1: Particles remain stuck together after stitch is removed
- this is during runtime
- run above script, particles remain stuck together.
- go to ObiStitcher inspector, press "Clear all stitches", particles remain stuck together

Issue 2: Seem only able to remove half the total stitches (?)
- above script stops removing stitches at the halfway mark
(eg I have 81 stitches in my test sample, remaining stitches stays stuck at 40)
- I notice it removes every other stitch (1, 3, 5...)

I'm not sure what to do, I could not find relevant documentation.

Thanks in advance.
Reply
#2
(21-07-2023, 11:06 AM)danazoid Wrote: Was able to add stitches by script without issue.
I am having problems removing stitches though.

You need to call solver.SetConstraintsDirty(Oni.ConstraintType.Stitch); afterwards, otherwise changes to stitch constraints at runtime won't have any effect.


(21-07-2023, 11:06 AM)danazoid Wrote: Issue 2: Seem only able to remove half the total stitches (?)
- above script stops removing stitches at the halfway mark
(eg I have 81 stitches in my test sample, remaining stitches stays stuck at 40)
- I notice it removes every other stitch (1, 3, 5...)

This is a basic programming pitfall: removing elements from a collection while iterating trough it will skip elements unless you're careful. Think about this:

- You remove element at position N, so all elements after it move one position back to fill the gap: N+1 moves to N, N+2 moves to N+1, and so on.
- Your for loop advances to the next element, N+1.
- As a result, the element that was in position N+1 and that moved to N after deleting the one that was there is skipped.
- Do this for all elements, and you end up removing exactly half of them.

So if you want to remove all elements from a collection in for loop, the typical solution is to do so backwards (iterating from the end to the start). This is clean and very efficient. Another options are:
-If you want to delete all elements, just delete the first one until there's no elements left. This is a lot costlier than iterating backwards though, since each deletion moves all elements after it one position back. If you move backwards trough the array deleting the last element every time, there's no need to move any elements.
-Decrement the counter (i--) after deleting an element, but this is results in quite ugly code.

Note this applies to programming in general, regardless of the language:

https://stackoverflow.com/questions/3666...r-use-i-or
https://stackoverflow.com/questions/9362...javascript
https://www.reddit.com/r/godot/comments/...y_iterate/
Reply
#3
(21-07-2023, 11:22 AM)josemendez Wrote: You need to call solver.SetConstraintsDirty(Oni.ConstraintType.Stitch); afterwards, otherwise changes to stitch constraints at runtime won't have any effect.

Thanks! I got it working!
But with actor.SetConstraintsDirty(Oni.ConstraintType.Stitch), because I couldn't find the method in solver.

Another thing I found was that I have to use my VR headset to see it unstitch. For some reason if I go to (Unity) Scene view during runtime (and not paused), the unstitching happens but there is no particle movement.

Quote:This is a basic programming pitfall: removing elements from a collection while iterating trough it will skip elements unless you're careful.

Ah! Thanks for the explanation and the workaround!
Reply