Obi Official Forum
Help Getting Obi Rope GameObject from ObiSolver.Raycast - 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 Getting Obi Rope GameObject from ObiSolver.Raycast (/thread-3242.html)



Getting Obi Rope GameObject from ObiSolver.Raycast - Sanppy - 20-12-2021

Hey everyone,

I'm building a game in which Obi Ropes are supposed to be interactive objects that respond to mouse clicks.

I'm using the ObiSolver.Raycast example to determine whether a mouse click hit the rope and I'm able to get a QueryResult that seems to be correct for the relevant rope.
What I'm lacking is the next step in the process, and that is to get the GameObject associated with the QueryResult I got, e.g: the Obi Rope object.

Is there any way of doing that?

Just to give you an idea, here's my current code, based on the example, that uses the mouse position and successfully returns a relevant QueryResult 

Code:
if (Input.GetMouseButton(0))
        {

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            Debug.Log("X: " + ray.origin.x + "Y: " + ray.origin.y + "Z: " + ray.origin.z);
            Debug.DrawRay(ray.origin, ray.direction * 1000);

            int filter = ObiUtils.MakeFilter(ObiUtils.CollideWithEverything, 0);

            // perform a raycast, check if it hit anything:
            if (obiSolver.Raycast(ray, out QueryResult result, filter, 50, 0.1f))
            {
                // get the start and size of the simplex that was hit:
                int simplexStart = obiSolver.simplexCounts.GetSimplexStartAndSize(result.simplexIndex, out int simplexSize);

                // Debug draw the simplex:
                for (int i = 0; i < simplexSize; ++i)
                {
                    int particleIndex = obiSolver.simplices[simplexStart + i];
                    Vector3 pos = obiSolver.transform.TransformPoint(obiSolver.positions[particleIndex]);
                    Debug.DrawRay(pos, Vector3.up * 1000, Color.yellow);
                }
            }
        }


Thank you.

OK so I think I actually find out how to do it, essentially by using simplexStart, using it to find the particleIndex, and finally, using "particleToActor" with the particleIndex to fetch the ObiRope GameObject.

Here's how my current code looks like:
Code:
        if (Input.GetMouseButton(0))
        {

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            Debug.Log("X: " + ray.origin.x + "Y: " + ray.origin.y + "Z: " + ray.origin.z);
            Debug.DrawRay(ray.origin, ray.direction * 1000);

            int filter = ObiUtils.MakeFilter(ObiUtils.CollideWithEverything, 0);

            // perform a raycast, check if it hit anything:
            if (obiSolver.Raycast(ray, out QueryResult result, filter, 50, 0.1f))
            {
                //obiSolver.particleToActor
                // get the start and size of the simplex that was hit:
                int simplexStart = obiSolver.simplexCounts.GetSimplexStartAndSize(result.simplexIndex, out int simplexSize);

                int particleIndex = obiSolver.simplices[simplexStart];

                ObiRope rope = obiSolver.particleToActor[particleIndex].actor as ObiRope;

            }


I'm not sure if this is the optimal method. If Anyone has a better suggestion, I'd love to know about it.

Thanks.


RE: Getting Obi Rope GameObject from ObiSolver.Raycast - josemendez - 21-12-2021

(20-12-2021, 05:09 PM)Sanppy Wrote: I'm not sure if this is the optimal method. If Anyone has a better suggestion, I'd love to know about it.
Thanks.

That's 100% correct and the only way to do it: the raycast returns a simplex index, from that you get the particle index, and last you get the actor reference using the particleToActor array.

cheers!