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

Поделиться
HTML-код
  • Опубликовано: 23 дек 2024

Комментарии • 38

  • @AirmanCS
    @AirmanCS 7 месяцев назад +7

    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 3 месяца назад

      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.

  • @nick12791
    @nick12791 7 месяцев назад +4

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

  • @Doom_C
    @Doom_C 7 месяцев назад +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.

  • @LD_Anvil
    @LD_Anvil 7 месяцев назад +1

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

  • @konstryktor34
    @konstryktor34 6 месяцев назад +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.

  • @UltimatePerfection
    @UltimatePerfection 7 месяцев назад +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  7 месяцев назад

      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 7 месяцев назад +1

      @@MajikayoGames 3D platformer, actually.

    • @Hazan12345
      @Hazan12345 5 месяцев назад

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

  • @Furiac.
    @Furiac. 8 дней назад

    thank you so much. it works but now I just need to study the code some more and understand it.

  • @TRUFANATIC
    @TRUFANATIC 7 месяцев назад +2

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

    • @MajikayoGames
      @MajikayoGames  7 месяцев назад +2

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

    • @Doom_C
      @Doom_C 7 месяцев назад +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 6 месяцев назад +1

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

    • @MajikayoGames
      @MajikayoGames  6 месяцев назад

      awesome!! glad you're enjoying :)

  • @tomasz9429
    @tomasz9429 7 месяцев назад +3

    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.

  • @oddlytimbotwillison6296
    @oddlytimbotwillison6296 6 месяцев назад +1

    Very nice! A good primer for rigidbody interactions.

  • @deathsyrup
    @deathsyrup 7 месяцев назад +6

    Top tier 💯

  • @GeneralChrisGaming
    @GeneralChrisGaming 2 месяца назад +3

    Ice heavier than player
    Also player: fall faster

    • @MajikayoGames
      @MajikayoGames  2 месяца назад

      yeah im not sure why that happened tbh 🤣🤣

  • @jamesbogart3334
    @jamesbogart3334 4 месяца назад

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

  • @rupertkemsley7166
    @rupertkemsley7166 4 дня назад

    Hey, great tutorial!
    I was wondering if you could help with something. How would you go about implementing the character RigidBody3D physics interaction, but only in the y direction?
    I have this seesaw-like object in my game (a long, thin plank of wood resting over the edge of a pit), which is also a RigidBody3D. I've been trying to figure out how to make it so that the player, who has mass, can cause the seesaw to tilt based on their weight and where they’re standing on it.
    Any tips would be really appreciated!

  • @bardmask9089
    @bardmask9089 2 месяца назад +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.

  • @graphtet3751
    @graphtet3751 7 месяцев назад +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  7 месяцев назад

      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 7 месяцев назад +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

  • @PastaMaster115
    @PastaMaster115 6 месяцев назад +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  6 месяцев назад

      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 5 месяцев назад

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

  • @Mrtargi
    @Mrtargi 5 месяцев назад

    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 5 месяцев назад

      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

  • @picnic-in-the-oblivion
    @picnic-in-the-oblivion 5 месяцев назад

    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 4 месяца назад

      same problem, found a way to fix it yet?

    • @bananabarddev
      @bananabarddev 4 месяца назад

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

  • @LopkaUna
    @LopkaUna 3 месяца назад

    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 4 месяца назад

    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