# 2D Physics Strange Behaviour

4.2.1

### Question

I’m making a 2D space shooter. The spaceship is a rigidbody 2D controller by the following code:

``````extends RigidBody2D

const ROTATION_SPEED = 5.0
const THRUST_FORCE = 10
const TORQUE = 1
const MAX_VELOCITY = 5
const INTERPOLATION_SPEED: float = 0.5

@export var bullet : PackedScene

var camera: Camera2D
var collisionPolygon: CollisionPolygon2D

var interpolated_mass: float
var t: float
var thrust: float
var _min_v: Vector2
var _max_v: Vector2

camera = \$Camera2D
collisionPolygon = \$CollisionPolygon2D
interpolated_mass = mass
t = 1.0
thrust = 0.0
_min_v = Vector2(-MAX_VELOCITY, -MAX_VELOCITY)
_max_v = -_min_v

func _process(_delta):

#Thrust input
thrust = int(Input.is_action_pressed("thrust_forward")) * THRUST_FORCE

# Shoot input
if(Input.is_action_just_pressed("shoot")):
shoot()

# Mass interpolation
if t < 1:
t += _delta * INTERPOLATION_SPEED
else:
t = 1.0

interpolated_mass = lerp(interpolated_mass, mass, t)
collisionPolygon.scale = Vector2(interpolated_mass, interpolated_mass)
camera.zoom = (Vector2.ONE*6)/(interpolated_mass*1.5)

func _physics_process(_delta):
# Looking at mouse
look_at(get_global_mouse_position())
rotation_degrees += 90

#Limit velocity
linear_velocity = linear_velocity.clamp(_min_v, _max_v)
move_and_collide(linear_velocity)

func _integrate_forces(state):
state.apply_force(Vector2(0, -thrust).rotated(rotation))

func change_mass(value):
mass += value
t = 0

func shoot():
var b: Node2D = bullet.instantiate() as Node2D

b.position = self.global_position
b.rotation = self.global_rotation - PI/2
b.get_node("./Shape").scale = scale