godotengine / godot

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

Colliders will drag each other #83182

Closed Az-qianchen closed 1 year ago

Az-qianchen commented 1 year ago

Godot version

godot-4.2-dev6

System information

WIn 11

Issue description

When the player is below and close to the enemy, enemy movement speed will become the player movement speed. print safe_velocity to get the correct result This situation will not occur when the player is above the enemy

tmp6A80

https://github.com/godotengine/godot/assets/52212525/e059a796-4179-48e4-bb6e-12ccbe7bbe7f

Steps to reproduce

Use the left mouse button or WASD to move the character An error occurred after moving under the enemy character and getting close to it.

Minimal reproduction project

NavigationRegion2D_bug.zip

Az-qianchen commented 1 year ago

My guess is that move_and_slide() brings wrong collision sliding?

smix8 commented 1 year ago

NavigationRegion can not cause this.

Please try first with the agent script templates from the documentation and confirm that they are working before you try customization. Neither do they convert stuff to local or multiply by physics engine tick. https://docs.godotengine.org/en/latest/tutorials/navigation/navigation_using_navigationagents.html#navigationagent-script-templates

You are updating the navigation path very, very frequently with your timer which can cause all kinds of issues, see https://docs.godotengine.org/en/latest/tutorials/navigation/navigation_using_navigationagents.html#pathfollowing-common-problems

Az-qianchen commented 1 year ago

Neither do they convert stuff to local or multiply by physics engine tick.

Is there any other problem possibility, I changed the code from the documentation to increase the refresh time to 1 second but the problem still exists tmp5CE4

https://github.com/godotengine/godot/assets/52212525/b5ea0d0b-c9b9-4f11-aecc-f0882b1c4f42

smix8 commented 1 year ago

In your project set the NavigationAgent2D max_speed to the same max speed value that you use in your script. Also reduce the NavigationAgent2D time_horizon_agents to 0.1-0.3 since your speed is slow.

I did not encounter anything other than physics collision shape sliding after fixing that. The capsule of your agent shapes get stuck on the obstacle collision regularly. Your navigation mesh was baked with 10 pixel but your agent has 16 pixel.

Az-qianchen commented 1 year ago

tmpD359

Sorry, but I made changes and it seems there is still a problem. I tried changing the values ​​to be more extreme, but the problem still exists. Can you reproduce this problem in the original reproduction project I provided? Is a system or Godot version problem ?

smix8 commented 1 year ago

As said the main problem left is the physics collision of the two CharacterBody2D. If you disable the physics collision masks of the agents you see that there is no pathfinding or pathfollowing problem, it is all physics bonkers.

The speed increase is because the two collision shapes are sliding and getting stuck against each other dragging each other. Capsule shapes that are transformed are in general not very stable for physics and it is better to use simpler shapes.

The capsule collision shape is also too large for the navigation mesh margin so your agent gets stuck on the obstacles as well.

Az-qianchen commented 1 year ago

Thanks, this can be solved,but even use the simplest RectangleShape or CircleShape as CollisionShape, Will still have such a problem

aboveyunhai commented 1 year ago

@smix8 I think you misunderstand his problem. A lot of people (including me) faced the same trouble due to the default (select all) of the moving platform.

When two objects collides each other and treat each other as moving platform, if one is on top of another one, the bottom one (as the moving platform) will drag the one on top, which causes the weird dragging issue as the video shows.

@Az-qianchen unselect the layer 1 for both your character and enemy, since they aren't moving platform. I just tried it out from your code, it seems like is what you want.

Credit to this reddit post: https://www.reddit.com/r/godot/comments/11c4gww/help_please_my_character_is_getting_stuck_and/

image

Az-qianchen commented 1 year ago

@aboveyunhai In my testing, unselect the floor layer seemed to solve the left and right dragging problem, but the downward dragging problem still existed.

https://github.com/godotengine/godot/assets/52212525/9e558d5c-bc34-400b-9d4f-1d7c6cab4453

Even if the "on leave" setting is set to "do nothing", the issue still persists.

aboveyunhai commented 1 year ago

@Az-qianchen
I'm new to godot, so don't take my word as truth, when a collider is on top of another collider, move_and_slide will apply snapping while attaching, which will take your velocity into account somehow. So when your player (as the floor for the enemy) moved away, the initial speed becomes so fast that it made you feel like two objects were stuck together. I feel like it could be a bug, whether from your code or the engine itself, or I didn't fully understand the method or the collision concept. I didn't have the bottom stuck issue when I remove the floor layer in another sample project. At the mean time, set snapping length to 0 can solve your issue since the speed will be 0 at the moment of detaching, but it might not be what you want, it can cause other issues, or you might need to write your own move_and_slide function.

image

Az-qianchen commented 1 year ago

@aboveyunhai Thanks, I think this will solve the problem, I will close with comment