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

How to get the position at a point on a part?

Asked by
nc2r 117
4 years ago
Edited 4 years ago

For example, suppose I have a 4 by 1 by 2 part. Position returns the center. Suppose I want to get the exact position of the upper north east corner. I might want to add the vector (2, 0.5, -1) to the part. However, if the part rotates, it will screw stuff up, how can I add a position relative to the part rather than relative to world space. How should I proceed?

I am doing this in the context of gun barrels in a tool. I want to create the bullet at the end of the barrel. However, simply adding the length of the barrel divided by two will only work if the gun happens to be facing forward.

I found something promising here but how do I apply it for this purpose?

I don't think that any of the answers solved the problem, that if the part is rotated, the part should still remain at the upper north east corner.

0
This is very interesting, can you elaborate? alivemaeonman 14 — 4y

4 answers

Log in to vote
3
Answered by
RayCurse 1518 Moderation Voter
4 years ago
Edited 4 years ago

Your question is basically asking how to convert a point from the coordinate frame of the barrel to the coordinate frame of the world. In particular, the vector you might be working with is part.Size / 2. CFrames, which stands for coordinate frame, are made for this purpose. Multiplying a vector with a CFrame converts that vector from the cframe's coordinate frame to the world coordinate frame.

local offset = part.Size / 2
local cornerPosition = part.CFrame * offset

There is also a function called PointToWorldSpace that makes the purpose of the calculation more clear. It is exactly equivalent to the line above.

local cornerPosition = part.CFrame:PointToWorldSpace(offset)

As you mentioned, taking into account the part's cframe is not necessary if the part is not rotated. When the part isn't rotated, the position is simply part.Position + offset. In fact, adding the offset to the part's position is analogous to converting coordinate frames as well if the coordinate frames couldn't be rotated (in general they can).

Ad
Log in to vote
4
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

In my example, I'm going to have two parts as a visual: a blue part that is representing our part we want to get the position of and a red part that represents our position we want to get. links are visuals of each step.

Picture of the two parts originalPart is our blue part positionPart is our red part

Anyway, you didn't specify which direction was north, so I'm going to say the front face of the blue part is north.

We first want to put the red part at the cframe of the blue part so they have the same rotation:

positionPart.CFrame = originalPart.CFrame

We first want to shift the red part forward originalPart.Size.Z/2 studs on the z axis:

positionPart.CFrame = positionPart.CFrame * CFrame.new(0,0,-originalPart.Size.Z/2)

Then we'd want to shift the part to the right originalPart.Size.X/2 studs in the x-axis:

positionPart.CFrame = positionPart.CFrame * CFrame.new(originalPart.Size.X/2,0,0)

and then lastly, shift the red part up originalPart.Size.Y/2 studs in the y-axis

positionPart.CFrame = positionPart.CFrame * CFrame.new(0,originalPart.Size.Y/2,0)

You end up with the red part in the north east corner

You can do this all in one step by saying:

positionPart.CFrame = originalPart.CFrame * CFrame.new(originalPart.Size.X/2,originalPart.Size.Y/2,-originalPart.Size.Z/2)

Full Code:

local originalPart = Instance.new("Part")
originalPart.CFrame = CFrame.Angles(math.rad(20),0,math.rad(5))  --rotating the part to show it works when originalPart is rotated

originalPart.Parent = workspace
originalPart.Color = Color3.fromRGB(0,0,255)

local positionPart = Instance.new("Part")
positionPart.Parent = workspace
positionPart.Color = Color3.fromRGB(255,0,0)
positionPart.Size = Vector3.new(.5,.5,.5)

positionPart.CFrame = originalPart.CFrame * CFrame.new(originalPart.Size.X/2,originalPart.Size.Y/2,-originalPart.Size.Z/2)
0
Good, but you are not taking into account if the part rotates to an arbitrary orientation nc2r 117 — 4y
0
wym? this will move relative to the parts position/rotation? royaltoe 5144 — 4y
0
I forgot to set the red part to have the cframe of blue part royaltoe 5144 — 4y
0
fixed it. should work with rotation now. sorry about that. royaltoe 5144 — 4y
View all comments (5 more)
0
Thanks, but the question was explicitly about rotation nc2r 117 — 4y
0
Did I answer the wrong question? I thought you meant that you wanted to put the part in the corner of another part regardless of rotation. That's what the code above does. If you were asking something different please let me know. royaltoe 5144 — 4y
0
Suppose I want to set the position of a part relative to another part and it's rotation. Your recent edit solved my problem so ty nc2r 117 — 4y
0
yeah mb, glad you got it working royaltoe 5144 — 4y
0
why the heck did you accept the other dudes answer greatneil80 2647 — 4y
Log in to vote
0
Answered by 4 years ago

99% Sure this is a troll. It's really easy just do

 print(workspace.part.Position)
workspace.part1.Position = workspace.part2.Position

Try harder and look at the wiki next time.

0
idk who downvoted you, but not what he's asking royaltoe 5144 — 4y
0
This isn't what he's asking. He's asking about the exact position ON the part. So the Top Right Corner of the part, not the position of the part itself, the position ON the part. killerbrenden 1537 — 4y
0
This does not even remotely answer my question sorry nc2r 117 — 4y
Log in to vote
0
Answered by
Dfzoz 489 Moderation Voter
4 years ago

You can use lookVector, rightvector and upvectors from the part cframe and multiply it by the part size/2. You can also just get part.position+(part.Size/2) for that specific point i think.

Answer this question