Godot Version
4.3.stable
Question
Hello. I am new to Godot and am trying to create a pseudo-realistic 2D tank for practice. I have decided that I want the tank to move using physics so it can be forcefully shoved out of the way by larger, heavier tanks (or do the shoving itself). Thus, I created the chassis using a RigidBody2D. This tank object is intended to work for both the player and allied/enemy tanks. I thought this would be fairly simple, but I was evidently mistaken…
However, I came across an issue when trying to attach a turret onto the tank. The turret must satisfy these conditions:
- The turret must follow the mouse cursor.
- The turret must be able to collide with physics objects, both stopping and being stopped by things that strike the barrel. I would also prefer it if it was able to influence the tank (such as if the tank is driving towards a wall and the turret’s barrel impacts first, it should stop the tank)
- The turret must rotate with the tank chassis.
I cannot seem to come up with a solution that satisfies all three of these.
I tried using CollisionShape2Ds and a sprite - This created a turret that could rotate at a fixed speed and follow the mouse, and would rotate with the chassis, but would not stop when striking a physics object and instead possessed the power to rotate the entire tank. It also couldn’t respond properly to external forces such as an impact causing it to spin.
I tried using a RigidBody2D with a Pin joint. This created a turret that would rotate using torque (preferable) and stop when it struck physics objects, but would not rotate with the parent. I tried adding code to the pin joint to force it to rotate with the parent, but this resulted in the turret being unable to be moved with torque.
I tried using a RigidBody2D in freeze mode and an AnimatableBody2D. These also didn’t work as they prevented me from being able to rotate the turret using external forces.
I have concluded that I simply do not have the knowledge or tools required to solve this problem. Please help. How should I go about creating this tank? I can post additional information if you want.