Detecting Whether An Object Is Within The Boundaries Of A Rotated Cube

Hi, I am currently trying to find whether the player’s humanoid root part is within the boundaries of a cube. I tried

local entityLocation = -- player character humanoid root part position
local bulletInstance = -- cube part
local bulletLocation = bulletInstance.Position
local hitboxSize = bulletInstance.Size * 3 -- three is an arbitrary number which just closely matches the dimensions of the object

local function OutsideCubeBoundaries()
	for _, axisInstance in pairs({"x", "y", "z"}) do
		if (math.abs(entityLocation[axisInstance] - bulletLocation[axisInstance]) > hitboxSize[axisInstance]) then
			return true
		end
	end
end
if not OutsideCubeBoundaries() then
	print("in cube")
end

However, it doesn’t work accurately if the cube is rotated, so I am looking for a new method. How can I detect if the player is within the bounds of a rectangle rotated at, for example, 45 degrees? I’d like to not use raycasting if possible and I cannot use the Touched connection.

1 Like

can you just use get touching parts and loop through all the parts and if there arent any relevant ones then you can determine it?

2 Likes

Hi, this would probably work the best for my needs if get touching parts worked differently, but since it uses a touch interest like the touched connection, it can be exploited by the client just by deleting it.

1 Like

I have kind of thought of a method in which I can get all of the vertices of a cube by adding or subtracting three axes of its size divided by two multiplied by the cube’s CFrame, but I am still unsure how I could use this to see whether another position is inside the boundaries of eight points.

1 Like

You can try this instead. Let me know if it works:

local entity = -- player character humanoid root part
local bullet = -- cube part

local function OutsideCubeBoundaries()
	if (table.find(workspace:GetPartsInPart(entity), bullet)) then
		return true
	end
end
if not OutsideCubeBoundaries() then
	print("in cube")
end
2 Likes

If I’m not mistaken, the OutsideCubeBoundaries() function returns a boolean which depicts if the player is INSIDE the cube. You have a not operator in the if statement, this would only run if the player was OUTSIDE of the cube.

Something like this would achieve what the OP is requesting:

local function InsideCubeBoundaries(player: Player, cube: BasePart): boolean
	local Character = player.Character
	if (Character) then
		local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
		if (HumanoidRootPart) then
			return not not table.find(workspace:GetPartsInPart(cube), HumanoidRootPart)
		end
	end
	return false
end

-- Usage: InsideCubeBoundaries(player: The player to do the check on, cube: The part in which to check if the player is inside)
2 Likes

Hi, the GetPartsInPart method looks like it would work very well for my situation. However, I am creating the bullet on a local script and moving its hitbox on the server. Because of this, I don’t have the part available and I would have to create and tween an invisible object made on the server just to get this to work. I did find the GetPartBoundsInBox function while looking at the documentation of GetPartsInPart which I think will work if there isn’t another way to do so using math, so @daisytheghostchild98 's response has still been helpful, thanks.

1 Like

If you’re creating a bullet system, I suggest using raycasts instead of seeing if anything is inside a part.

1 Like


unconventional bullets. Collision only works for spheres.

1 Like

Ah, I see. My bad, then stick with regular collision.

2 Likes

you should try one of these if I remember right they use rotation of the part

2 Likes

I think the GetPartBoundsInBox function will work the best and I will use that if there is no better solution. It would be a little bit more efficient to test if just one object is in a bullet rather than finding all of the objects in it and then sorting through that, so is there a formula or some way that I can use to detect if a position is inside the eight vertices of a cube, if I already have all of the positions?

2 Likes

check out this section on that page you can filter what is in by the same way raycast work

most ppl use raycast to determine if a player or object is in the line of the bullet and do damage by that if i am understanding what your trying to do

1 Like

Nyonic’s solution works the best. I also found some more advanced documentation on the subject which I may use if I want the hitbox to be smaller or a different shape than the bullet’s perception itself at c# - How to determine a point is inside or outside a cube? - Stack Overflow . Thanks!

1 Like