Skip to content

update collision hashmap on instance changes #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions src/Simulation/CollisionModule.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
local UPDATE_ON_PROP_CHANGE = {
"Size", "Position", "Orientation", "CanCollide"
} --when these properties change within a part. re-calculate its collision

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

Expand Down Expand Up @@ -191,14 +195,14 @@ function module:WritePartToHashMap(instance, hullRecord)
end
end


function module:RemovePartFromHashMap(instance)
-- size data is the cached previous cframe/size
-- used to clear a part's previous record in the hashmap after its cframe or size has changed
function module:RemovePartFromHashMap(instance, sizeData)
if instance:GetAttribute("ChickynoidIgnoreRemoval") then
return
end

local minx, miny, minz, maxx, maxy, maxz = self:FindAABB(instance)
local minx, miny, minz, maxx, maxy, maxz = self:FindAABB(sizeData or instance)

if (maxx-minx > self.fatPartSize or maxy-miny > self.fatPartSize or maxz-minz > self.fatPartSize) then

Expand Down Expand Up @@ -396,6 +400,27 @@ function module:GenerateSnappedCFrame(instance)
)
end

local function makeRecord(instance)
return {
instance = instance,
Size = instance.Size,
CFrame = instance.CFrame
}
end

function module:UpdateCollisionOnInstance(instance)
module:RemovePartFromHashMap(instance, module.hullRecords[instance])

if instance.CanCollide == false then
return
end

local record = makeRecord(instance)
self:WritePartToHashMap(instance, record)

module.hullRecords[instance] = record
end

function module:ProcessCollisionOnInstance(instance, playerSize)
if instance:IsA("BasePart") then
if instance.CanCollide == false then
Expand Down Expand Up @@ -432,10 +457,9 @@ function module:ProcessCollisionOnInstance(instance, playerSize)
return
end]]--

local record = {}
record.instance = instance
local record = makeRecord(instance)
--record.hull = self:GenerateConvexHullAccurate(instance, playerSize, self:GenerateSnappedCFrame(instance))
self:WritePartToHashMap(record.instance, record)
self:WritePartToHashMap(instance, record)

module.hullRecords[instance] = record
end
Expand Down Expand Up @@ -843,9 +867,18 @@ function module:MakeWorld(folder, playerSize)
local startTime = tick()
local meshTime = 0

local function updateOnChange(instance)
for _, prop in pairs(UPDATE_ON_PROP_CHANGE) do
instance:GetPropertyChangedSignal(prop):Connect(function()
self:ClearCache()
self:UpdateCollisionOnInstance(instance)
end)
end
end

coroutine.wrap(function()
local list = folder:GetDescendants()
local total = #folder:GetDescendants()
local total = #list

local lastTime = tick()
for counter = 1, total do
Expand All @@ -855,7 +888,10 @@ function module:MakeWorld(folder, playerSize)

local begin = tick()
self:ProcessCollisionOnInstance(instance, playerSize)
local timeTaken = tick()- begin

updateOnChange(instance)

local timeTaken = tick() - begin
if (instance:IsA("MeshPart")) then
meshTime += timeTaken
end
Expand All @@ -866,7 +902,7 @@ function module:MakeWorld(folder, playerSize)
if (tick() - lastTime > maxTime) then
lastTime = tick()

wait()
task.wait()

local progress = counter/total;
module.loadProgress = progress;
Expand Down Expand Up @@ -894,8 +930,14 @@ function module:MakeWorld(folder, playerSize)


folder.DescendantAdded:Connect(function(instance)
if not instance:IsA("BasePart") then
return
end

self:ClearCache()
self:ProcessCollisionOnInstance(instance, playerSize)

updateOnChange(instance)
end)

folder.DescendantRemoving:Connect(function(instance)
Expand Down