0
votes

I'm trying to make a small circle move in another bigger circle as a ball moving in circle relative to Android phone tilting. I'm doing this in Godot but I think the principle is the same in all game engines probably. I make a scene in Godot and add two sprites as the two circles as the following picture. I get the accelerometer 3D vector, use x and y values and calculate the angle in x and y plate (relative to y axis i.e Vector2(0, 1) ) and rotate the mentioned scene to that degree using an animation, using this code

func _process(delta: float) -> void:
    var vec3 = Input.get_accelerometer()
    accelVec = Vector2(-stepify(vec3.x, 0.1), -stepify(vec3.y, 0.1))

    var angle = accelVec.angle_to(Vector2(0, 1))
    rotateTween.interpolate_property(self, "rotation", rotation, angle, 0.2, 
                                    Tween.TRANS_LINEAR)
    rotateTween.start()

    return

But the problem lies in here that when the x value of accelerometer 3D vector changes from a positive to negative value i.e when the ball is at top of the circle and is going to go to the other half of the circle, it actually moves from bottom of the circle to the desired point rather than from top of the circle. I hope I explained the problem well, though I doubt it. I added the Gif that shows actual test on an android phone here Testing in Android. Any idea how to solve this problem? Thanks in advance.

1

1 Answers

0
votes

This is because Tween is interpolating linear values. It doesn't know it's working with angles, or that they should wrap around.

So when you're at -179 degrees and you tween to 179--a difference of 2 degrees--Tween just sees -179 -> 179 and goes through the whole circle.

You probably don't need a Tween here at all because _process() happens every frame.