#Whats an efficient way to make a memory copy of some components from entities

1 messages · Page 1 of 1 (latest)

sonic matrix
#

For each frame, I need to compute the directions of my units based on the Position and LinearVelocity of all other units. (Similar to the swarm (boid) simulation).
If I allocate a NativeArray like the one below, can I assume they are in the same order?

Furthermore, how do I get access to a LinearVelocity component? It is part of the RigidBodyAspect but I cannot use it to fill a NativeArray.

NativeArray<LocalTransform> localTransformMemory = m_WalkingAgents.ToComponentDataArray<LocalTransform>(Allocator.TempJob);
        NativeArray<Physics.LinearVelocity> localVelocityMemory = m_WalkingAgents.ToComponentDataArray<LocalTransform>(Allocator.TempJob);

If that assumption does not hold. How do I best create a data structure that allows me to create a copy of these two types of data efficiently.

Other approaches could be:

  1. Using a NativeParallelHashMap<> for each data type with Entity as the key. I am not sure how an efficient data allocation would look, similar to ToComponentDataArray.
  2. Create another data type in that someone combines the elements.
steady stratus
#

If I allocate a NativeArray like the one below, can I assume they are in the same order?
Yes. Though, I am assuming you aren't using a query with one of those components as optional, I don't know what that would do.

Furthermore, how do I get access to a LinearVelocity component? It is part of the RigidBodyAspect but I cannot use it to fill a NativeArray.
When you inspect the code, you will find the rigidbody aspect uses the PhysicsVelocity component for that, so you could just get that structure instead

#

to compute the directions of my units based on the Position and LinearVelocity of all other units

if you're tackling this in a brute-force manner, even with parallelization, you will quickly find it becomes very expensive; usually there's some kind of spatial subdivision to make this work efficiently

#

of course it depends on the number of entities whether this is a real problem or not

#

usually though, instead of allocating arrays like that, you would create a job to iterate over the data for you; this would save you the cost of creating the array

but if you are just starting out, it's not too complicated to change that later

sonic matrix