The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Hi,

I've a layout-x-y-centered control with a RidigBody2D child with a linear_velocity.
This body pauses when the tree gets paused.
When this body enters an ***Area2D*** it should be repositioned to the x-y-center.

When the tree gets paused it works. The body is centered. But after unpausing, it looks like the body is back at the old position. Like warping.

func _integrate_forces(state):
    if restart:
        restart = false
        stopMoving(state)
        startup()
        get_tree().paused = false # switch to check for paused position

func startup():
    var rnd = RandomNumberGenerator.new()
    rnd.randomize()

    var force_x = (rnd.randi_range(1, 2) * 2 - 3) * difficulty
    var force_y = (rnd.randi_range(1, 2) * 2 - 3) * 2 * difficulty
    linear_velocity.x = force_x
    linear_velocity.y = force_y

func stopMoving(state: Physics2DDirectBodyState):
    get_tree().paused = true
    state.linear_velocity = Vector2(0, 0)
    position = Vector2(0, 0)

func _on_RightWall_body_entered(body):
    restart = true

func _on_LeftWall_body_entered(body):
    restart = true

Why this doesn't work as expected?

in Engine by (21 points)

1 Answer

0 votes
Best answer

You're correct to be using _integrate_forces() to affect the RigidBody2D without breaking physics, however, just as you use the state to change linear_velocity, you must also use it to set the position. This is done via the state.transform:

state.transform.origin = Vector2()
by (22,191 points)
selected by

Ok. Thx. I've breen pretty sure, that I've already tried that call before without success. But maybe somehow different. You're right, now this works.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.