+1 vote

I'm making a angrybirds styled game, where the player launches a rock from a slingshot that collides with other objects.
One of these objects is another slingshot, where the rock can be launched again.
It's composed by an Area2D with a pretty big collision radius, and when the rock hits it, it's supposed to teleport the rock to the center of the slingshot object and let the player launch it again.
The rock is a RigidBody2D, and at the moment this is the code I use to teleport it to the center of the slingshot:

func _integrate_forces(state):
    if current_slingshot != null and in_slingshot:
        self.position = current_slingshot.position
        self.sleeping = true

This does teleport the rock to the center of the slingshot, but when I launch it again with apply_impulse(), the rock is launched from the position where it first collided with the slingshot, and not from its center.
What am I doing wrong?

in Engine by (16 points)

1 Answer

+2 votes
Best answer

You cannot move a rigid body with position.

You are correct that you need to use _integrate_forces(), but any physics changes must be made to the Physics2DDirectBodyState - the state in the callback.

func _integrate_forces(state):
    if current_slingshot != null and in_slingshot:
        var new_transform = state.get_transform()
        new_transform.origin = current_slingshot.position
        self.sleeping = true

More information here: http://kidscancode.org/blog/2017/12/godot3_kyn_rigidbody1/

by (21,979 points)
selected by

Thanks. It's working now.

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.