How to Use Box2D Physics | Coding a 2D Game Engine in Java #48 @GamesWithGabe
How to Use Box2D Physics | Coding a 2D Game Engine in Java #48  @GamesWithGabe
Uploaded May 2021 | Updated September 2026, 2 weeks ago
Join the Discord: discord.gg/4tHeAkxNg7

In this episode I go over various improvements to our physics abstraction over Box2D. This involves creating a callback method for collisions and game objects on the component level. There are 4 events that you can recieve information about, including beginCollision, endCollision, preSolve, and postSolve. I also go over how to implement raycasting using Box2D, creating sensors using Box2D, and a few more physics abstractions. Lastly, I improve the editor a bit more from the last episode by adding multi-select object highlighting, and allowing the user to click and drag and only place one object in each grid square.

Code: github.com/codingminecraft/MarioYoutube/tree/479f2d9d788146da120266f7900126792a67c36d

0:00 Intro
1:10 Highlighting Selected Game Objects
5:00 Fixing Duplicated Objects when Click/Dragging
11:39 Fixing Key Listener Bug
13:10 Box2D Contact Listener
15:51 Aside about Box2D User Data
19:16 Box2D Contact Listener Callbacks
25:08 Raycasting in Box2D
31:54 Various Box2D Helper Functions
37:05 Brief Note on Sensors
37:30 Back to Business
43:52 In the Next Episode

---------------------------------------------------------------------
Website: ambrosiogabe.github.io
Github: github.com/ambrosiogabe

Here are some books I recommend if you want to learn about game engine development more thoroughly. I do not profit off any of these sales, these are just some books that have helped me out :)

My Recommended Game Engine Books:
Game Engine Architecture: gameenginebook.com
Game Physics Cookbook (Read this before the next physics book): amazon.com/Game-Physics-Cookbook-Gabor-Szauer/dp/1787123669
Game Physics (Ian Millington): amazon.com/Game-Physics-Engine-Development-Commercial-Grade/dp/0123819768
Game Programming Patterns (Free): gameprogrammingpatterns.com

My Recommended Beginning Game Programming Books:
JavaScript Game Design: apress.com/gp/book/9781430247166

My Recommended Java Books:
Data Structures/Algorithms: amazon.com/Data-Structures-Algorithms-Java-6th-ebook/dp/B00JDRQF8C
LWJGL (Free, but I haven't read this thoroughly): lwjglgamedev.gitbooks.io/3d-game-development-with-lwjgl/content
How to Use Box2D Physics | Coding a 2D Game Engine in Java #48Switching Languages | Cocoa Engine Devlog #2How Shaders Work (in OpenGL) | How to Code Minecraft Ep. 3Translate and Scale Gizmo | Coding a 2D Game Engine in Java #35Moving the Snake | Java Snake Part 6Spritesheets | Coding Geometry Dash in Java #6Bubble Sort | Python for Absolute Beginners #11SOLID Stinks! How to Write Actual Clean Code
GamesWithGabe |

How to Use Box2D Physics | Coding a 2D Game Engine in Java #48

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER