0 votes

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?

asked Apr 30, 2018 in Engine by Torgo (15 points)

1 Answer

+1 vote
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
        state.set_transform(new_transform)
        self.sleeping = true

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

answered Apr 30, 2018 by kidscancode (17,164 points)
selected Apr 30, 2018 by Torgo

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 How to use this Q&A? before posting your first questions.