Gravity with VehicleBody on a spherical terrain

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DaddyMonster

I’ve got a spherical 3d world and I want to add a VehicleBody. Up to now I’ve implemented a newtonian gravity system using move_and_slide on KBs.

Obviously I can’t use move_and_slide with my VB and neither can I use the physics gravity settings as they’re on the global y. I’d preferably like to use my existing methods which do the gravity, buoyancy and friction physics and spit out a Vector3 velocity variable.

I was surprised add_central_force seemed to have no effect at all:

if obj_1 is KinematicBody:
	obj_1.move_and_slide(obj_1.velocity)
if obj_1 is VehicleBody:
	obj_1.add_central_force(obj_1.velocity)

Any suggestions?

Ugh, this is not going well. First I jumped for joy when I saw RB/VB had a mode for KB but it doesn’t inherit with move_and_slide so I’m not sure what the point of it is. Then I saw they had this and gave a 2nd premature cheer:

func _integrate_forces(state):
	state.add_central_force(velocity)

But it has no effect at all. Yet the docs give the following example for a customer look_at function with a RB:

func look_follow(state, current_transform, target_position):
    var up_dir = Vector3(0, 1, 0)
    var cur_dir = current_transform.basis.xform(Vector3(0, 0, 1))
    var target_dir = (target_position - current_transform.origin).normalized()
    var rotation_angle = acos(cur_dir.x) - acos(target_dir.x)

    state.set_angular_velocity(up_dir * (rotation_angle / state.get_step()))

func _integrate_forces(state):
    var target_position = $my_target_spatial_node.get_global_transform().origin
    look_follow(state, get_global_transform(), target_position)

So why does set_angular_velocitywork and not add_central_force? Worse, s_a_v is listed as a method for RigidBody2d and isn’t in the docs RigidBody… I guess it’s an omission, I’m taking it as read the tutorial code works.

Of course, I might just be kidding myself and this will actually need a custom physics module for the wheels to work but I can’t see anything in the docs about how to do that…

DaddyMonster | 2021-01-29 16:04

How did you move forward with this?

I have finally started using Godot myself. I am trying to figure out something similar, and so far briefly tried out some solutions that don’t really work. In the process, I have discovered some weird behaviour (no knowledge about how “gravity physics” are working):

  1. Searching the docs for “gravity” I found Area3D. So I added a volume around by terrain, adjusting the gravity as needed. My idea was that the vehicle passes into the volume where it will be affected by another gravity force.

  2. This works fine. It’s not very user friendly, but the car stuck to the ground.

  3. When I drove to the edge of my test track, the car would exit the Area3D and seemingly revert the gravitational pull back to default.

  4. However, if I moved the vehicle by assigning a new transform (on the other side of the sphere, outside the Area3D volume), the gravity adjustment caused by entering the Area3D volume would remain. The vehicle would fall upwards.

This was unexpected. As that makes me think that the vehicle node somehow stores its own gravity settings… despite - after my very brief search - VehicleBody3D does not inherit from any entity that involves gravity. Clearly I’m wrong.

But could be that all we have to do is write a new vehicle class that gets its gravitational pull from a Node3D? This is at least something I’ll explore.

Gatada | 2022-09-12 22:20