Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Keep track of space on a grid? Object Collision using Vector3?

Asked by
legosweat 334 Moderation Voter
8 years ago

Keep track of space on a grid?: On a grid of 5x5 blocks, I'm moving objects which take up some of the space on the grid, now if I were to for say, add another object, how would I be able to know if there is enough space for it on the grid?

Picture Example: Example 1

Based off of that, how would I calculate whether there is enough space for another object on the grid

Hopefully someone can get me a start on this, because I have no idea where to start.

Object Collision?: Since I'm moving my objects using CFrame, I'm not able to tell whether the object is in another object, I know you can get touching parts, but I'd like the object not to collide with the other objects on the grid.

If used a Vector3 instead of CFrame, and kept the y-axis the same every time I move it, will that make it not able to go over the object and not collide into it?, since Vector3 has its own built in collision detection, and since the y-axis can't change, it won't go over the object either??

For example if there's a rock on the grid, it will not go over it nor collide into it, will Vector3 be able to do that if you keep the y-axis the same every time, or what?

Instead of that, I could use :GetTouchingParts() but I would like to see what I can do.

I have no example code sadly, I deleted what I've already tried, but I just need some feedback and information to get me a good start on this.

1 answer

Log in to vote
1
Answered by 8 years ago

If your content is grid aligned, why not track it yourself?

The issue I find is that people often look to Roblox APIs to solve their issues. Manipulating things in 3D space using Roblox's CFrames and Vector3s is one of the more common reasons I've seen for people to drop reason and start looking for solutions through Roblox.

That aside, the way to do it is to just track it. Imagine your grid as a 2D array, and it gets much more simple.

local gridlet = {{},{},{},{},{}};

That's your little 5x5 array. I know, it doesn't look 5x5, but those are constraints that your code imposes through hard coding limits - It has to be done sometimes. Say you have a House that takes up the top 2x3 of the grid, from the top left. Now your array looks a bit like

local gridlet = {
{EntityHouse, EntityHouse},
{EntityHouse, EntityHouse},
{EntityHouse, EntityHouse},
{},
{}
};

Which of course means that your grid knows all of the range from [1][1] to [2][3] is taken. That's great news for you now, because it means that if you then want to place a 2x2 rock at [4][4], you can check to see that you can.

local open = true;
for x=4,5 do -- 2x from 4
  for y=4,5 do -- 2y from 4
    if gridlet[x][y] then open = false; break; end; -- Whoops, we're over something here.
  end;
end;
if open then
  -- Place it.
end

And of course, those values can be substituted at run through variables, and even put into a function for an API.

Ad

Answer this question