RigidBody2D and StaticBody2D unexpected collision behaviour

Godot Version

I’m using godot 4.4.1

Question

I’m using a RigidBody2D and applying a force to it using a slingshot mechanic. However, when the rigidbody2d lands on a staticbody, the force does not work. I’m not sure why this is happening since I am not shooting the RigidBody2D into the staticbody.

this is my rigidbody2d script:

extends RigidBody2D
var direction := Vector2.ZERO
var initial_impulse = Vector2(500, -200)
var launched = false
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.

# RigidBody2D

func _integrate_forces(state: PhysicsDirectBodyState2D) -> void:
	if launched:
		apply_central_impulse(direction)
		launched = false

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass

and my slingshot script:

extends Line2D
@onready var player = get_parent().get_node("player")
var vector_start := Vector2.ZERO
var vector_final := Vector2.ZERO
var mouse_in_player := false
# Called when the node enters the scene tree for the first time.
func _input(event: InputEvent) -> void:
		if Input.is_action_just_pressed("mouse_click"):
			vector_start = get_global_mouse_position()
			vector_final = vector_start
			
		if Input.is_action_pressed("mouse_click"):
			vector_final = get_global_mouse_position()
			points[0] = vector_start
			points[1] = vector_final
		
		if Input.is_action_just_released("mouse_click"):
			player.direction = (vector_start - vector_final) * 2
			player.launched = true
			reset_line()
			

	
func reset_line():
	points[0] = Vector2.ZERO
	points[1] = Vector2.ZERO


func _on_character_body_2d_mouse_entered() -> void:
	mouse_in_player = true

I think _integrate_forces() is not getting called when the RigidBody is sleeping. So you would either have to relocate the launching implementation (there is no need for it to be in _integrate_forces() anyway) or set the RigidBody’s can_sleep to false.

1 Like

that works, thanks