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

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 )

set_fixed_process(true)
``````
in Engine

Look here for global settings:

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

Global gravity on runtime:
https://godotengine.org/qa/1290/how-to-change-global-gravity-properties-at-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

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)

set_use_custom_integrator(true)
``````

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

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)`.
`state.get_step()` returns the delta value. I abbreviated it there as dt for delta time.
``````var velocity = (state.get_linear_velocity() + gravity + extra_force) * state.get_step()