Efficient serverside hitbox?

I’m trying to make a “tower defense”-ish type game, and I wanna make a “trap”.

⁽ᵐᵒᵈᵉˡ ᶦˢ ᶠᵒʳ ᵗᵉˢᵗᶦⁿᵍ ᵖᵘʳᵖᵒˢᵉˢ⁾

It should activate when an enemy steps on it, but here’s my issue:

How would I make the hitbox for the trap?

I don’t wanna use a hitbox community module, I wanna make my own.

I thought about having a runservice connection and constantly checking closest entity/spatial queries, but isn’t that gonna cause massive lag considering there might be like 30 traps?

Alternatively I thought about just using .Touched but everyone says it’s unreliable.

help

2 Likes

You’ll just have to use spatial queries. If your enemies aren’t that fast you don’t need to do it every heartbeat. Alternatively, if this trap is placed on the path, you could do something like saving what percent of the length of the track the trap is at, and have the first enemy to get that far in the track get trapped.

3 Likes

Hitbox detection is a really common problem, so there are lots of solutions

I think its called spatial hashing where you break up the map into cells, categorize entities into the nearest cell, and only check entities that are in nearby cells

For higher numbers the most efficient method is using quadtrees

edit: if you use roblox’s spatial query which probably already does this then you should put all enemies into one folder and make an overlapparam that only includes those enemies

1 Like

@BubbleNinjaisnoob Smart, problem is some traps might just sit on walls, and you could fling enemies into them, if the loop isn’t fast enough it might miss them. I get the path idea too but It’s not that type of tower defense, It’s not really a “tower defense” in the typical way I guess.


@happya_x That sounds super good but I’m not sure I’m smart enough to make that, it sounds so incredibly complicated. Putting entities in folders with overlap params sounds really good but will that do enough when there might be like 30 traps using spatial queries every frame?

I’m starting to think maybe Roblox just isn’t the right engine to make my idea into a game :frowning:

Probably not, maybe you can use SimpleZone?

1 Like

I did think about SimpleZone or other hitbox modules but:

I don’t wanna use a hitbox community module, I wanna make my own.

I might just have to use spatial queries and just benchmark how much it can handle.

1 Like

just 30 traps should have no lag even with an inefficient method

also if the enemies are moving in a path, then you can represent their position in just one dimension with a number that represents how far along they are along the path

and then hitbox detection is much faster because you can just compare if the progress along the path is between two numbers which represent the edges of the trap on the path

I’ll try just spatial queries in that case.


(The path progress method wont work)

I get the path idea too but It’s not that type of tower defense, It’s not really a “tower defense” in the typical way I guess.

There’s no paths, It’s not a tower defense in the typical way, I just called it a “tower defense” because… you place traps and turrets, and they defend you xd.

You don’t need spatial queries, just a rapid magnitude check for something this simple.

I imagine it’s more performant than spatial queries so I might use it for simple traps, but what about more complex hitboxes, like this for example

Magnitude is like, a “circle”, right, so I’m not sure how to incorporate it in that without looking hella fake.

If there’s really a lot of these traps, you could use Hitbox.CFrame * Target.CFrame:Inverse() to get the relative offset of the target from the hitbox, which accounts for rotation of the hitbox itself.

From there, you just need to see if any position axis of the offset is lower than half of the hitbox’s corresponding size axis. If the target is 3 studs away in the X-axis relative to the hitbox, and the hitbox’s size is 6, then they are within the bounds of the hitbox.

The downside compared to a spatial query is that this only accounts for the very center of the target.

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