Need help with creating custom collision

Hello everybody, as the title suggests I need help making the pushboxes present within traditional fighting games.

Here is what I got so far

As you can see, when the player faces the right (or does any move related to that ie. jumping_forward) it’s a lot more buggy, but when the player faces the left however, the system is working just as intended.

Here is the code to my “custom collision”:

local Melee = require(game.ReplicatedStorage.Melee).new()
local Util = Melee.Util

local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

-- Player and character setup
local Player = Util.getPlayerVariables(game.Players.LocalPlayer)
local PlayerHRP = Player.HumanoidRootPart
local DummyHRP = workspace.Dummy:WaitForChild('HumanoidRootPart')

-- Define a function to get bounding box corners from a push box
local function getBoundingBox(pushBox)
	local min = pushBox.Position - pushBox.Size / 2
	local max = pushBox.Position + pushBox.Size / 2
	return min, max
end

-- Function to check if two boxes intersect
local function boxesIntersect(min1, max1, min2, max2)
	return (min1.X <= max2.X and max1.X >= min2.X) and
		(min1.Y <= max2.Y and max1.Y >= min2.Y) and
		(min1.Z <= max2.Z and max1.Z >= min2.Z)
end

-- Function to resolve collisions
local function resolveCollision(hrp1, hrp2, deltaTime: number)
	local pushBox1 = Player.Character:WaitForChild('DebugBoxes'):WaitForChild('Collision')
	local pushBox2 = workspace.Dummy:WaitForChild('DebugBoxes'):WaitForChild('Collision')

	if not pushBox1 or not pushBox2 then
		return
	end

	local min1, max1 = getBoundingBox(pushBox1)
	local min2, max2 = getBoundingBox(pushBox2)

	if boxesIntersect(min1, max1, min2, max2) then
		-- Calculate overlap
		local overlapX = (max1.X - min2.X)
		local overlapZ = (max1.Z - min2.Z)

		-- Compute the new CFrame for hrp2
		local newCFrame = hrp2.CFrame - hrp2.CFrame.LookVector*(overlapX/4)

		-- Use TweenService for smooth movement
		TweenService:Create(
			hrp2,
			TweenInfo.new(0.01, Enum.EasingStyle.Linear),
			{CFrame = newCFrame}
		):Play()
	end
end

-- Main update loop
RunService.RenderStepped:Connect(function(deltaTime: number) 
	resolveCollision(PlayerHRP, workspace.Dummy.HumanoidRootPart)	
end)
2 Likes

Fixed it y’all, I js had to take into account relative position.

local Melee = require(game.ReplicatedStorage.Melee).new()
local Util = Melee.Util
local Fighter = Melee.Fighter.new()

local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

-- Player and character setup
local Player = Util.getPlayerVariables(game.Players.LocalPlayer)
local PlayerHRP = Player.Character:WaitForChild('HumanoidRootPart')
local DummyHRP = workspace.Dummy:WaitForChild('HumanoidRootPart')

-- Define a function to get bounding box corners from a push box
local function getBoundingBox(pushBox)
	local min = pushBox.Position - pushBox.Size / 2
	local max = pushBox.Position + pushBox.Size / 2
	return min, max
end

-- Function to check if two boxes intersect
local function boxesIntersect(min1, max1, min2, max2)
	return (min1.X <= max2.X and max1.X >= min2.X) and
		(min1.Y <= max2.Y and max1.Y >= min2.Y) and
		(min1.Z <= max2.Z and max1.Z >= min2.Z)
end

-- Function to resolve collisions
local function resolveCollision(hrp1, hrp2, deltaTime)
	local pushBox1 = Player.Character:WaitForChild('DebugBoxes'):WaitForChild('Collision')
	local pushBox2 = workspace.Dummy:WaitForChild('DebugBoxes'):WaitForChild('Collision')

	if not pushBox1 or not pushBox2 then
		return
	end

	local min1, max1 = getBoundingBox(pushBox1)
	local min2, max2 = getBoundingBox(pushBox2)

	if boxesIntersect(min1, max1, min2, max2) then
		-- Calculate overlap based on relative position
		local overlapX = (max1.X - min2.X)
		local overlapDirection = hrp1.Position.X > hrp2.Position.X and -1 or 1
		
		
		if overlapDirection == -1 then
			overlapX = -1*(max2.X-min1.X)
		end
	
				
		local newCFrame = hrp2.CFrame + Vector3.new(overlapX, 0, 0)
	
		
		TweenService:Create(
			hrp2,
			TweenInfo.new(0.02, Enum.EasingStyle.Linear),
			{CFrame = newCFrame}
		):Play()
	
	end
end


-- Main update loop
RunService.RenderStepped:Connect(function(deltaTime)
	resolveCollision(PlayerHRP, DummyHRP, deltaTime)
end)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.