This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

How to move player along with moving platform ?

GarromOrcShamanGarromOrcShaman Posts: 214Member
edited May 2018 in Programming

Hi everyone. I have a moving platform and need to set player(ball-shaped rigidbody) to be child of platform so player will move with platform. Problem is that i need to reparent node without removing and adding it because i need to maintain physical momentum and variables. I tryed to set owner variable but it thorow error Coddition '!ovner_valid' is true .Is there any way how to reparent node without destroying it ?
EDIT : Parenting node does not work because child does not inherit position/rotation. Further discussion is about how to make moving platform/player movement work.


Tags :

Answers

  • GarromOrcShamanGarromOrcShaman Posts: 214Member

    Hmm. This might help but debug says :
    0:00:08:0737 - Can't add child 'Player' to 'Floor7', already has a parent 'Level'. Type:Error Description: Can't add child 'Player' to 'Floor7', already has a parent 'Level'. Time: 0:00:08:0737 C Error: Condition ' p_child->data.parent ' is true. C Source: scene\main\node.cpp:1331 C Function: Node::add_child

    Yes, Floor7, This is what I love about scripts, you can make moving platform of absolutely everything just by adding right script. Floor7 was originally meant as static part of level. Level node is root node.

  • arthurarthur Posts: 34Member

    You need to remove_child before add_child. remove_child does not free node.

  • GarromOrcShamanGarromOrcShaman Posts: 214Member

    @arthur said:
    You need to remove_child before add_child. remove_child does not free node.

    Now I'm able to parent player (rigidbody) to platform (staticbody) but player does not inherit position. It is bug or my mistake ? How i can make rigidbody inherit position/rotation from static body ?

  • SeaFishelleSeaFishelle Posts: 39Member

    @GarromOrcShaman said:

    @arthur said:
    You need to remove_child before add_child. remove_child does not free node.

    Now I'm able to parent player (rigidbody) to platform (staticbody) but player does not inherit position. It is bug or my mistake ? How i can make rigidbody inherit position/rotation from static body ?

    Manually?

    transform += Floor7.transform

  • GarromOrcShamanGarromOrcShaman Posts: 214Member

    @SeaFishelle said:

    @GarromOrcShaman said:

    @arthur said:
    You need to remove_child before add_child. remove_child does not free node.

    Now I'm able to parent player (rigidbody) to platform (staticbody) but player does not inherit position. It is bug or my mistake ? How i can make rigidbody inherit position/rotation from static body ?

    Manually?

    transform += Floor7.transform

    Apparently, operator "+" cannot be used to "Transform" and "Transform". Adding origins does not work neither, it fires player away instead of copying platform position.

  • arthurarthur Posts: 34Member

    I don't think it will. You want to teleport your player or something like this?
    If so, you can try to change player's global_position after adding it to new parent

  • GarromOrcShamanGarromOrcShaman Posts: 214Member

    @arthur said:
    I don't think it will. You want to teleport your player or something like this?

    Nope. If player stands on moving platform I want him to move together with platform while able to move around platform on its own. In Unity, things like this was done by setting parent but in GoDot, player (rigidbody) does not inherit position and rotation from platform (staticbody). Question is how i can make player (rigidbody) inhert from his parent, the moving platform (staticbody)

  • SeaFishelleSeaFishelle Posts: 39Member

    I accomplished this in BGE a few years ago. If I remember, I'll look at my old projects when I get home and see how I did it. Being BGE, I of course had to hack it to hell and back to get around the bizarre quirks of the parent/child physics system... Hopefully with Godot's much more straight-forward design, it should be a relatively simple concept application.

  • GarromOrcShamanGarromOrcShaman Posts: 214Member

    @SeaFishelle said:
    I accomplished this in BGE a few years ago. If I remember, I'll look at my old projects when I get home and see how I did it. Being BGE, I of course had to hack it to hell and back to get around the bizarre quirks of the parent/child physics system... Hopefully with Godot's much more straight-forward design, it should be a relatively simple concept application.

    Thanks a lot ! it sounds promising !

  • GarromOrcShamanGarromOrcShaman Posts: 214Member

    I tried to experiment with velocity using line below.
    body_object.linear_velocity = body_object.linear_velocity + self.linear_velocity
    It does not seems to work, but i think I'm on right track now.

  • elkondoelkondo Posts: 17Member
    edited May 2018

    Let me just point you to a tutorial I made which touches on this (in 2D).
    https://godotdevelopers.org/forum/discussion/19373/tutorial-pixel-perfect-moving-platforms-with-one-side-collision

    In short, I use custom collision detection to check whether the player has landed on the platform (though you can just use areas or so if you don't need it pixel-perfect) and if so, keep changing his global_position in the platform's script until he jumps, walks off of the platform or gets stuck on a ledge.

    As for using linear_velocity, the issue I had with it is that you need constant gravitational force, because it only works when the bodies are touching. In my code I only added downward momentum to the player if he wasn't on the floor, which in practice meant he would flicker between >0 and 0 speeds every frame if he was standing on something. Because of that, linear_velocity would only affect him every other frame, moving him at half the speed I set it to.
    Also, keep in mind that (I think) linear_velocity only works on certain types of nodes, the ones affected by physics, like RigidBody. It might not work with static or kinematic, though don't quote me on this.

  • GarromOrcShamanGarromOrcShaman Posts: 214Member

    @elkondo said:
    Let me just point you to a tutorial I made which touches on this (in 2D).

    My game is way too different to make it work. My player is ball shaped rigid body moved only by adding torque to body. And my game is 3D

    As for using linear_velocity, the issue I had with it is that you need constant gravitational force, because it only works when the bodies are touching.

    I think my problem is different. Look at gif below to have know how my game looks like
    Image showing my problem
    As you can see my only problem is player not keeping up with platform.

    Also, keep in mind that (I think) linear_velocity only works on certain types of nodes, the ones affected by physics, like RigidBody. It might not work with static or kinematic, though don't quote me on this.

    You are right. Player is rigidbody and platform is static body by defalut but I'm trying kinematic body now. But if i switch platform to rigid body it starts blinking up and down because player is made child of platform.

  • MagicLordMagicLord Posts: 578Unconfirmed

    @GarromOrcShaman said:

    @elkondo said:
    Let me just point you to a tutorial I made which touches on this (in 2D).

    My game is way too different to make it work. My player is ball shaped rigid body moved only by adding torque to body. And my game is 3D

    As for using linear_velocity, the issue I had with it is that you need constant gravitational force, because it only works when the bodies are touching.

    I think my problem is different. Look at gif below to have know how my game looks like
    Image showing my problem
    As you can see my only problem is player not keeping up with platform.

    Also, keep in mind that (I think) linear_velocity only works on certain types of nodes, the ones affected by physics, like RigidBody. It might not work with static or kinematic, though don't quote me on this.

    You are right. Player is rigidbody and platform is static body by defalut but I'm trying kinematic body now. But if i switch platform to rigid body it starts blinking up and down because player is made child of platform.

    Physics have bugs and some configuration won't work in Godot.
    It's not a good practice to use parent and child for physics, and have parent and child as different physics nodes. And most of the time it won't work, Godot expects the first node of your prefab to be a physic node and only one.

    Most games are about hacks.
    When your kinematic player is on top of the platform, use raycast and keep it a little above the physic platform like 0.01 m above , and smooth it's Y position with some lerp.

  • elkondoelkondo Posts: 17Member
    edited May 2018

    @GarromOrcShaman said:

    @elkondo said:
    Let me just point you to a tutorial I made which touches on this (in 2D).

    My game is way too different to make it work. My player is ball shaped rigid body moved only by adding torque to body. And my game is 3D

    I think the same general solution could be applied though. Find out if the ball is on the platform, if it is, update its global_position in the platform's code while forcing the ball to move around as if it was on the ground.
    Also, I'm not a fan of relying on physics for movement and such. How about just faking the torque-based movement, calculate linear speeds yourself and just move the ball around via global_position? I have a feeling it'll be much more reliable and should feel better in general.

  • antonioabantonioab Posts: 2Member
    edited June 2018

    Hi.
    I had problems with moving platforms, but with parenting, i solveld the movement artifacts
    I use this function in a 2D context, but it has to be valid for 3D
    This function translate global to local and local to global positions
    Use a custom on_platform detection before reparenting ( RayCast ... etc )

    func change_parent( target ):
        var parent = get_parent()
        if target != parent :
            var tmp = get_global_transform().xform( position )
            parent.remove_child( self )
            target.add_child( self )
            position = get_global_transform().xform_inv( tmp )
    

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.