RayCast .LengthSquared() > 0.0f' failed (DEF-1286) (SOLVED)

I keep, seemingly randomly, having my game crash out with an error in the console:

dmengine: ../src/box2d/Box2D/Collision/b2DynamicTree.h:232: void b2DynamicTree::RayCast(T*, const b2RayCastInput&) const [with T = b2WorldRayCastWrapper]: Assertion r.LengthSquared() > 0.0f' failed.

Other than being something to do with raycasts I have no idea what’s causing it! I’m using editor 2 and seeing this issue on both my linux machine and someone else’s windows machine.

If it’s of any help then the project is at https://github.com/dirty-casuals/V.Ampere/ the code is a bit of a mess but the raycasts are done in simple_ai/ai.script and player/player_power.script.

2 Likes

I believe that’s the error you get if you try a ray cast with zero length.

Interesting, thanks. I just tried a quick and dirty test of changing my raycasts to be of length 0 and the engine keeps the game running with WARNING:PHYSICS: Ray had 0 length when ray casting, ignoring request..

Ah, you’re right, sorry. I copy-pasted your code and didn’t have any errors with it, but I wasn’t giving it the same inputs as you are. I think it may have something to do with your angles? If you divide by zero somewhere you’ll get infinity, then if you take the math.cos of that you get NAN, and giving your ray_cast a vector with NAN in it produces the error. It might not be the angles, but I would try printing the position vectors that you’re giving to the ray cast to see if any of them are bogus, and trace it back from there.

2 Likes

Thanks, I’ll have a play and try boost the number of raycasts a bit to try cause it to happen a bit more frequently whilst I track down the issue. Something like you suggest makes sense to me.

I’ve just worked this out, took me a while - was a pain to trigger. Thanks for your help @ross.grams it was kind of what you expected.

local rot = 2 * math.asin(go.get_rotation().z)

was the offending line. The issue occurs when rotation is something like (0, 0, 1.0000001192093, -0.00037728901952505).

vmath.quat(0, 0, 1.0000001192093, -0.00037728901952505).z = 1.0000001192093

math.asin of this value is nan due to it being outside of -1 to 1. I can easily fix this in my code :slight_smile: . Whether or not it’s a bigger issue or expected that .z is greater than 1 I don’t know as I don’t fully understand quaternions and this could be down to lua rounding errors?

3 Likes

Quaternions can represent a rotation in 3D space if the length of the quaternion is 1. (x*x+y*y+z*z+w*w == 1)
I believe that it might be a rounding error in this case, but since you are using the “.z” component as a rotation value (a bit unusual imho), I wonder how you created the rotation to begin with?

For understanding quaternions better, I encourage you to watch presentations on the subject, for instance Understanding Quaternions by Jim Van Verth.

2 Likes

The rotation is just obtained from go.get_rotation(). I shall further my understanding of quaternions as they seem to be very useful to have a good grasp of.

1 Like

Correct. When ray casting in 2D with the same x and y value but with different z-values the engine check for 0-length failed and the crash occurred. Fixed in Defold 1.2.116.

2 Likes