Rigidbody2d falling very slowly
Asked Answered
J

4

0

Hoi1! I'm having this weird problem where my player character (consisting of a Rigidbody2d parenting a rectangular CollisionShape2d) is falling slower than they should on:

  1. Running the project
  2. Falling of the platform (StaticBody2d) by themselves
    The effect goes away when any of the keys that control the player (via code) is pressed. Speaking of, here's my entire code:
    extends RigidBody2D
    
    @export var target_speed = 250
    @export var minimum_speed = 10
    @export var acceleration_time = 1
    @export var decceleration_time = 0.2
    @export var turn_time = 0.5
    
    var target_acceleration = 0
    var move_direction = 0
    
    @export var gravity = 980
    
    var locomotive_forces = Vector2(0, 0)
    var external_forces = Vector2(0, 0)
    
    # Called when the node enters the scene tree for the first time.
    func _ready():
    	pass # Replace with function body.
    
    func _integrate_forces(state):
    	external_forces = Vector2(0, gravity)
    	
    	for i in (state.get_contact_count() - 1):
    		external_forces += state.get_contact_impulse(i)
    	
    	apply_central_force(locomotive_forces + external_forces)
    
    # Called every frame. 'delta' is the elapsed time since the previous frame.
    func _process(delta):
    	if Input.is_action_pressed("move_left"):
    			move_direction = -1
    	else:
    		if Input.is_action_pressed("move_right"):
    			move_direction = 1
    		else:
    			move_direction = 0
    			
    	if move_direction != 0:
    		if sign(linear_velocity.x) == move_direction:
    			if abs(linear_velocity.x) < target_speed:
    				target_acceleration = target_speed / acceleration_time * move_direction
    			else:
    				target_acceleration = 0
    		else:
    			target_acceleration = target_speed / turn_time * move_direction
    	else:
    		if abs(linear_velocity.x) < minimum_speed:
    			linear_velocity.x = 0
    			target_acceleration = 0
    		else:
    			target_acceleration = target_speed / decceleration_time * -linear_velocity.normalized().x ##get something better
    	
    	locomotive_forces.x = mass * target_acceleration - external_forces.x
    	
    	print_debug(locomotive_forces + external_forces)
    	print_debug(sign(linear_velocity.x) == move_direction)
    I've also seemed to stumble upon the fact that perhaps, this phenomenon is framerate dependent as it all but goes away when I try to record it. Still, I'll post one of my attempts, although the effect is barely noticable:
Jamesjamesian answered 13/3, 2024 at 18:50 Comment(0)
E
0

Try moving the whole thing into _physics() instead of _process(). My guess is that that solves the issue.

Endorsement answered 14/3, 2024 at 7:8 Comment(0)
J
0

Here's what happening (finally v w v; )

Jamesjamesian answered 13/3, 2024 at 18:57 Comment(0)
J
0

Update:

Jamesjamesian linear_velocity.x = 0

^ This seems to be the culprit (wtf godot?). I have no idea why assigning the x component of velocity should affect its y component (the issue persists even while using the built-in gravity or constant force utilities), but it makes things really annoying to implement (ironic).

Jamesjamesian answered 14/3, 2024 at 6:19 Comment(0)
E
0

Try moving the whole thing into _physics() instead of _process(). My guess is that that solves the issue.

Endorsement answered 14/3, 2024 at 7:8 Comment(0)
J
0

Endorsement That is what I did, but the problem with that is it makes the 'jittering', when the body is at rest, way worse, so I have to use a much larger value of the minimum_speed variable to get it to stop. Tbh, for the nature of my game, I need the engine to handle the physics, so I'll have to find a better solution to this other problem, but yes, doing it under _physics_process solves the issue at hand.

Jamesjamesian answered 14/3, 2024 at 10:41 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.