Trying to do a solar system

Hey, I’m trying to convert my very simple (and inefficient) gravity setup to ECS. How would I iterate through every gravity entity INSIDE the system Entities.ForEach thing? I’m really confused!

Could you post your code, would be easier to help.

here is the doc page

https://docs.unity3d.com/Packages/com.unity.entities@0.11/manual/ecs_entities_foreach.html

Here’s my quick test in Mono. It’s really crappy, but it’s just a test and it works ok. Each gravity entity adds itself to the gravityTransform list.

Each body loops through every other body and applies forces to it. The 2nd example is where I moved this to a more efficient gravityManager that better loops through bodies, which I imagine would work better for ECS type setup

    public void Update()
    {
        for( int i = 0; i<gravityBodies.Count; i++ )
        {
            direction = myTransform.position - gravityTransforms[i].position;
            //distance = direction.sqrMagnitude;
            distance = direction.magnitude;

            if( distance>10 ) // && distance < 1000)
            {
                //direction = direction/distance; // More efficient normalise (no sqrR)
                direction.Normalize();

                force = forceScalar * (direction * (body.mass * gravityBodies[i].mass) / (distance * (distance / 2)));
                gravityBodies[i].AddForce(force * Time.deltaTime);
                // totalGravityBodies[i].AddForce( force );
            }
        }
    }
    public void Update()
    {
        deltaTime = Time.deltaTime;
      
        for (int currentBody = 0; currentBody < bodies.Count; currentBody++)
        {
            for (int bodyToAffect = 0; bodyToAffect < bodies.Count; bodyToAffect++)
            {
                direction = bodies[currentBody].position - bodies[bodyToAffect].position;
                distance  = direction.magnitude;

                if (distance > 10) // && distance < 1000)
                {
                    //direction = direction/distance; // More efficient normalise (no sqrR)
                    direction.Normalize();

                    force = gravityComponents[currentBody].forceScalar *
                            (direction * (bodies[currentBody].mass * bodies[bodyToAffect].mass) /
                             (distance * (distance / 2)));
                    bodies[bodyToAffect].AddForce(force * deltaTime);
                    // forces[currentBody] += force;
                }
            }
        }

        // for (int i = 0; i < bodies.Count; i++)
        // {
            // bodies[i].AddForce(forces[i]); // * deltaTime);
        // }
    }

Here’s my attempt in DOTS. The inner foreach seems to work, but the impulse just isn’t applied (even with a hack massive number).
The entity from the Entities.ForEach can be moved with the same “ComponentExtensions.ApplyLinearImpulse” line though (outside the inner normal foreach)

    protected override void OnUpdate()
    {
        deltaTime = Time.DeltaTime;

        NativeArray<Entity> allEntities = EntityManager.GetAllEntities(Allocator.Temp);

        Entities.ForEach((Entity                currentEntity,
                          ref Translation       currentTranslation,
                          ref Gravity_DOTS_Data currentGravityData,
                          ref PhysicsMass       physicsMassCurrent,
                          ref PhysicsVelocity   physicsVelocity
                         ) =>
                         {
                             foreach (Entity affectedEntity in allEntities)
                             {
                                 if (EntityManager.HasComponent<Gravity_DOTS_Data>(affectedEntity))
                                 {
                                     Translation affectedTranslation =
                                         EntityManager.GetComponentData<Translation>(affectedEntity);
                                     PhysicsMass physicsMassAffected =
                                         EntityManager.GetComponentData<Unity.Physics.PhysicsMass>(affectedEntity);
                                     PhysicsVelocity physicsVelocityAffected =
                                         EntityManager.GetComponentData<Unity.Physics.PhysicsVelocity>(affectedEntity);

                                     direction = currentTranslation.Value - affectedTranslation.Value;

                                     // distance  = direction.magnitude;
                                     distance = math.distance(currentTranslation.Value,
                                                              affectedTranslation.Value);


                                     // if (distance > 10) // && distance < 1000)
                                     // {
                                     direction = math.normalize(direction);

                                     force = currentGravityData.forceScalar *
                                             (direction * (physicsMassCurrent.InverseMass *
                                                           physicsMassAffected.InverseMass) /
                                              (distance * (distance / 2)));

                                     ComponentExtensions.ApplyLinearImpulse(ref physicsVelocityAffected,
                                                                            physicsMassAffected,
                                                                            force * 1000f);
                                     // }
                                 }
                             }
                         }
                        );
    }