How to change Gravity Direction of Physics Object?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Anutrix
:warning: Old Version Published before Godot 3 was released.

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():
	set_fixed_process(true)
:bust_in_silhouette: Reply From: avencherus

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():
	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)

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.

Anutrix | 2017-04-10 13:23

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()
set_linear_velocity(velocity)

avencherus | 2017-04-10 13:34

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.

Anutrix | 2017-04-10 14:11

:bust_in_silhouette: Reply From: eons

Look here for global settings:

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

Global gravity on runtime:
https://forum.godotengine.org/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.