How to Raycast Against a Box (OBB) | Coding a 2D Physics Engine in Java #9 @GamesWithGabe
How to Raycast Against a Box (OBB) | Coding a 2D Physics Engine in Java #9  @GamesWithGabe
Uploaded July 2020 | Updated September 2026, 3 weeks ago
Join the Discord: discord.gg/4tHeAkxNg7

In this tutorial, I explain how to manually create a ray and cast it against a rotated box. I show how you can use some vector projections and a bit of trigonometry to find the exact point at which a ray hits a rotated box. I also describe the cases when the ray would miss the box, and how to detect those cases. Lastly, I describe how to detect where the ray hits the box if the origin of the ray is inside the box. This is just one method of solving this particular problem, there may be better solutions, but this is a good introduction to how to solve a raycast.

Source Code: github.com/codingminecraft/MarioYoutube/tree/537d8b589d13ecc4a2a830ff45c607a2feb4b381

0:00 Intro
0:39 Raycasting Box Math
10:55 Coding the Raycast

---------------------------------------------------------------------
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

Outro Music: bensound.com/royalty-free-music
How to Raycast Against a Box (OBB) | Coding a 2D Physics Engine in Java #9Adding an Engine Runtime (Play/Stop Buttons) | Coding a 2D Game Engine in Java #39ImGui Docking in Java LWJGL | Coding a 2D Game Engine in Java #28Why C++ Scares the Crap Out of MeFor Loops | Python For Absolute Beginners #6The Component Pattern | Coding Geometry Dash in Java #4Serialization | Coding Geometry Dash in Java #14ImGui Fonts and Scene Integration | Coding a 2D Game Engine in Java #18How OpenGL Graphics Programming Works | Coding a 2D Game Engine in Java #4Adding Multiplayer, Colored Lights, and More | Coding Minecraft Devlog #2Bitmap Generation | Font Rendering in Java #1Compiler Options & Visual Studio  | C++ For Java Devs Ep. 5
GamesWithGabe |

How to Raycast Against a Box (OBB) | Coding a 2D Physics Engine in Java #9

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER