Godot 4: Character to Rigid Body Interaction
Key Takeaways
This video teaches how to handle interaction between character bodies and rigid bodies in Godot 4
Full Transcript
welcome to another Godot recipes video this time around we're going to talk about how to use a character body to interact with a rigid body one of the most common physics related questions that comes up in Godot is when you have a character body like my little green Cube here and these red squares are rigid bodies and if all you do is add your character body and your rigid bodies and you're trying to push them you'll see nothing happens you can't push them and it doesn't matter what you set their Mass to they won't move this is the default Behavior the rigid bodies which are controlled by the physics engine are not detecting any collision with the character body which is being moved in code to solve this we have two options in the project settings I've created three physics layers one for the world one for the player and one for items such as the Box and then I've placed each object in those layers so the player node is in Layer Two and the box is in layer three and that's the default Behavior if we ran it right now nothing will have changed but let's go into the player mask and remove three so now when the player moves it will not detect collisions with the rigid bodies so it's just going to plow right through them but the rigid body is going to still try and separate because the rigid body can see layer 2 which the player is on so it's going to want to keep its Collision box separated so let's try that and see what that looks like so now you can see I'm pushing them however there are some problems if I keep pushing against it I it will clip through the wall and you are not able to jump on top of them because you'll push them right through the ground Collision those of you who are familiar with good O3 will recognize this as the default Behavior which was called infinite inertia in that version of movement slide and so for some games this will be this will be just fine you can shove the boxes they fall over and you're not concerned with wall glitches and such but if you want something a little more robust where things do not glitch out when you push into the box then we need to try option number two okay for this option we're going to put the mask back so we're back to the way we started the character body won't push the rigid body and this time we're going to add some code into our player so when the character body hits a rigid body it's going to apply an Impulse or a push to the rigid body so in our player code we're going to add a variable here to control how strong that force is and then in our physics process after a move and slide we're going to get the slide Collision and if the thing we collided with was a rigid body then we're going to apply an Impulse in the opposite direction from the Collision normal which will mean into the rigid body using that push Force so now when we run we'll see we can push but now if I push against the wall I can't push it through the wall and if I jump on top of it I also can't glitch it through the ground but I can push them and they'll slide up hills and so on now how strongly you want to set that push force is going to depend on a few factors how fast your character moves how massive the boxes are as you can see here the boxes I've given them a mass of one if the boxes have a really high mass you're going to need a stronger push Force and so it's something you're going to have to adjust based on your particular game this will work just as well in 3D as it does in the 2D example here and some other things you might want to try for your game if you're experimenting with this is to perhaps move use the velocity of your player as a factor in the push Force you can scale it up or down based on how fast your character is moving things like that you can find this in many other good old recipes tutorials examples and tips at good or recipes.com don't forget to like And subscribe on YouTube to get the latest video tutorials and if you'd like to help support these efforts please consider clicking the patreon link thanks
Original Description
A common question: how to handle interaction between a character body and a rigid body. Works in 2D and 3D.
Text version and link to code:
https://kidscancode.org/godot_recipes/4.x/physics/character_vs_rigid
Want to learn Godot 4? Check out my new book, Godot 4 Game Development Projects.
Available now! https://amzn.to/3qcOZ4E
Support me on Patreon: https://www.patreon.com/kidscancode
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from KidsCanCode · KidsCanCode · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
KidsCanCode - Patreon Intro Video
KidsCanCode
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
Extra: Setting up the Atom Editor for Python
KidsCanCode
Game Development 1-1: Getting Started with Pygame
KidsCanCode
Game Development 1-2: Working with Sprites
KidsCanCode
Game Development 1-3: More About Sprites
KidsCanCode
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
Pygame Shmup Part 8: Sound and Music
KidsCanCode
Pygame Shmup Part 9: Shields
KidsCanCode
Pygame Shmup Part 10: Explosions
KidsCanCode
Pygame Shmup Part 11: Player Lives
KidsCanCode
Pygame Shmup Part 12: Powerups
KidsCanCode
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
Pygame Platformer Part 1: Setting Up
KidsCanCode
Pygame Platformer Part 2: Player Movement
KidsCanCode
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
Pygame Platformer Part 4: Jumping
KidsCanCode
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
Pygame Platformer Part 6: Game Over
KidsCanCode
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
Pygame Platformer Part 8: Saving High Score
KidsCanCode
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
Pygame Platformer Part 14: Sound and Music
KidsCanCode
Pygame Platformer Part 15: Powerups
KidsCanCode
Pygame Platformer Part 16: Enemies
KidsCanCode
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
Tile-based game Part 1: Setting up
KidsCanCode
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
Tile-based game Part 3: Smooth Movement
KidsCanCode
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
Tile-based game Part 5: Player Graphics
KidsCanCode
Related Reads
📰
📰
📰
📰
How Creative Characters Like Sprunki Inspire Kids to Learn Coding
Medium · Programming
Computer Applications for Primary School Children: A Fun + Safe Guide for Ages 6-12
Dev.to · Ogunkola Adeola
From 0 to 20 Chapters: My Story‑Driven Rust Book for Kids Now Has a Bilingual Interactive Demo
Dev.to · born1987-ir
How Kids Can Build Fighting Games Stickman in Scratch
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI