Any Way to lock a RigidBody2D's movement in one axis?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tusharkant15
:warning: Old Version Published before Godot 3 was released.

I’m making a bubble matching game where once matched the bubbles fall down. I have chosen to implement this using RigidBody2D however I want to make sure that all the movement is restricted on the Y axis to ensure that all the bubbles are perfectly aligned.

Currently I have set the collision shape to a rectangle to ensure the bubbles don’t behave strangely, however every now and then the boxes get stuck half way through, presumably on the edges, and prevent the bubbles above them from falling down.

:bust_in_silhouette: Reply From: Scipi

One thing you could do is update the x position on each ball to one determined on loading. Something like:

onready var _x_pos = get_pos().x

func _ready():
    set_fixed_process(true)

func _fixed_process():
    var pos = get_pos()
    set_pos(Vector2(_x_pos, pos.y))

There also seems to be a way using custom integrators and ignoring linear velocity along the x axis, but testing shows the x axis does change a little. Still, it could lead to a more elegant solution than overwriting the position every frame:

func _ready():
	set_use_custom_integrator(true)

func _integrate_forces(state):
	var vel = state.get_linear_velocity()
	state.set_linear_velocity(Vector2(0, vel.y))

Sorry but neither worked.

tusharkant15 | 2017-02-20 15:02