Get floating point distance accuracy at position

When I was writing a physics system for bullets, represented as spheres, I encountered an issue where at a certain low speed or distance checks, the bullet would clip through parts, as the check would be basically Raycast(position, nextPosition clamped to position), which wouldn’t actually detect anything.

A solution I thought about was calculating the distance between the position and the next position followed by a floating point bit (if it’s even called that).

The problem is that I haven’t been able to understand how vectors or floating points are constructed, which ends up limiting my capability of doing this. I have researched about this but I have been left confused still.

Any help or useful documentation or tips are useful.

Are you sure they’re clipping through parts due to precision errors?
Raycasts clip through parts if their origin is inside of them.

The precision value I used was a number similar to 0.01, if the bullet was too far away, the 0.01 would round down to 0, which resulted in no raycast at all. Increasing this value would stop this from happening, but I want to keep this value as small as possible because this basically makes the hitbox bigger. This is why I want to find the smallest distance possible with floating point.

Why exactly are you raycasting that short? What do you mean by “makes the hitbox bigger”?

I should have clarified, I apologize.

I am taking into account the cases where the velocity is very close to zero, the bullet physics I have also have an option to make a bullet roll if its velocity goes through the ground. The main issue I would find when doing this was that whenever it would be rolling down a slope, the hits a flat surface, it would cause this issue where the bullet, due to floating point precision errors (which is what I want to prevent), be just right at the surface, and raycasting through there causes the bullet to ignore the part. Another example is the one I provided earlier.

The bullet physics are done using raycast, specifically, raycasting from position to targetposition + bulletRadius/2 + 0.01. If I increase this small value, it would mean that the distance of the raycast would increase and hit objects further away, which gives the illusion of a bigger hitbox.

I don’t think I quite understand, what do you mean by bullet roll?

Rolling physics, like when a sphere is rolling down the floor.

Do you need the raycast to get the surface normal, or are you only using it to detect when it hits the ground?

The same raycast is used for both. I want to get the floating point distance accuracy to see if I can also pull the part backwards by a small amount to prevent clipping issues.

Well have you tried starting the raycast further back (overlapping with the last raycast slightly)?

Also are you raycasting every frame? That seems expensive.

That is what I previously said, and that’s why I want to know how I could get the smallest possible distance from the position, because I don’t want to have it be a bigger number than it should.

Also are you raycasting every frame? That seems expensive.

Many guns raycast every frame, so this shouldn’t be a problem, it’s up to how the code is handled. Either way, I skip certain frames if time passed since raycast is too short.

Maybe look into this resource: Making a combat game with ranged weapons? FastCast may be the module for you!

I appreciate your answer, but I want to create my own custom bullet system.

  1. I already have written my custom code
  2. My code includes, as I said, rolling physics, for more realistic behavior, or to implement grenades and such. (The issue of this post is here)
  3. FastCast is great for newer developers, but I like understanding the whole code of something I am going to use, the actual scripts themselves are very unorganized (from what I am used to) and doesn’t allow me to do more behind the scenes.
  4. With FastCast, I am able to hit people behind walls without server security, with my code I want to implement my custom security measures.

Well I don’t think floats/doubles are really your issue, but you could try varying the length of the raycast based on bullet velocity.

And at scales of hundredths of studs, will it really make a significant difference if they were just a bit longer?

but you could try varying the length of the raycast based on bullet velocity.

I previously said, that this also happens at small velocities, if it’s small then the value offset will also be smaller.

And at scales of hundredths of studs, will it really make a significant difference if they were just a bit longer?

And as I also said, I tested making it longer, and it does work but I can’t have a constant for every distance, which goes back to the main problem I stated, once again.