Welds: Difference between revisions
>NXTBoy |
>Sduke524 I tried why this worked so it wasn't so much of giving out random code. |
||
Line 80: | Line 80: | ||
== Welding together two existing bricks == | == Welding together two existing bricks == | ||
Sometimes, you want to just weld together two parts in their current positions, so that they remain in the same relative positions. This | Sometimes, you want to just weld together two parts in their current positions, so that they remain in the same relative positions. This takes just a basic understanding of CFrame math and welds. As previously mentioned, | ||
{{lua|=Part1.CFrame == Part0.CFrame * C0 * C1:inverse()}} | |||
So lets take this and apply some basic algebra | |||
{{lua|= | |||
Part1.CFrame * C1 == Part0.CFrame * C0 | |||
C1 == Part0.CFrame:inverse() * Part1.CFrame * C0 | |||
}} | |||
So this means we can do : | |||
{{lua|= | {{lua|= | ||
function weld(a, b) | function weld(a, b) | ||
Line 86: | Line 95: | ||
weld.Part0 = a | weld.Part0 = a | ||
weld.Part1 = b | weld.Part1 = b | ||
weld.C0 = CFrame.new() | weld.C0 = CFrame.new() --identity so it doesn't effect the math | ||
weld.C1 = a.CFrame * b.CFrame:inverse() | weld.C1 = a.CFrame * b.CFrame:inverse() | ||
return weld; | return weld; |
Revision as of 03:10, 28 February 2012
Introduction
No, these are not the kind of weld where you take the weld tool and put two bricks together. This is serious welding, used for multi-handled weapons (such as stravant's NXT Gen Weapons), and much, much more. Now, here's a bit on welds:
Prerequisite: Know how to use a basic cframe (CFrame.new(#, #, #)), and how to index and change the properties of an object, using a script.
What are welds?
They are an object, that hold their parent part, and another part, in a position relative to each other. For example, hold one part 2 units to the right of the other brick. They can be used in places where parts need to be held together at odd angles, but still be able to move, such as for a vehicle.
Basic structure of a weld
The weld object is placed inside of a part, and a property is set to determine another part which should be welded to the original part. Then two CFrames, the C0 and the C1, tell the weld how the parts should be placed.
Part0: This property of a weld is an Object, it must always be set to the part the weld is in for the weld to work
Part1: This property of a weld is an object and tells is which part it should be attaching its parent to
C0: the C0 of a weld, determines how the [offset point] should be attached to the Part0
C1: the C1 of a weld, determines how the Part1 should be attached to the [offset point]
Advanced
weld.Part1.CFrame == weld.Part0.CFrame * weld.C0 * weld.C1:inverse()
Setting the values
Figuring out what to set the C0 and C1 to is a bit finicky, but one you get good at it, it can go quite quickly. For Welds, you dont have to worry about the C1, it's automaticaly set to a "unit" or unrotated CFrame, you only have to deal with C1 when working with motors, so ignore it for now. The C0 will tell the weld how it should attatch itself to the other part, eg.
[weld].C0 = CFrame.new(0, 2, 0)
This tells the weld that it should hold the part1 in a position 2 studs above the part0, simple eh?
Rotating the CFrame
To rotate the CFrame you must use the following command, which is case sensitive. CFrame.fromEulerAnglesXYZ(#, #, #)
This is used as follows: [weld].C0 = CFrame.new(0, 2, 0)*CFrame.fromEulerAnglesXYZ(0, math.pi, 0)
This tells the weld to attach the part1, two studs above the part0 AND rotate the part1 by 180 degrees relative to the part0
You use math.pi, as the number to rotate by, use it in the following fashion :
- math.pi = 1/2 of a turn
- math.pi/2 = 1/4 of a turn
- math.pi/4 = 1/8 of a turn
Putting it all together
Now that you know how to use the basics of a weld, here's how you put it all together. Here's an example:
Lets say that this is in a script, in a vehicle:
pln = script.Parent local w = Instance.new("Weld") w.Parent = pln.Engine w.Part0 = pln.Engine --> part0 has to be the parent w.Part1 = pln.Wing1 w.C0 = CFrame.new(0, 0, 6)*CFrame.new(0, -math.pi/5, 0)
Walkthrough
- Open Roblox Studio
- Insert > Object > Part
- Rename Part "Engine"
- Insert > Object > Part
- Rename Part "Wing1"
- Group Engine and Wing1 together as "pln"
- Click "pln"
- Insert > Object > Script into "pln"
- Copy and paste the script in "Putting it all together" above.
- Test... Engine should have a "weld" logo in the Explorer menu.
Welding together two existing bricks
Sometimes, you want to just weld together two parts in their current positions, so that they remain in the same relative positions. This takes just a basic understanding of CFrame math and welds. As previously mentioned,
Part1.CFrame == Part0.CFrame * C0 * C1:inverse()
So lets take this and apply some basic algebra
Part1.CFrame * C1 == Part0.CFrame * C0
C1 == Part0.CFrame:inverse() * Part1.CFrame * C0
So this means we can do :
function weld(a, b)
local weld = Instance.new('Weld')
weld.Part0 = a
weld.Part1 = b
weld.C0 = CFrame.new() --identity so it doesn't effect the math
weld.C1 = a.CFrame * b.CFrame:inverse()
return weld;
end
You can then do the following to weld two parts together:
weld(game.Workspace.Part, game.workspace.Brick).Parent = game.Workspace