godotengine / godotengine/godot

Issue with slepping property when setting linear_velocity to itself with _integrate_forces in RigidBody3D

Open
#108,656 1 comment 0 reactions 0 assignees View on GitHub
bug confirmed topic:3d topic:physics
Dominant language
C++
Stars
117k
Forks
26.8k
PR merge metrics
PR metrics pending

Description

### Tested versions

Godot version: 4.4.1 stable

This bug probably didn't exist earlier because I was following this tutorial https://youtu.be/k0vZbIclXjE?t=20408 which used Godot 4.1 and in their code it worked great.

### System information

Godot v4.4.1.stable - Windows 11 (build 26100) - Multi-window, 2 monitors - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 4060 Laptop GPU (NVIDIA; 32.0.15.7602) - AMD Ryzen 7 7435HS (16 threads)

### Issue description

So, this code was present in this tutorial (https://youtu.be/k0vZbIclXjE?t=20408) and it worked great in Godot 4.1, it was meant to make a pool ball not bounce too much hard, so it checked to see if its linear velocity in the y axis was higher than 0.2, and if so, it was limited to that, otherwise it would remain the same:

func _integrate_forces(state):
linear_velocity.y = min (0.2, linear_velocity.y)

It worked great in the tutorial, but when trying to replicate this code on Godot 4.4.1, it didn't work for me because my sleeping state was not working properly. It started switching from true to false every frame, even without any forces or movement being applied to my pool balls. So I did some more testing.

Even with only writing this below the sleeping state also wasn't working properly, which doesn't make any sense because I was just setting the linear_velocity to itself in the y axis (if I just comment out this function then the sleeping state would work properly):

func _integrate_forces(state: PhysicsDirectBodyState3D) -> void:
self.linear_velocity.y = self.linear_velocity.y
print(linear_velocity)

### Steps to reproduce

So, I included a MRP below. It is pretty straight forward, just extract the zip project, run it, and you will see the output printing sleeping true and false every frame. Then, comment out the _integrate_forces function and it will stop.

To reproduce:
1. Create an empty project, add a 3D node as root
2. Add 2 childs: StaticBody3D with CollisionShape3D (Box shape), and a RigidBody3D with CollisionShape3D (Sphere or any other shape).
3. Place the Rigidbody on top of the box so the box could be supporting it entirely, make the box bigger if needed.
4. Create a script for the RigidyBody3D with the code bellow, and run it to see the output:

extends RigidBody3D

## When commenting this function, the sleeping state works as expected
func _integrate_forces(state: PhysicsDirectBodyState3D) -> void:
linear_velocity.y = linear_velocity.y

func _on_sleeping_state_changed() -> void:
print("sleeping?", sleeping)

### Minimal reproduction project (MRP)

[sleeping_property_issue.zip](https://github.com/user-attachments/files/21247555/sleeping_property_issue.zip)

Contributor guide

Open the contributing guide

Research direction

Start with the attached sleeping_property_issue.zip and reproduce the behavior using the RigidBody3D script's _integrate_forces callback. Compare the sleeping-state output with the velocity assignment enabled and commented out. Done means the supported rigid body can enter sleep without switching state every frame when the assignment does not change its velocity.

Written by the indexing model from the issue text.

Assessment

Domain
game-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.