Character Body to Rigid Body 3D Physics Interaction | Godot 4 Tutorial

Поділитися
Вставка
  • Опубліковано 15 лис 2024

КОМЕНТАРІ • 36

  • @AirmanCS
    @AirmanCS 6 місяців тому +6

    Love the ending bc is true I tried to do a 3D breakout bc it was "simple" and I almost end up, calculating all normals and velocities by hand xD, at the end I conformed myself with a decent result and left it as a nice prototipe of a simple game that I will polish if necessary, but physics took me by surprise when they are part of the gameplay

    • @MichaelGame_Dev
      @MichaelGame_Dev 2 місяці тому

      Doing a 3d breakout now. Personally, I would recommend a characterbody for the ball and paddles just due to this. I'm using rigidbody's for the bricks. I want some of the bricks to slide. I can get them to move, but it doesn't feel right, so digging into that some.

  • @konstryktor34
    @konstryktor34 5 місяців тому +2

    I can finally implement physics in my game with character bodies. It was a big problem for me, cause I am not a coder, but i wanted it so bad! Learning something new everyday. Definitely like and subscribe.

  • @GeneralChrisGaming
    @GeneralChrisGaming 23 дні тому +1

    Ice heavier than player
    Also player: fall faster

    • @MajikayoGames
      @MajikayoGames  23 дні тому

      yeah im not sure why that happened tbh 🤣🤣

  • @LD_Anvil
    @LD_Anvil 6 місяців тому +1

    Special thanks to you, I have looked around for physics interaction for a long time with no result.

  • @nick12791
    @nick12791 6 місяців тому +4

    Keep them coming I'm here for it, another amazing video!

  • @Doom_C
    @Doom_C 6 місяців тому +1

    Funny that you upload this, was just messing around with your old source code earlier today to check if physics was applied to objects.

  • @deathsyrup
    @deathsyrup 6 місяців тому +6

    Top tier 💯

  • @tomasz9429
    @tomasz9429 6 місяців тому +2

    You can get some funny results while playing around with the BOUNCE property on the RigidBody3D. Managed to launch myself into space again after colliding with two objects which had bounce set to 1. I saw Azathoth this time. Wouldn't recommend.

  • @jamesbogart3334
    @jamesbogart3334 3 місяці тому

    Thanks, this worked great and was exactly what I needed. No other good answers for this that I could find.

  • @TRUFANATIC
    @TRUFANATIC 6 місяців тому +2

    Bro! You are my hero, thank you, your videos is exactly what I wanted!

    • @MajikayoGames
      @MajikayoGames  6 місяців тому +2

      :D more to come. going to keep building out this fps controller/game for a while.

    • @Doom_C
      @Doom_C 6 місяців тому +1

      @@MajikayoGames You're the man. ATM this is by far the best fps controller out there for Godot, and I've checked a lot of them.

  • @ignis621
    @ignis621 4 місяці тому +1

    thanks man! ive been getting used to godot and your videos are a great help. you're awesome!

  • @UltimatePerfection
    @UltimatePerfection 6 місяців тому +2

    I've just used a rigid body for my character. I realize that I have made it harder on myself in some ways, but I can work around those issues and as a bonus I don't have to worry about making it possible for the character to be moved by a moving obstacle in physically accurate manner (i. e. character flies away after being hit by an oversized hammer) and weird collision issues such as character being stuck in the walls or whatever as the physics engine will do that work for me.

    • @MajikayoGames
      @MajikayoGames  6 місяців тому

      Yes this is definitely one possible solution, this could be good for a lot of games. I probably should have mentioned this in the video too. Like you said it could have some downsides but all depends on what type of game you're making.

    • @UltimatePerfection
      @UltimatePerfection 6 місяців тому +1

      @@MajikayoGames 3D platformer, actually.

    • @Hazan12345
      @Hazan12345 3 місяці тому

      What problems have you face using rigid body for your controller? Curious to know the ups and downs of each type for godot

  • @oddlytimbotwillison6296
    @oddlytimbotwillison6296 4 місяці тому +1

    Very nice! A good primer for rigidbody interactions.

  • @bardmask9089
    @bardmask9089 Місяць тому +1

    im having problems with this method, first my character has deceleration instead of instantly stopping which makes the rigidbodies "stick" to the player if he moves slowly, also theres an issue where the rigidbody can "phase" through other colliders when pushed directly into them.

  • @PastaMaster115
    @PastaMaster115 5 місяців тому +1

    It doesn't appear to be working. I don't have the weight shapes you have in the video. But I'm playing with the colored cubes and spheres, which I can see already came with programmed masses. 10s, 7s, 5, and 1s. But it seems they all push exactly the same. I've even increased their masses to 1000 and still get the same result. I even copied your code directly to see if I did something wrong. I could see you got way more resistance from the bigger shapes. With me that's not happening.

    • @MajikayoGames
      @MajikayoGames  5 місяців тому

      maybe because they are spheres, easier to push, but yes it's not a perfect method, it would be very difficult to get perfect physics interaction. this is just a simple method to apply some force when pushing against objects. as i said at the end, for more complex cases you would probably want to program it in yourself.

  • @Mrtargi
    @Mrtargi 4 місяці тому

    Do you have Idea how can Character body attach rigid body to itself or drag or pull it?

  • @Mrtargi
    @Mrtargi 4 місяці тому

    Thank you for tutorial! I have a strange issue that everything is working perfectly with small weights, but if I put it for example 30kg it is really hard to move, no matter what weight does my character have. Tried to play with friction option but it does not help. What could it be? Or I'm not seeng obvious? I'll provide a code snippet under that comment. Thanx in advance

    • @Mrtargi
      @Mrtargi 4 місяці тому

      extends Node
      class_name MovementController
      @export var player : Player
      @export var mesh_root : Node3D
      @export var rotation_speed : float = 8
      @export var fall_gravity : float = 45
      var direction : Vector3
      var velocity : Vector3
      var cam_rotation : float = 0
      var player_init_rotation : float
      var acceleration : float
      var speed: float
      func _ready():
      player_init_rotation = player.rotation.y
      func _physics_process(delta: float) -> void:
      velocity.x = speed * direction.normalized().x
      velocity.z = speed * direction.normalized().z

      if not player.is_on_floor():
      velocity.y -= fall_gravity * delta

      player.velocity = player.velocity.lerp(velocity, acceleration * delta)
      _push_rigid_bodies()
      player.move_and_slide()

      var target_rotation = atan2(direction.x, direction.z) - player.rotation.y
      mesh_root.rotation.y = lerp_angle(mesh_root.rotation.y, target_rotation, rotation_speed * delta)

      ## Debug prints
      #print("Direction: ", direction)
      #print("Velocity: ", velocity)
      #print("Player Velocity: ", player.velocity)
      #print("Target Rotation: ", target_rotation)
      #print("Mesh Root Rotation Y: ", mesh_root.rotation.y)
      func _push_rigid_bodies():
      for i in player.get_slide_collision_count():
      var c := player.get_slide_collision(i)
      if c.get_collider() is RigidBody3D:
      var push_dir = -c.get_normal()
      # How much velocity the object needs to increase to match player velocity in push direction
      var velocity_diff_in_push_dir = player.velocity.dot(push_dir) - c.get_collider().linear_velocity.dot(push_dir)
      # Only counts velocity towards push dir, away from character
      velocity_diff_in_push_dir = max(0., velocity_diff_in_push_dir)
      # Objects with bigger mass than us should be harder to push. But should not be pushed faster than we are moving

      var mass_ratio = min(1., player.PLAYER_APPROX_MASS_KG / c.get_collider().mass)

      # Don't push object from above below
      push_dir.y = 0
      var push_force = mass_ratio * 5.0
      c.get_collider().apply_impulse(push_dir * velocity_diff_in_push_dir * push_force, c.get_position() - c.get_collider().global_position)

      func _on_set_movement_state(_movement_state : MovementState):
      speed = _movement_state.movement_speed
      acceleration= _movement_state.acceleration

      func _on_set_movement_direction(_movement_direction: Vector3):
      direction = _movement_direction.rotated(Vector3.UP,cam_rotation + player_init_rotation)
      func _on_set_cam_rotation(_cam_rotation : float):
      cam_rotation = _cam_rotation

  • @graphtet3751
    @graphtet3751 6 місяців тому +1

    After I tried this my character in game is not moving when i try to move i am not receiving any errors but i can’t move my player character how do i solve this

    • @MajikayoGames
      @MajikayoGames  6 місяців тому

      can't think of why that would happen. you're talking about with the _push_away_rigid_bodies function? does it still work if you comment out the function call before move and slide? and you are still calling move_and_slide right?

    • @graphtet3751
      @graphtet3751 5 місяців тому +1

      @@MajikayoGames I went back through to check the move and slide and found out that the problem was i placed the function inside the physics process after moving the push rigid bodies function above the physics process it started working as intended

  • @picnic-in-the-oblivion
    @picnic-in-the-oblivion 4 місяці тому

    Hello, I've used _push_away_rigid_bodies() function, put it before move_and_slide() all works good, but when I jump on the rigidbody3d object it behave very weird and glitchy (I try to stand on barrel mesh, so when Its moved and I jumped on it, barrel with pushed away very fast and fly away :D)

    • @SomeWhiteGuy1
      @SomeWhiteGuy1 3 місяці тому

      same problem, found a way to fix it yet?

    • @bananabarddev
      @bananabarddev 2 місяці тому

      Probably forgot to change the Y value in push_dir Vector3 to 0

  • @LopkaUna
    @LopkaUna 2 місяці тому

    works flawlessly!
    i also strict height of collision with
    [if self.global_position.y - get_slide_collision(clsns).get_position(cldrs).y < 0.9:]
    so i got no issues with walking on little objects

  • @Ver2ion
    @Ver2ion 2 місяці тому

    im trying to get this code to stop working if the player is above or on top of the object because if i stand on an object they will start to get impulses until the object like explodes and i go flying off of it lol
    UPDATE:
    put this after push_dir for more better accurate results without glitches and launching in the air
    if abs(push_dir.y) > 0.1: # Adjust this threshold if needed
    continue