Obi Official Forum
Help Fluid Particles interaction Script - Printable Version

+- Obi Official Forum (https://obi.virtualmethodstudio.com/forum)
+-- Forum: Obi Users Category (https://obi.virtualmethodstudio.com/forum/forum-1.html)
+--- Forum: Obi Fluid (https://obi.virtualmethodstudio.com/forum/forum-3.html)
+--- Thread: Help Fluid Particles interaction Script (/thread-2579.html)

Pages: 1 2 3 4


RE: Fluid Particles interaction Script - JoseCarlos S - 27-11-2020

(27-11-2020, 03:16 PM)josemendez Wrote: You're not checking if the collider reported by the contact is the player, you're merely checking if the player exists. Since this is probably always true, any collider relatively close to the fluid will be destroyed.

Correct solution:

Code:
    void Solver_OnCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
    {
        var world = ObiColliderWorld.GetInstance();
        foreach (Oni.Contact contact in e.contacts)
        {
            // this one is an actual collision:
            if (contact.distance < 0.01)
            {
                ObiColliderBase collider = world.colliderHandles[contact.other].owner;
                if (collider == player)
                        GameObject.Destroy(collider.gameObject);
            }
        }
    }


Nice! It works! If i change the script to be for the emitter instead of the solver, can i get this interaction with one fluid but not the other? Right now it dies when touching any fluid, and i whant it killing itself when touching certain fluids/emitters


RE: Fluid Particles interaction Script - josemendez - 27-11-2020

(27-11-2020, 03:31 PM)JoseCarlos S Wrote: Nice! It works! If i change the script to be for the emitter instead of the solver, can i get this interaction with one fluid but not the other? Right now it dies when touching any fluid, and i whant it killing itself when touching certain fluids/emitters

You can't just add this script to a emitter, programming doesn't work that way :/.
You should check if the particle belongs to a certain emitter, depending on the result you decide if you destroy the collider or not. To retrieve the emitter a particle belongs to, you can do:

Code:
ObiSolver.ParticleInActor pa = solver.particleToActor[contact.particle];
ObiEmitter emitter = pa.actor as ObiEmitter;

See "Retrieving the actor involved in a contact": http://obi.virtualmethodstudio.com/tutorials/scriptingcollisions.html


RE: Fluid Particles interaction Script - JoseCarlos S - 27-11-2020

(27-11-2020, 03:38 PM)josemendez Wrote: You can't just add this script to a emitter, programming doesn't work that way :/.
You should check if the particle belongs to a certain emitter, depending on the result you decide if you destroy the collider or not. To retrieve the emitter a particle belongs to, you can do:

Code:
ObiSolver.ParticleInActor pa = solver.particleToActor[contact.particle];
ObiEmitter emitter = pa.actor as ObiEmitter;

See "Retrieving the actor involved in a contact": http://obi.virtualmethodstudio.com/tutorials/scriptingcollisions.html


Should look something like this? (No reaction to any fluid/emitter yet):

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Obi;

[RequireComponent(typeof(ObiSolver))]
public class Collision2 : MonoBehaviour
{

    ObiSolver solver;
    public ObiCollider2D player;
    public ObiEmitter emitter;
   

    Obi.ObiSolver.ObiCollisionEventArgs collisionEvent;

    void Awake()
    {
        solver = GetComponent<Obi.ObiSolver>();
       
    }

    void OnEnable()
    {
        solver.OnCollision += Solver_OnCollision;
    }

    void OnDisable()
    {
        solver.OnCollision -= Solver_OnCollision;
    }

    void Solver_OnCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
    {
        var world = ObiColliderWorld.GetInstance();
        foreach (Oni.Contact contact in e.contacts)
        {
            // this one is an actual collision:
            if (contact.distance < 0.01)
            {
                ObiSolver.ParticleInActor pa = solver.particleToActor[contact.particle];
                ObiEmitter emitter = pa.actor as ObiEmitter;

                ObiColliderBase collider = world.colliderHandles[contact.other].owner;
                if (collider == player && emitter == player)
                    GameObject.Destroy(collider.gameObject);
            }
        }
    }

}


RE: Fluid Particles interaction Script - josemendez - 27-11-2020

hmm.. I get the impression that you don't understand the code at all  Huh

(27-11-2020, 04:00 PM)JoseCarlos S Wrote: if (collider == player && emitter == player)

This line makes no sense whatsoever. You're comparing objects of different types (which won't ever be equal to each other). "emitter" is of type ObiEmitter, and "player" is of type ObiCollider2D. A emitter is not a collider, so this will always be false.

Moreover, you've declared the emitter variable twice: once as a public member of the script, and another within the Solver_OnCollision method. This script won't compile.

At this point I should really insist that you invest some time learning basic programming, as I can't possibly handhold you trough every line of code you write. Not that I have anything against helping you, it's just that I don't have the resources to do that. This forum is for help with Obi-related issues only.


RE: Fluid Particles interaction Script - JoseCarlos S - 27-11-2020

(27-11-2020, 04:04 PM)josemendez Wrote: hmm.. I get the impression that you don't understand the code at all  Huh


This line makes no sense whatsoever. You're comparing objects of different types (which won't ever be equal to each other). "emitter" is of type ObiEmitter, and "player" is of type ObiCollider2D. A emitter is not a collider, so this will always be false.

Moreover, you've declared the emitter variable twice: once as a public member of the script, and another within the Solver_OnCollision method. This script won't compile.

At this point I should really insist that you invest some time learning basic programming, as I can't possibly handhold you trough every line of code you write. Not that I have anything against helping you, it's just that I don't have the resources to do that. This forum is for help with Obi-related issues only.


I have the attention span of monkey and the awareness of a toddler... Lengua Sorry for the stupid questions i asked during the thread, and yea, i have a lot to learn about programing. I think i managed to make it work tho:


using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Obi;

[RequireComponent(typeof(ObiSolver))]
public class Collision2 : MonoBehaviour
{

    ObiSolver solver;
    public ObiCollider2D player;
    public ObiEmitter fluid;
   

    Obi.ObiSolver.ObiCollisionEventArgs collisionEvent;

    void Awake()
    {
        solver = GetComponent<Obi.ObiSolver>();
       
    }

    void OnEnable()
    {
        solver.OnCollision += Solver_OnCollision;
    }

    void OnDisable()
    {
        solver.OnCollision -= Solver_OnCollision;
    }

    void Solver_OnCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
    {
        var world = ObiColliderWorld.GetInstance();
        foreach (Oni.Contact contact in e.contacts)
        {
            // this one is an actual collision:
            if (contact.distance < 0.01)
            {
                ObiSolver.ParticleInActor pa = solver.particleToActor[contact.particle];
                ObiEmitter emitter = pa.actor as ObiEmitter;
                if (pa.actor == fluid)
                {
                    ObiColliderBase collider = world.colliderHandles[contact.other].owner;
                    if (collider == player)
                        GameObject.Destroy(collider.gameObject);
                }
            }
        }
    }

}
The player dies when touching the "fluid" emitter. Posted it just for others to see so it might help them... With this i solved my main problems with obi for my proyect, dont know if ill post any question again, but for the time being ill let you rest Jose Gran sonrisa(its been a long fkn thread) Thanks for all the help and the patience, i wish all forums where like this one...

Cheers.


RE: Fluid Particles interaction Script - josemendez - 02-12-2020

You got the code right, man Sonrisa. Keep up the good work!