Image Update: Non-Axis-Aligned Box


《Drifting : Weight of Feathers》

Steam - Steam Page
Itch.IO - Non-Axis-Aligned Box
IndieDB - Non-Axis-Aligned Box
UE4 Forum - UE4 Forum

Non-Axis-Aligned Box

I encountered a seemingly simple mathematic problem yesterday, and trying to figure out an appropriate solution for an entire morning:

Check whether a point is inside a non-axis-aligned box

Calling UPrimitiveComponent::IsOverlappingActor() was my first attempt, however, after some thought experiments I abandon the idea for 2 reasons: first, the point is not represented as an actor (creating a collidable dummy actor will increase calculation overhead and further complicate my colliding channel); second, it will be much better performance-wise to avoid using per tick collision event if I could figure out a way to use mathematic to solve the problem.

Calling UKismetMathLibrary::IsPointInBox() was also a failed attempt since the box is non-axis-aligned, the function assumed the box is perfectly aligned with the world coordination (simply comparing the max/min x,y,z values will return incorrect results if the box is rotated). Another version of the function - UKismetMathLibrary::IsPointInBoxWithTransform() seems to do what I wanted - calling the IsPointInBox() after transforming the point from world coordinate system to component coordinate system - however, for some unknown reason (perhaps incorrect value for the input component transform?), the function still gave me the incorrect result after calling it.


(if you go through the four sides of the box clockwise, check the point is at the right of each side; Instead, if you go through the four sides counter-clockwise, check the point is at the left of each side)

I googled and found various ways to solve the problem on StackOverflow - alas, due to my poor math grade in high school, I cannot comprehend the logic behind all these clean and clever solutions! I was hesitant to add these methods to my project before gaining an understanding of how it works. After some more failed attempts to try and understand the online solutions, one of the solution gave me an idea of how to (partially) solve the problem:

Check the point is at the right of each side of the box in 2D, then compare the max/min z value

The obvious downside of this method is it only works if the box is rotated in the z-axis, a rotation in x, y axis will give incorrect results - not a general solution to the problem but good enough for the particular problem I encountered in my project. I proceed to implement the method into my code and it works like a charm, then I happily close my project and went out and grab something to eat for lunch. XD

I hope you enjoy this week's update, feel free to provide an even easier solution for people that are not good at math (such as me) to give them a better time to develop their games! 

Have a relaxing weekend! d(゚∀゚*)   

Get 《Drifting : Weight of Feathers》

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.