0 votes


I am currently developing space "racing" game where the players will use local gravity fields (planets, stars, black holes) to slingshot their ships around.

I would like to draw predicted trajectory based on current position and velocity. So far I was able to implement this feature using scripts by managing the gravity by myself. The map is static and I can calculate the force pulling the player based on position.
My algorithm is:

  • get current position and velocity
  • calculate the gravity force for the position
  • calculate velocity change and get new position
  • repeat multiple times ([number of physics steps per second] * [prediciton length in seconds]), store points in array
  • draw polyline

This system works great, but I would like to use Area2D (with point gravity and gravity falloff) to do this for me since I it is already implemented and potentially much faster than scripted solution. I will still run the prediction loop from script but I would like to get the gravity vector from physics server based on global_position.

Is there a function to get gravity vector based on the global_position from the physics server?
So far I was unable to locate the method where is the gravity force is actually calculated for area2D. Can someone point me to the right direction?

Thank you

in Engine by (12 points)

1 Answer

0 votes

The applied gravity is calculated by the server using the global gravity plus all the modifications added by the areas.

Per body, you can get the total gravity applied to them via the body state https://docs.godotengine.org/en/3.0/classes/class_physicsdirectbodystate.html

by (7,898 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.