Hi how can i make asteroids like nds?

Hi im trying to make an nds clone for practice, and im having trouble with making meteors, it sounds easy at first, but

  1. im having trouble with collision detection when the asteroid hits the ground, what would be the most effective way of detecting collision for high speed objects (im currently using .touched, i have tried raycast but it doesnt seem to work that well either).
  2. the way my system works is that it clones a premade asteroid but it causes a lot of lag spiked and im not sure if thats causing it, here is the script:
local module = {}

local Meteor = script.Parent.meteor

function OnTouched(hit, meteorClone)
	meteorClone:Destroy()
	print("Destroyed")
end

function module.Init()
	print("Disaster initialized")	
end

function module.Main(Duration: number)
	
	local MeteorNumber = 60
	
	while MeteorNumber > 0 do
		MeteorNumber -= 1
		local meteorClone = Meteor:Clone()
		meteorClone.AsteroidScript.Enabled = true
		meteorClone.Parent = workspace
		task.wait(math.random(1,3))
		
		local Connection
		
		Connection = meteorClone.Touched:Connect(function(hit)
			OnTouched(hit, meteorClone)
			Connection:Disconnect()
		end)
		
	end

end

return module


You should recycle your asteroids instead of creating new ones repeatedly.

how can i do that///////////////////////////////////

Make the code generate the required number of asteroids. Then, when an asteroid is touched, just set its position back instead of deleting it, basically recycling it.

I implemented and it still kind of lags at the start

The method he is referring to is something called Object Pooling which is a technique people use to optimize new instances being added to the place rather than creating in the moment which draws more memory, I recommend doing the pooling when the instance is initializing and adding/removing from the pool based on the server size / other variables that you might see.

If you have any questions I’d be glad to answer, thank you!

Hey both of you thanks, i made this how can i improve it:

Object Pooling Module

local module = {}

local ReplicatedStorage = game:GetService("ReplicatedStorage")

function module:Init()
	return true
end

function module:initPool(template: Part , count: number)
	
	local PooledObjects = {}
	
	for i = 1, count do
		local clone = template:Clone()
		table.insert(PooledObjects, clone)
		clone.Parent = ReplicatedStorage
	end
	
	return PooledObjects
	
end

function module:RequestObject(Pool: SharedTable , template: Part)

	local Object : Part
	 
	if #Pool > 0 then
		Object = table.remove(Pool, #Pool) 	
	else
		local clone = template:Clone()
		table.insert(Pool, template)
		return
	end
	
	Object.Parent = workspace

	if Object:FindFirstChildOfClass("Script") then
		Object:FindFirstChildOfClass("Script").Enabled = true	
	end
	
	
	return Object 

end

function module:ReturnObject(Pool: SharedTable , Object: Part)
	
	table.insert(Pool, Object)
	
	if Object:FindFirstChildOfClass("Script") then
		Object:FindFirstChildOfClass("Script").Enabled = false	
	end
	
	Object.Parent = ReplicatedStorage
	
end

function module:DestroyPool(Pool : SharedTable)
	
	for i = 1, #Pool do
		Pool[i]:Destroy()
	end
	
	Pool = nil
	
end

return module

MeteorDisaster Module Script

local module = {}

local ModuleLoader = require(game:GetService("ReplicatedStorage").MyServices.ModuleLoader)
local ObjectPoolModule = ModuleLoader:GetServices("ObjectPoolModule")

local Pool = nil
local Template = script.Parent.meteor

Continue = true


function OnTouched(hit : Part , Pool, Meteor)
end


function module.Init()
	print("Disaster Initialized")
	Pool = ObjectPoolModule:initPool(Template, 8)	
end

function module.Main()
		
	while Continue do
		
		local Meteor : Part = ObjectPoolModule:RequestObject(Pool, Template)
		
		print(Pool)
		
		task.delay(2,function()
			ObjectPoolModule:ReturnObject(Pool, Meteor)
		end)
		
		task.wait(math.random(20,100)/100)
		
	end

end

function module.Stop()
	Continue = false
	task.wait(2)
	ObjectPoolModule:DestroyPool(Pool)
	Continue = true
end

return module

im still figuring out the collision detection

quick question, would GetPartsInRadius() locally be a good idea for the meteor collision detection?

Hello! You can use something called Octrees for your 3d Spatial querying of asteroid collision which I have already discussed about in this forum but if you want my quote then here it is

If the space is large enough, you could just use Octrees or Quadtrees in order to Spatially Query for items, there’s a nice Octree API already on the platform. If you’re interested or have any questions let me know!

Thank you.