0 votes

We know all rigid bodies fall down in Godot w.r.t screen.

  1. What is the best way to change gravity direction such that the rigid
    bodies fall left, right or down? I know we can make it fall up by
    negating gravity scale but how to make it fall left or right. Is it
    possible or should I rotate all objects except main object(player) ?
  2. Can some one help me extend the default physics engine in Godot ? I
    am from Java background(full language knowledge but very low
    external library practical knowledge) but I know syntax of python
    well but don't know where to start. Also I need this kind of gravity
    control in both 3D and 2D either separately or combined. Any insight
    would be helpful.

I currently using the following code. I don't know if it is appropriate.

var velocity = Vector2()
const gravity=Vector2(0,100) #direction of gravity

func _fixed_process(delta):
    velocity += delta * gravity
    var motion = velocity * delta
    move( motion )

func _ready():
in Engine by (51 points)

2 Answers

+2 votes
Best answer

Look here for global settings:

Scene>Project settings>Physics 2D>Default gravity vector

Global gravity on runtime:

You can also use Area2D space override to create your own gravity zone and change it anytime.

And the custom integration as explained in the other answer.

by (7,888 points)
selected by
0 votes

Just to get you started, to do these things with a RigidBody it's possible to override the current integration, and create your own. For your example of reversing gravity, this is a simplified example of what you can do.

extends RigidBody

func _integrate_forces(state):
    var dt = state.get_step()
    var gravity = state.get_total_gravity() # The default gravity, you can use your own.
    var velocity = state.get_linear_velocity()

    state.set_linear_velocity((velocity + gravity * -1) * dt)

func _ready():

If you also interested in choosing the gravity's direction, you can use a unit vector as a direction and multiply it into the magnitude of gravity.

var direction = some_normal_vector * gravity.length()

state.set_linear_velocity(velocity + direction * dt)
by (5,234 points)
edited by

But set_linear_velocity() sets constant velocity right thus acceleration is zero. So how to make it accelerate(like under gravity). I thought of multiplying with delta but state is not available in _process(delta) and delta is not available in integrate_forces(state).
Please help.

state.get_step() returns the delta value. I abbreviated it there as dt for delta time.

If you want to include other forces, you would add them as well.

var velocity = (state.get_linear_velocity() + gravity + extra_force) * state.get_step()

Sorry for my mistake. I forgot basic physics. I forgot that change in velocity is acceleration. I saw change in arithmetic sequence (which is correct) but taught it should change in geometric sequence(which is wrong). Thanks anyways and sorry for wasting your time.

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 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.