# Math Question: Calculating force around Vector3 length inside a 3D Grid (compute shader)

I’m having a tough time phrasing this question, so I’ll do my best. I’d be happy to provide more details if necessary.

Currently I have a kernel that is calculating a force within a certain radius around a certain point in a 3D grid like so (“_GridSize” is (128, 128, 128)):

``````RWStructuredBuffer<float3> _VelocityWrite;
float3 _Velocity;
float4 _ForcePosition, _GridSize;

#pragma kernel VelocityImpulse

{
float3 cellDistanceFromForce = cellPosition / (_GridSize.xyz - 1.0f) - _ForcePosition.xyz;

float mag = cellDistanceFromForce.x*cellDistanceFromForce.x + cellDistanceFromForce.y*cellDistanceFromForce.y + cellDistanceFromForce.z*cellDistanceFromForce.z;

float3 forceAmount = exp(-mag / rad2) * _Velocity * _DeltaTime;

int idx = id.x + id.y*_Size.x + id.z*_Size.x*_Size.y;

}
``````

This is working well. It can be visualized like this:

What I’m looking to do is calculate the force around “_Radius” along the entire length of the “_Velocity” vector. In other words, I’m looking to get a non-zero “forceAmount” value for grid cell points that fall within the radius along the “_Velocity” vector.

What I’m looking for might be visualized as:

Honestly, I’m not even sure where to start.

Thanks for any help!