godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
89.56k stars 20.58k forks source link

PhysicsDirectSpaceState intersect_shape and intersect_ray stop reporting collisions of moved KinematicBody #63964

Closed phryk closed 2 years ago

phryk commented 2 years ago

Godot version

3.4.2

System information

FreeBSD 13.0 amd64

Issue description

When using PhysicsDirectSpaceState's intersect_shape and intersect_ray functions, things work fine at first, but KinematicBodys that move more than ~1 unit from their spawn position stop being reported. Actually this isn't entirely accurate, but I'm not quite sure how to describe this, so let me share some observations:

I've stumbled onto this when trying to implement boids while being unwilling to iterate over all boids to get the closest ones. I used intersect_shape with a sphere around each boid, but had to notice fairly early, that boids can't see each other anymore if they move more than ~1 unit from where they spawned.

If you save the scene (I ran my boids as a tool script), close it and open it again, the moved boids can see each other again – until they move ~1 unit from their new spawn position.

I believe this might be connected to #40952, but am not entirely certain. The problem is definitely specific to KinematicBody, but I don't have any objects scaled to 0,0,0.

I'm at least pretty sure it's not #49055 because I get the same behavior, no matter whether I use Bullet or Godot's own physics engine.

This could also just be me fundamentally misunderstanding something about [ray|shape]casts and writing my code wrong, but I'm not finding anything that could explain this in the docs and I've already tried numerous times to get help with this through both IRC and Discord, but no cigar.

I would be grateful for any help in triaging this, first of all by reproducing this problem on a different platform than FreeBSD so I can be sure it's not an OS or upstream problem.

Steps to reproduce

I've written a ShapeIntersector Node type that should condense the minimum functionality for reproduction. It comes with a debug sphere the same size as the sphere used for the shapecast, this sphere turns greener the more of it's kind it sees within the shapecast. It also draws lines to the neighbors it sensed.

Script at third-party paste because GarbageHub already refused to handle this file as plain upload, form input and as zipped file: https://paste.xinu.at/8bJIg/gdscript >:(

intersect_shape_troubles_a These were all spawned at 0,0,0 and moved afterwards. As you can see shapecasting detects fine near the origin, but stops working for objects that were translated further away than 1u.

Extra bamboozle:

intersect_shape_troubles_c The ShapeIntersectors near the origin keep being able to detect the one outside of the "safe zone".

intersect_shape_troubles_d But once it's translated in there, it senses everybody around it.

And for proof that this is specific to KinematicBody you can just change the extends… in ShapeIntersector to RigidBody et voila, the problem disappears.

phryk commented 2 years ago

Oh, probably also noteworthy to say, that I did actually test all this with raycasting too. I rewrote my entire boid sensory system with raycasts after failing with shapecast, only to see the same problem reproduced: boidraysense_c

bitsawer commented 2 years ago

At some point in the past I noticed that physics shapes didn't always update when moving them (or their parents, can't quite remember) in the editor manually, especially with ray-casting using PhysicsDirectSpaceState. I was using a "tool" script to raycast things in the editor scene. Reloading the scene updated the physich shapes. During runtime everything worked well. I haven't checked if this behaviour is still there, I assumed it was some bug or issue that would be fixed soon, not sure if I'm even talking about the same issue.

Also, probably duplicate of https://github.com/godotengine/godot/issues/43744 ?

Zireael07 commented 2 years ago

Yep, likely a duplicate. I was going to comment that I already saw this reported.

TLDR: Tool script physics don't work properly, but it works at runtime

phryk commented 2 years ago

Oh, thanks. That really seems to describe my issue. Also very nice to hear it's fixed in 4.0 and should work in runtime. I'll do some further debugging to make sure it actually works during runtime.

phryk commented 2 years ago

Yup, seems to work in runtime. At least most of the time – but I'm pretty sure that's because I went from shape- to raycast, leaving some literal blind spots in my boids perception. :)

Guess I'm gonna roll this back to the previous, more elegant way with shapecasting.

phryk commented 2 years ago

Okay, for the record, I just rolled my boid code back to use shapecasting and it does indeed work during runtime and apparently without blind spots. Thanks for the help! :)