Welds: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>ENET
No edit summary
>JulienDethurens
Replaced "lets" by "let's".
 
(13 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Map|Main Page|Tutorials}}
{{Map|Main Page|Tutorials}}


== Introduction ==
==Prerequisites==
 
*Being able to use a basic CFrame (CFrame.new(#, #, #)).
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 [http://www.roblox.com/User.aspx?id=80119 stravant's] NXT Gen Weapons), and much, much more. Now, here's a bit on welds:
*Knowing how to index and change the properties of an object, using a script.
 
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? ==
== What are welds? ==
Line 21: Line 19:
C0: the C0 of a weld, determines how the [offset point] should be attached to the Part0  
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 [offest point]
C1: the C1 of a weld, determines how the Part1 should be attached to the [offset point]


=== Advanced ===
=== Advanced ===
weld.Part1.CFrame == weld.Part0.CFrame * weld.C0 * weld.C1:inverse()
{{lua|=Part1.CFrame * C1 == Part0.CFrame * C0}}


== Setting the values ==
== Setting the values ==
Line 55: Line 53:
Now that you know how to use the basics of a weld, here's how you put it all together. Here's an example:
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:  
Let's say that this is in a script, in a vehicle:  
<pre>
<pre>
pln = script.Parent  
pln = script.Parent  
Line 79: Line 77:
* Test... Engine should have a "weld" logo in the [[Explorer]] menu.
* Test... Engine should have a "weld" logo in the [[Explorer]] menu.


== Relative Position Welding ==
== Welding together two existing bricks ==
This takes two parts and creates a weld between them. It uses their distance from each other relatively.
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,
<br>
{{lua|=Part1.CFrame * C1 == Part0.CFrame * C0}}
<pre>function weld(x, y)
 
  local weld, cj;
So let's take this and apply some basic algebra
  weld = Instance.new('Weld');
 
  cj = CFrame.new(x.Position);
{{lua|
  weld.Part0 = x;
                         Part1.CFrame * C1 {{==}} Part0.CFrame * C0
  weld.Part1 = y;
Part1.CFrame:inverse() * Part1.CFrame * C1 {{==}} Part1.CFrame:inverse() * Part0.CFrame * C0
  weld.Parent = weld.Part0;
                                        C1 {{==}} Part1.CFrame:inverse() * Part0.CFrame * C0
  weld.C0 = x.CFrame:inverse()*cj; --get the inverse of x and times it by cj
}}
  weld.C1 = y.CFrame:inverse()*cj; --get the inverse of y and times it by cj
So this means we can do :
  return weld;
{{lua|=
end</pre>
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 = b.CFrame:inverse() * a.CFrame
    return weld;
end}}
You can then do the following to weld two parts together:
{{lua|=
weld(game.Workspace.Part, game.workspace.Brick).Parent = game.Workspace
}}


== See Also ==
== See Also ==
* [[Weld]]
* [[Weld]]
* [[CFrame]]
* [[CFrame]]
* [[Joints]]


[[Category:Scripting Tutorials]]
[[Category:Scripting Tutorials]]

Latest revision as of 23:17, 16 March 2012

Prerequisites

  • Being able to use a basic CFrame (CFrame.new(#, #, #)).
  • Knowing 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

Part1.CFrame * C1 == Part0.CFrame * C0

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:

Let's 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 * C1 == Part0.CFrame * C0

So let's take this and apply some basic algebra

                         Part1.CFrame * C1 == Part0.CFrame * C0
Part1.CFrame:inverse() * Part1.CFrame * C1 == Part1.CFrame:inverse() * Part0.CFrame * C0
                                        C1 == Part1.CFrame:inverse() * Part0.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 = b.CFrame:inverse() * a.CFrame
    return weld;
end

You can then do the following to weld two parts together:

weld(game.Workspace.Part, game.workspace.Brick).Parent = game.Workspace

See Also