Obi Official Forum
How to add external force to particular particles? - 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: How to add external force to particular particles? (/thread-419.html)



How to add external force to particular particles? - dimmduh - 28-02-2018

I created custom class from ObiExternalForce, added my solver. But it does not work. Do you have any idea how to add external force to particular particles?

Code:
using Obi;
using UnityEngine;

public class ObiSnakeExternalForce : ObiExternalForce
{
   public float k = 100;
   public override void ApplyForcesToActor(ObiActor actor)
   {
       var force= new Vector4(0, k, 0, 1);
       force[3] = 1;//actor.UsesCustomExternalForces ? 1 : 0;
       Oni.AddParticleExternalForce(actor.Solver.OniSolver, ref force, actor.particleIndices, actor.particleIndices.Length);
   }
}



RE: How to add external force to particular particles? - dimmduh - 07-03-2018

Bump.
Please, help  Triste


RE: How to add external force to particular particles? - josemendez - 07-03-2018

(07-03-2018, 03:26 AM)dimmduh Wrote: Bump.
Please, help  Triste

Hi,

You should uncomment the //actor.UsesCustomExternalForces ? 1 : 0; line. Most actors do not use a custom way to apply external forces, in fact as of now only cloth does (forces are applied to cloth triangles instead of particles).

Also, Oni.AddParticleExternalForce takes an array of forces (Vector4[]), one for each particle index passed in the second argument. Since you're passing all particles in the actor (actor.particleIndices) but only one force, the call will have no effect. In fact, it can potentially crash your app since the size of the array you're passing does not match the size you SAY you're passing, which will most likely cause an access violation.

If you wanted to add force to a single particle instead, you should then call it like this:
Code:
Oni.AddParticleExternalForces(actor.Solver.OniSolver,new Vector4[]{force},actor.particleIndices[index],1); //index runs from 0 to actor.particleIndices.Length