Line vs Box2D AKA Raycasting | Coding a 2D Physics Engine in Java #7 @GamesWithGabe
Line vs Box2D AKA Raycasting | Coding a 2D Physics Engine in Java #7  @GamesWithGabe
Uploaded July 2020 | Updated September 2026, 2 weeks ago
Join the Discord: discord.gg/4tHeAkxNg7

In this tutorial, I explain how to check if a line is intersecting with a box. The way that I accomplish this is using a raycasting techinque (you could achieve the same result using variants of clipping algorithms). I extensively explain the mathematics behind this, going over a concrete example of how the raycast works. I explain how we can use the minimum and maximum of an AABB (2D box) to figure out the entry point and exit point of an intersecting line. Then, we implement the method. I also go over how to extend this functionality to check if a line is intersecting with an OBB (2D rotated box).

Source Code: github.com/codingminecraft/MarioYoutube/tree/df92aa761308cf34906a347ddce908eebff120af

0:00 Intro
0:32 Math behind line vs box
15:38 Coding line vs box method

---------------------------------------------------------------------
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/K
My Camera: amzn.to/30Y22I7
My Microphone: amzn.to/2BpYbbT

Outro Music: bensound.com/royalty-free-music
Line vs Box2D AKA Raycasting | Coding a 2D Physics Engine in Java #7Fireballs and Bug Fixes | Coding a 2D Game Engine in Java #57Deserialization Part 2 | Coding Geometry Dash in Java #16Why Minecraft is a Technical Feat | Explaining the Engineering Behind an Indie IconCreating a Scene Manager & Delta Time Variable | Coding a 2D Game Engine in Java #3Rendering the First Character | Font Rendering in Java #2Mario Turtle AI | Coding a 2D Game Engine in Java #55Dictionaries | Python for Absolute Beginners #10Editor Camera Controls with Lerp | Coding a 2D Game Engine in Java #33Alpha Blending and Z-Indexing | Coding a 2D Game Engine in Java #16What is Path??Translating 10,000 Lines of Java Code to C++ Part 1 | Cocoa Engine Devlog #9
GamesWithGabe |

Line vs Box2D AKA Raycasting | Coding a 2D Physics Engine in Java #7

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER