With the following code, I either get a straight line or no line at all. I have tried almost the same thing in Unity and I get something that looks like orbiting, but here I get nothing.

How can I fix my code to draw a trajectory?

func _update_trajectory(delta):
    var line_points = []
    var pos = mousePosition
    #starting velocity 200 for now
    var xAxis=200
    var yAxis =200
    var vel = Vector2(0,100)
    var vecR = sunGravity.position-pos
    var gravityForce = (vecR).normalized()
    var angle= 0
    var lastPos = pos +velocity.normalized()
    for i in range(1,MAX_POINTS-1):
        pos += vel*delta

func Rigidbody_test(pos1:Vector2):
   var planetPos = sunGravity.position
   var dir=planetPos-pos1
   var dis = dir.length_squared()
   var forceMag=2*g_constant*((sunGravity.mass*currentMass)/dis)
   var force =dir.normalized()*forceMag
   return force
  • \$\begingroup\$ It looks like your last edit introduced an error - you start the _update_trajectory function, but the lines under it are not indented as expected (or you're missing the pass statement). Regarding the problem, how are you drawing line? Does the inspector show the contents/points of the line changing? \$\endgroup\$
    – Pikalek
    Commented Jan 4, 2023 at 19:25

1 Answer 1


I have fixed my issue by increasing gravity constant so instead of having 0.0006 or whatever I just increased it to something like 274 which actually showed the curvature.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .