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;
StructuredBuffer<float3> _VelocityRead;
float3 _Velocity;
float _Radius, _DeltaTime;
float4 _ForcePosition, _GridSize;

#pragma kernel VelocityImpulse

[numthreads(NUM_THREADS, NUM_THREADS, NUM_THREADS)]
void VelocityImpulse(uint3 cellPosition : SV_DispatchThreadID)
{
	float3 cellDistanceFromForce = cellPosition / (_GridSize.xyz - 1.0f) - _ForcePosition.xyz;

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

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

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

	_VelocityWrite[idx] = _VelocityRead[idx] + forceAmount;
}

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

Radius around a point

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:
Radius around a vector

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

Thanks for any help!

Hello, this thread seems to have some useful discussion about a similar problem, the answer has links to some point cylinder tests.

I was thinking you could also just start at one end of your capsule and do multiple sphere tests along the direction, wouldn’t be perfect but it you have a small enough step you could get decently close results. I am not sure if this would cause you to gather multiple samples from the same points though.

Lastly I noticed in your code that you use the ratio of square magnitude over square radius. Just in case you weren’t aware that is not the same ratio as the magnitude over the radius. In case you were doing this as an optimization step, but maybe it still works fine? Just a thought