Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get particles in a group?
#1
Hi

as the title says how do i get particles in a group from code?
would it be possible to post a snippet to illustrate how its done?

ultimately im looking to get the position of said particles

thanks
Reply
#2
(14-03-2023, 01:07 PM)tegleg Wrote: Hi

as the title says how do i get particles in a group from code?
would it be possible to post a snippet to illustrate how its done?

ultimately im looking to get the position of said particles

thanks

Hi!

A ObiParticleGroup contains a list of particle indices that you can just iterate:

Code:
var group = rope.blueprint.groups[0];

for (int i = 0; i < group.particleIndices.Count; ++i)
{
  int index = group.particleIndices[i];

  // given the index of the particle, get its current position:
  int indexInSolver = rope.solverIndices[index];
  var position = rope.solver.positions[indexInSolver];
}
Reply
#3
thank you
how do i get them with the group name?
Reply
#4
(14-03-2023, 04:23 PM)tegleg Wrote: thank you
how do i get them with the group name?

They're stored in a plain list, so you just loop trough the groups until you find the one with the name you're interested in.

There's usually not enough groups that O(N) lookup time by name would pose any problem. If it does (for example because you have thousands of groups and you need to look them up by name very often) you can insert them in a dictionary to look them up in constant time.
Reply
#5
ok thanks

is it possible to addforce to an individual particle?
or add force at position (giving the particle position)?

edit, solved it by deconstructing AddForce() and applying force to a single particle instead of them all
Reply
#6
(14-03-2023, 06:01 PM)tegleg Wrote: ok thanks

is it possible to addforce to an individual particle?
or add force at position (giving the particle position)?

edit, solved it by deconstructing AddForce() and applying force to a single particle instead of them all

Yes, it’s possible. You can get/set particle positions velocities, masses, forces and a lot other properties. See the particles API in the manual: http://obi.virtualmethodstudio.com/manua...icles.html
Reply
#7
thanks
is this best done in FixedUpdate?
there seems to be some fighting going on
Reply
#8
(14-03-2023, 09:54 PM)tegleg Wrote: thanks
is this best done in FixedUpdate?

Depends on where you're updating your solver. There's several ObiUpdater components available in Obi, the default one is ObiFixedUpdater so yes, setting particle properties in FixedUpdate is a good option.

If you want your code to work regardless of where you're updating physics, you can also use Obi's own solver callback events: http://obi.virtualmethodstudio.com/manua...olver.html

(14-03-2023, 09:54 PM)tegleg Wrote: there seems to be some fighting going on

What do you mean "fighting"?
Reply
#9
maybe substep is the right option, i had to do this with unreal engine in the past to make it behave. how would i add force from another script in substep?

perhaps i need to explain what i am doing
i am attempting to create essentially a hover vehicle using raycast suspension. the raycast starts from a particle on each corner and applies upwards force to the corner particles.
the technique works fine with unity's rigid body but produces 'jiggle' on the softbody. it is fighting the external forces.

i copied the code from AddForce() but apply it to a single particle like this
Code:
body.solver.externalForces[particleindex] += suspensionForce / body.solver.invMasses[particleindex];
it is very close to working properly

edit:
this works. thanks for the help
Code:
void Actor_OnPreSolverStep(ObiActor actor, float stepTime)
    {
        actor.solver.externalForces[particleindex] += suspensionForce / actor.solver.invMasses[particleindex];
    }
Reply