Get opposite direction of a RigidBody2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Pelli

Hi, I am trying to make an AI to play against the pc. The pc is a cannon and has to catch a ball. So I am moving the cannon to the actual ball position, but I need to rotate the cannon to the opposite direction that is facing the ball, like this:

or like this, when ball hits a wall:

enter image description here

I have created an Area2D that detect when the ball enters the area, so I have the ball saved, and I can get the get_linear_velocity() every frame, but is not working, honestly
I don´t know how to do it and I tried a lot of things and I am always getting something that is not the actual direction of the ball

Cannon.rotation = (ball.get_linear_velocity().angle() + deg2rad(180))

that line of code above works for the first image, but not when the ball is hitting something and changing completly the direction like in the second image.

:bust_in_silhouette: Reply From: magicalogic

Maybe you should just use look_at method.
Refer to the docs here.

Yes I tried it too and not worked, the only vector that I tought it was cannon.look_at(ball.get_linear_velocity()), any other vector that I should try?

Pelli | 2021-05-28 14:03

You should try cannon.look_at(ball.global_position)

magicalogic | 2021-05-28 14:13

Thanks!!, I don´t know why global.position didn´t come to my mind, I feel a little silly, Also I don´t know why but I have to add 90 degrees to the cannon after look_at() to get the cannon working, instead it looked to the left and not to the direction of the ball

Pelli | 2021-05-28 14:44

The sprite has to face +x direction which is 0°.

magicalogic | 2021-05-29 04:51