We know all rigid bodies fall down in godot w.r.t screen. 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) ?Can I do it programmatically or using the editor.
How to change Gravity Direction of Physics Object
Well, if you think about it gravity is just a source of acceleration toward a terminal velocity along a vector/towards a center of mass. If I understand you right you might want to just write your own physics engine or a subset of one at least and use that over the default one far as gravity is concerned. I believe some here have done so before, so might be plenty here able to give some advice towards that.
- Edited
Can some one help me extend the default physics engine in Godot or develop a new one for this purpose? I am from Java and C++ 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. Any help would be nice. Also I need this kind of gravity control in both 3D and 2D either separately or combined. Any insight would be helpful.
Well, you say you have a java and c++ background so why not start by dissecting the godot source code, particularly the physics engine. While at it also visit the irc channels, there's 2: one for users the other for developers. as long as you don't just post spam there and have the patience to wait for replies you find some very good help there I think.
- Edited
This should work:
extends RigidBody2D
func _ready():
set_fixed_process(true)
func _fixed_process(delta):
var velocity = get_linear_velocity() #get velocity
velocity.x += 9 #apply gravity (change this line)
set_linear_velocity(velocity) #set velocity
- Edited
@cdqwertz said: This should work:
extends RigidBody2D func _ready(): set_fixed_process(true) func _fixed_process(delta): var velocity = get_linear_velocity() #get velocity velocity.x += 9 #apply gravity (change this line) set_linear_velocity(velocity) #set velocity
Thanks Megalomaniak and cdqwertz . This may work but still the code is not perfectly self explanatory without comments(I guess) and I have to set it for each node. "I" guess better solution is this or this.
I'd say there is no need to replace the whole physics engine only because the direction of gravity doesn't suit your needs. :)
If the gravity vector stays the same throughout the game then you can change the default gravity vector in the projects settings. (Should be normalized imho)
Or, you can calculate the gravity (alone) indidually for each RigidBody: a) Set the objects "Gravity Scale" to 0 b) Put some code in the RigidBodies "_fixed_process" method. Roughly like this:
func _fixed_process(delta):
#change this vector according to your needs. "* delta" scales it to the time-interval of fixed_process
var gravity=Vector3(0,get_mass() * -9.8,0)*delta
#
#local coordinates of objects center of gravity
var local_cog = Vector3(0,0,0)
#
#apply one gravity "impulse" per process interval
apply_impulse(local_cog, gravity)
If nothing happens, then the Body might be "sleeping" and be woken up with set_sleeping(false) Code should work. I just tested it.