Should I use NativeScript for my physics?

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

Hi guys, I’ve been making a game for Android that consists in beating a rotating maze, as a free falling ball. The player would rotate the maze to drive the ball to the end.

My first idea for the ball, was to use a RigidBody2D, and change the direction of the default gravity vector at runtime with this code I found online:

Physics2DServer.areasetparam(getworld2d().getspace(), Physics2DServer.AREAPARAMGRAVITYVECTOR, Vector2(insert_coordinates_here))

I tryed it, but it didn’t work too well. I wanted to rotate it 90º/15 per physicsprocess, and effectively turn it 90º at a time in 1/4 seconds. When it stopped rotating (as I said before, after 90º, a.k.a. at the axi), the ball would behave well, but while the gravity was rotating the ball would move really erratically.

So I decided to code the physics myself. My question is: is it too complex for GDScript? Would I benefit from using NativeScript?
It runs fine on my phone but it doesn’t run well on friend of mine’s phone. 30fps at best by looking at it. (it ain’t no dinassaur, his phone. and mine isn’t a flagship either)
Would using NativeScript potentially improve/solve the situation?
If not, do you guys have any tips to make the code better/more efficient ;)? Or to export “better”? (idk)
Can/Should I use multi-threading?
I’m in the second year of my Computer Science degree and I am not very keen on/have no idea about multi-threading, but I’m willing to dive into it.

Here’s the code if you’re willing to take a look (Ball.gd):

#res://Scenes/Ball/Scripts/Ball.gd
extends KinematicBody2D

#constant by which the ball is accelerated down
const gravity = 10

#constant by which the balls velocity (motion.length()) is multiplied if it were to bounce straight up
const bounce_force = 0.225

#contants to make the ball rotation by friction look smooth
#---make-require-tweaks---
const friction = 0.047
const friction_ratio = 75

# do not change ----------------
var sprite_rotation_speed = friction/friction_ratio
#               ----------------

var motion = Vector2(0,0)

var normal = Vector2(0,-1)

var gravity_multiplier = 1

var rotation_force = 0

#processes bounces, sliding and free falling
func _physics_process(delta):
	_update_gravity()
	if test_move(global_transform, delta*motion) and motion.length() > 10:
		var col = move_and_collide(motion*delta)
		var aux_motion = motion.bounce(col.normal)
		motion = aux_motion*lerp(1, bounce_force, abs(motion.angle_to(aux_motion)/PI))
		_update_sprite_rotation()
	else:
		motion = move_and_slide(motion, normal)
		if is_on_wall() or is_on_floor() or motion.length() <= 10:
			motion -= motion*friction;
			_update_sprite_rotation()
	_rotate_sprite()

#rotates the ball and all its physics atributes. 
#Desired effect for the end user: map is rotated, the ball keeps being affected by the gravity (falling down), as per usual
func _rotate(rotation_factor):
	normal = normal.rotated(rotation_factor)
	motion = motion.rotated(rotation_factor)
	rotate(rotation_factor)
	
#welp, updates the gravity c:
func _update_gravity():
	motion -= normal*gravity*gravity_multiplier

#rotates the sprite based on friction
func _rotate_sprite():
	$Sprite.rotate(rotation_force)

#updates the values by which the sprite will be rotated by (friction)
func _update_sprite_rotation():
	if abs(normal.angle_to(motion)) != 0:
		rotation_force = (normal.angle_to(motion)/abs(normal.angle_to(motion)))*(normal.rotated(PI/2).abs()*motion).length()*sprite_rotation_speed		

Thanks in advance!