#Disable gravity and drag on a rigid body

44 messages · Page 1 of 1 (latest)

sterile tusk
#

Hey, so I have a spaceship which moves using force applied to it through a script. I have gravity set to 0, but the ship still loses speed exponentially depending on how fast it is moving. This only happens while the player is inside the spaceship, when I remove the player from the ship it moves just fine without losing speed. Is is possible I make the player/ship ignore drag while inside the ship? Sry if this question is written poorly I'm tired af and going to bed, I'll check replies when I wake up, thanks.

acoustic juniper
#

you could measure speed and set it back when it slows down?

sterile tusk
#

I'll give that a go, but I'm worried about jittering. Thanks.

#

Thanks that actually worked really well.

acoustic juniper
#

you just need to set it back before it's displayed

sterile tusk
#
func _physics_process(delta):
    var direction = Input.get_axis("move_ship_left", "move_ship_right")
    if direction:
        self.apply_force(Vector2(FORCE,0) * direction)
    else:
        if self.linear_velocity != prev_velocity:
            self.linear_velocity = prev_velocity
    prev_velocity = self.linear_velocity```
#

this was my code

wide onyx
sterile tusk
#

Why not? Also how else should it be done?

abstract wolf
#

Hello, would you mind sharing more information about the project?

#

For example what type of body is the player? Can we see the player's script?

sterile tusk
#

sure, i've been working on it for like a day so its nothing much but i'll grab what have so far

#

the player is a CharacterBody2D while the ship is a RigidBody2D using 4 collision shapes so that the player can move around inside

#
extends CharacterBody2D


const SPEED = 50.0
const JUMP_VELOCITY = -250.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")


func _physics_process(delta):
    # Add the gravity.
    if not is_on_floor():
        velocity.y += gravity * delta

    # Handle Jump.
    if Input.is_action_just_pressed("ui_accept") and is_on_floor():
        velocity.y = JUMP_VELOCITY

    # Get the input direction and handle the movement/deceleration.
    # As good practice, you should replace UI actions with custom gameplay actions.
    var direction = Input.get_axis("ui_left", "ui_right")
    if direction:
        velocity.x = direction * SPEED
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)
    print(velocity)
    move_and_slide()

Player script ^

#
extends RigidBody2D

const FORCE = 150.0
var prev_velocity = Vector2.ZERO
# Called when the node enters the scene tree for the first time.
func _ready():
    pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
    var direction = Input.get_axis("move_ship_left", "move_ship_right")
    if direction:
        self.apply_force(Vector2(FORCE,0) * direction)
    else:
        if self.linear_velocity != prev_velocity:
            self.linear_velocity = prev_velocity
    prev_velocity = self.linear_velocity

ship script ^

#

oops, one sec

#

fixed

abstract wolf
#

Gotcha, thank you. And the problem is that once the player is inside the ship, the ship is dragged down by gravity / potentially collides with the player and is slowed down?

sterile tusk
#

yeah thats what I thought...

#

one second

#

I have some collision settings I thought might fix it. Layer 1 being the player and Layer 2 being the Ship
This is the player collision layers

#

and this being the ship's

#

currently the ship actually doesn't fall down but is slowed by the player being inside

abstract wolf
#

Doing the collision layers/masks like that fixes it for me - I don't see the ship being slowed down by the player.

#

(Having player and ship on two different layers, and only the player masks the ship)

wide onyx
# sterile tusk Why not? Also how else should it be done?

The position, rotation or even velocity of Rigidbodies shouldn't be maninpulated directly because it interferes with the internal physics simulation. This can lead to unpredictable behavior like missed collisions or rubberbanding.

Using forces, impulses or torque on Rigidbodies is the preferred option. if you have to manipulate one of the aforementioned properties directly you have to do so in _integrate_forces(), where you have direct access to the PhysicsDirectBodyState.

sterile tusk
#

Thanks, i did notice the player occasionally clipping through a wall. That may be what caused it

abstract wolf
#

Right, I think in this case, the main issue is that if you override the linear_velocity instead of using the built-in RigidBody2D functions like apply_central_force() or add_constant_force(), then the physics engine will behave unpredictably when it attempts to correct the linear_velocity itself.

And there are many times it attempts to do that, like when one object is pushed out of another that is colliding with it/penetrating it too much, or when it's simulating the weights of objects that are pushing each other... the bounciness of certain physics materials... etc.

sterile tusk
#

So to avoid adjusting the linear_velocity i just need to fix the issue of the player slowing down the ship while inside...?

#

otherwise what method should i use to apply a correctional force?

abstract wolf
#

I'm unsure exactly what's causing this particular problem. Codimon's advice is good though. It's possible this problem may be solvable with just collision layers/masks.

#

I didn't run into the issue of the ship dropping or slowing down on my end, when I made a barebones version of your project.

#

Would you maybe be able to upload your project zipped up?

sterile tusk
#

Sorry, got busy! Yeah sure let me sort that out.

#

Its not much I might just forget about it for now and work on other features.

acoustic juniper
#

huh? what happened here?

#

wasn't this solved?

abstract wolf
#

I don't think so. I'm unable to check it out right now,

but Space_Prototype.zip should still have the issue occurring. Whereas my version above it (Tests.zip) doesn't have the issue. But the approaches might be different.

#

the problem is that once the player is inside the ship, the ship moves much slower

sterile tusk
acoustic juniper
#

of course it's not ideal..