Obi Official Forum
Help Instantiate from Prefab - Each end into position - Printable Version

+- Obi Official Forum (https://obi.virtualmethodstudio.com/forum)
+-- Forum: Obi Users Category (https://obi.virtualmethodstudio.com/forum/forum-1.html)
+--- Forum: Obi Rope (https://obi.virtualmethodstudio.com/forum/forum-4.html)
+--- Thread: Help Instantiate from Prefab - Each end into position (/thread-618.html)



Instantiate from Prefab - Each end into position - Jerth - 12-06-2018

Hey feel free to send me to other places to do more reading. Or re-reading. I may have missed the answer elsewhere! But I have been struggling for a week now.

I have a similar goal to the user in this thread although I'm not getting a flat crash. Sometimes I get a performance bog down and THEN crash... Anyway. 

Idea  TL;DR I am simply trying to spawn a rope from a prefab, in precise position, but it keeps tearing.

Huh The Problem, longer version: 
  • I have prefab of a rope just the way I like it. Would rather not generate the rope from code (using the stuff in the Helper script). Anyway, it is in a gameobject with its own solver. 
  • And most importantly, this prefab rope has two gameobject children: call them BlockA and BlockB. These blocks represent the start and end of the rope. Each particle tip of the rope is constrained to their blocks. So far so straightforward.
  • The idea is... tie TWO objects together easily. By attaching / childing the blocks to those objects. I can use the ObiCursor to changelength no problem.
  • In other words... I aim to create a function that accepts two vector3 positions and then creates an appropriately sized rope between them. Or in other words again, imagine two objects floating around, but then when the user presses SPACEBAR the two objects have the prefab rope spawned between them, each with a Block childed' to it.

Ángel
  1. First question: What do I need to watch out for? Surely connecting two objects at runtime is a somewhat common application of Obi Rope.
Spawning a rope prefab connecting two objects (mostly by managing Gameobject block ends) has proven difficult! I mean, I have learned in these forums (or, Taiga forums) I have to disable the actor to teleport rope. OK. And at one point I crashed my computer with one batch of code - and no, it wasn't using any WHILE loops  Triste  I think it was partly because I tried turning TEARABLE = false. But that's not the only reason my system bugged out I'm sure. It's not a parent/Child feedback thing either I don't think; because the Blocks do not REMAIN children in my code - they are parented away to OTHER objects. So they shouldn't be moving with the prefab anymore - which would move the rope - which would move the transform and cause vicious feedback I'm sure...

Anyway. I do think part of the problem is that I transform the ends of the rope in just a couple frames. I mean, I have to do this because I want the rope to spawn apparently instantaneously. Unfortunately right now I do this: (a) Spawn the rope in approximately the right space, but then (b) Quickly teleport the edges into position, and then the rope TEARS every time.

So...
      
     2. Second Question: Suppose I'm doing everything else right. But my rope is indeed being torn by the act of transforming the blocks to which its ends are constrained... is there another way to get each end immediately into their proper position? For that matter, I may need to iterate through each particle and specify its location - so the whole thing is in position... Is that it?

PS.
If you can see the picture attached, that might help. Note that the final position of the rope might mean using the ObiCursor real quick too. Because the desired length might be longer or shorter. But again, getting the correct length has not been much of a problem for me.
[attachment=144]

Any help would be appreciated.


RE: Instantiate from Prefab - Each end into position - josemendez - 12-06-2018

(12-06-2018, 05:55 PM)Jerth Wrote: Hey feel free to send me to other places to do more reading. Or re-reading. I may have missed the answer elsewhere! But I have been struggling for a week now.

I have a similar goal to the user in this thread although I'm not getting a flat crash. Sometimes I get a performance bog down and THEN crash... Anyway. 

Idea  TL;DR I am simply trying to spawn a rope from a prefab, in precise position, but it keeps tearing.

Huh The Problem, longer version: 
  • I have prefab of a rope just the way I like it. Would rather not generate the rope from code (using the stuff in the Helper script). Anyway, it is in a gameobject with its own solver. 
  • And most importantly, this prefab rope has two gameobject children: call them BlockA and BlockB. These blocks represent the start and end of the rope. Each particle tip of the rope is constrained to their blocks. So far so straightforward.
  • The idea is... tie TWO objects together easily. By attaching / childing the blocks to those objects. I can use the ObiCursor to changelength no problem.
  • In other words... I aim to create a function that accepts two vector3 positions and then creates an appropriately sized rope between them. Or in other words again, imagine two objects floating around, but then when the user presses SPACEBAR the two objects have the prefab rope spawned between them, each with a Block childed' to it.

Ángel
  1. First question: What do I need to watch out for? Surely connecting two objects at runtime is a somewhat common application of Obi Rope.
Spawning a rope prefab connecting two objects (mostly by managing Gameobject block ends) has proven difficult! I mean, I have learned in these forums (or, Taiga forums) I have to disable the actor to teleport rope. OK. And at one point I crashed my computer with one batch of code - and no, it wasn't using any WHILE loops  Triste  I think it was partly because I tried turning TEARABLE = false. But that's not the only reason my system bugged out I'm sure. It's not a parent/Child feedback thing either I don't think; because the Blocks do not REMAIN children in my code - they are parented away to OTHER objects. So they shouldn't be moving with the prefab anymore - which would move the rope - which would move the transform and cause vicious feedback I'm sure...

Anyway. I do think part of the problem is that I transform the ends of the rope in just a couple frames. I mean, I have to do this because I want the rope to spawn apparently instantaneously. Unfortunately right now I do this: (a) Spawn the rope in approximately the right space, but then (b) Quickly teleport the edges into position, and then the rope TEARS every time.

So...
      
     2. Second Question: Suppose I'm doing everything else right. But my rope is indeed being torn by the act of transforming the blocks to which its ends are constrained... is there another way to get each end immediately into their proper position? For that matter, I may need to iterate through each particle and specify its location - so the whole thing is in position... Is that it?

PS.
If you can see the picture attached, that might help. Note that the final position of the rope might mean using the ObiCursor real quick too. Because the desired length might be longer or shorter. But again, getting the correct length has not been much of a problem for me.


Any help would be appreciated.

Hi,

Imho your approach of using prefabs is fundamentally flawed: what happens if the objects you want to attach using the rope are further apart than the prefab rope's length? the rope will stretch instantaneously upon starting simulation, and will most likely tear apart (depending on your tear threshold). This will only work if the distance between the objects you want to attach is approximately the same as the rope length. It will also fail if the objects are in different positions than the rope's ends, as you'd need to move the rope particles around pretty quickly. It is much easier to procedurally generate your rope with the correct position/length right from the start.

Obi includes a sample script to do just this: procedurally create a rope between two transforms: /Obi/Scripts/Utils/ObiRopeHelper. It might prove useful.


RE: Instantiate from Prefab - Each end into position - Jerth - 16-06-2018

(12-06-2018, 06:58 PM)josemendez Wrote: Hi,

Imho your approach of using prefabs is fundamentally flawed: what happens if the objects you want to attach using the rope are further apart than the prefab rope's length? the rope will stretch instantaneously upon starting simulation, and will most likely tear apart (depending on your tear threshold). This will only work if the distance between the objects you want to attach is approximately the same as the rope length. It will also fail if the objects are in different positions than the rope's ends, as you'd need to move the rope particles around pretty quickly. It is much easier to procedurally generate your rope with the correct position/length right from the start.

Obi includes a sample script to do just this: procedurally create a rope between two transforms: /Obi/Scripts/Utils/ObiRopeHelper. It might prove useful.

You're right. The HELPER script was not as intimidating as I thought, and quite elegant. Thanks josemendez.

Corazón

A new problem I'm running into is that I crash my Unity (and computer lol) when I try to use an OFFSET on my constraints  Huh


Code:
constraintsBatch.AddConstraint(rope.UsedParticles - 1, targetCube.GetComponent<ObiCollider>(), Vector3.zero, 0f);


That works fine but if I replace "Vector3.zero" with a calculated offset... BAM.
Any tips off the bat??


RE: Instantiate from Prefab - Each end into position - josemendez - 16-06-2018

(16-06-2018, 03:03 AM)Jerth Wrote: You're right. The HELPER script was not as intimidating as I thought, and quite elegant. Thanks josemendez.

Corazón

A new problem I'm running into is that I crash my Unity (and computer lol) when I try to use an OFFSET on my constraints  Huh


Code:
constraintsBatch.AddConstraint(rope.UsedParticles - 1, targetCube.GetComponent<ObiCollider>(), Vector3.zero, 0f);


That works fine but if I replace "Vector3.zero" with a calculated offset... BAM.
Any tips off the bat??

The offset is expressed in the collider's local space. Make sure you're not passing a world space value, as it might be extremely large, causing huge torques to develop. Anyway, in 3.4 and up large torques/velocities should not cause a crash.