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

Set boundary for rotated objects?

Asked by 6 years ago
Edited 6 years ago

Sorry if the title didn't make much sense, it's kind of tough to explain what I am trying to achieve.

I am working on a game that requires a placement system. Everything works fine, except for the boundaries of the player's plot.

At first, I calculated this by taking the position of the plot, subtracting that by the size of the plot divided by two. Then, I subtracted from that with the size of the object the player is trying to place down and divided that by two.

If this is hard to picture in your head, here is basically everything I explained above:

maxX1 = (workspace.Tycoons.Tycoon1.Base.Position.X - workspace.Tycoons.Tycoon1.Base.Size.X / 2) + ghost.HitBox.Size.X / 2
            maxX2 = (workspace.Tycoons.Tycoon1.Base.Position.X + workspace.Tycoons.Tycoon1.Base.Size.X / 2) - ghost.HitBox.Size.X / 2
            maxY1 = (workspace.Tycoons.Tycoon1.Base.Position.Z - workspace.Tycoons.Tycoon1.Base.Size.Z / 2) + ghost.HitBox.Size.Z / 2
            maxY2 = (workspace.Tycoons.Tycoon1.Base.Position.Z + workspace.Tycoons.Tycoon1.Base.Size.Z / 2) - ghost.HitBox.Size.Z / 2

Probably not the best way to do it, but that's why I'm here in the first place :P!

While that works (sort of), if I ever had an object that was not in a perfect square shape, and I rotated it, then tried to place that object at the edge, it would think that it was not rotated.

Again, that's probably not explained very well so here is an image.

Here is an image of the object not rotated 90 degrees.

Here is also a GIF to better demonstrate.

As you can see, the object is being stuck two units from the edge. (One unit is 2x2 studs)

Now, here is the actual question: how do I fix this? What should I know, and what should I be doing?

If you need any more information, just ask in the comments.

0
This is a really good question! Unfortunately I'm not too much of a math wiz. I believe this relates to how the Size vector is *local* (it's relative to a point within the object itself) whereas you want to use it to find corners that are in the *global* space (in the world). Keep trying to get this answered User#18718 0 — 6y
0
Thanks! I'm not much of a math wiz either, but hopefully someone can answer. Operation_Meme 890 — 6y
0
I'm having a lot of difficulty understanding what you are trying to do? Could you try explaining it again another way? EgoMoose 802 — 6y
0
I tried to explain my best. I think it would be best if you could try it out for yourself. Join the game, hit E and place down any upgrader, try placing it at the edge of the plot and try rotating it all ways. https://www.roblox.com/games/1386550012/top-secret Operation_Meme 890 — 6y
View all comments (2 more)
0
It's currently closed to visitors? EgoMoose 802 — 6y
0
Oh, my friend did that. It should be open now. Operation_Meme 890 — 6y

1 answer

Log in to vote
1
Answered by
EgoMoose 802 Moderation Voter
6 years ago

So I'm still not entirely sure what you want, but I think this is the basic idea.

You want to have a grid placement system that properly accounts for size when you rotate your objects that are not perfectly square.

I suppose there are a few ways to do this, but probably the easiest way is just to flip the size x and z values depending on the rotation and then treat the placement like you normally do.

Here's a simple example where I do that:

local base = game.Workspace.Base;
local part = game.Workspace.Part;

local mouse = game.Players.LocalPlayer:GetMouse();
mouse.TargetFilter = part;

local rotation = 0;
game:GetService("UserInputService").InputBegan:connect(function(input, process)
    if (not process) then
        -- press r key to rotate
        if (input.KeyCode == Enum.KeyCode.R) then
            rotation = rotation + math.rad(90);
        end;
    end
end);

game:GetService("RunService").RenderStepped:connect(function(dt)
    if (mouse.Target == base) then
        -- rotate the x and z values
        local r = CFrame.Angles(0, rotation, 0) * part.Size;
        -- since it's size make sure components are positive
        local sx, sz = math.abs(r.x)*0.5, math.abs(r.z)*0.5;
        -- convert to object space for ease of calculation
        local p = base.CFrame:pointToObjectSpace(mouse.Hit.p); 
        local size2 = base.Size*0.5;

        -- check to make sure the selected point has enough space for the object to fit on the base
        if (math.abs(p.x) + sx < size2.x and math.abs(p.z) + sz < size2.z) then
            -- place the object
            part.CFrame = CFrame.new(base.CFrame:pointToWorldSpace(Vector3.new(p.x, p.y + size2.y, p.z))) * CFrame.Angles(0, rotation, 0);
        end;
    end;
end);
0
Thank you! This is what I was looking for. Operation_Meme 890 — 6y
Ad

Answer this question