Lining up two objects together in mission editor
by Keith Johnston · in Artist Corner · 03/11/2003 (8:47 am) · 8 replies
Is there an easy way in the mission editor to line up two objects so that they are right next to each other (sides touching). If I was building a wall, for example, and I wanted all the pieces to be separate shapes (DTS objects)?
#2
03/11/2003 (10:46 am)
Best I've ever been able to do is by hand editing the Position and Rotation values in the mission editor.
#3
Save this as 'editorSnap.cs' in the common/editor dir:
Add this to editor.cs after the 'exec(EditorGui.cs') line:
Add this in EditorGui.cs at the end of the function EditorGui::init.
And add this in the same file at the end of the function EditorMenuBar::onMenuItemSelect:
03/11/2003 (2:06 pm)
OK, I wrote a small snap menu that will help with this:Save this as 'editorSnap.cs' in the common/editor dir:
function snapInit() {
EditorMenuBar.addMenu("Snap", 1);
EditorMenuBar.addMenuItem("Snap", "Align Top", 1, "");
EditorMenuBar.addMenuItem("Snap", "Align Bottom", 2, "");
EditorMenuBar.addMenuItem("Snap", "Align Y", 3, "");
EditorMenuBar.addMenuItem("Snap", "Align X", 4, "");
}
function snapMenu(%menuId, %item) {
switch$(%item)
{
case "Align Top":
SnapAlignTops();
case "Align Bottom":
SnapAlignBottoms();
case "Align Y":
SnapAlignY();
case "Align X":
SnapAlignX();
}
}
function SnapAlignTops()
{
if (EWorldEditor.getSelectionSize()>1) {
SnapAlign2(5,0);
}
}
function SnapAlignBottoms()
{
if (EWorldEditor.getSelectionSize()>1) {
SnapAlign2(2,1);
}
}
function SnapAlignX()
{
if (EWorldEditor.getSelectionSize()>1) {
%firstObj = EWorldEditor.getSelectedObject(0);
%forward = %firstObj.getForwardVector();
%up = "0 0 1";
%vec = vectorCross(%forward,%up);
SnapAlign(%vec,0,3);
}
}
function SnapAlignY()
{
if (EWorldEditor.getSelectionSize()>1) {
%firstObj = EWorldEditor.getSelectedObject(0);
%vec = %firstObj.getForwardVector();
SnapAlign(%vec,1,4);
}
}
function SnapAlign(%vector,%boxmin,%boxmax)
{
%count = EWorldEditor.getSelectionSize();
if (%count > 1) {
%firstObj = EWorldEditor.getSelectedObject(0);
%lastTrans = %firstObj.getTransform();
%firstBox = %firstObj.getObjectBox();
%min = getWord(%firstBox,%boxmin);
%max = getWord(%firstBox,%boxmax);
%lastHalf = (%max-%min)/2;
for (%i=1;%i<%count;%i++) {
%obj = EWorldEditor.getSelectedObject(%i);
// Line this object up with the last one
%curTrans = %lastTrans;
// Extract the position
%curPos = getWords(%lastTrans, 0, 2);
// Get half the length of this object along the Y axis
%curBox = %obj.getObjectBox();
%min = getWord(%curBox,%boxmin);
%max = getWord(%curBox,%boxmax);
%curHalf = (%max-%min)/2;
// Add our half to half of the last one
%dist = %lastHalf + %curHalf;
%newVec = vectorScale(%vector, %dist);
%newPos = VectorAdd(%curPos,%newVec);
// Update the position
%curTrans = setWord(%curTrans, 0, getWord(%newPos, 0));
%curTrans = setWord(%curTrans, 1, getWord(%newPos, 1));
%curTrans = setWord(%curTrans, 2, getWord(%newPos, 2));
// Set the transform
%obj.setTransform(%curTrans);
// This becomes the new 'last' object
%lastHalf = %curHalf;
%lastTrans = %curTrans;
}
}
}
function SnapAlign2(%zindex,%bottom)
{
%count = EWorldEditor.getSelectionSize();
if (%count > 1) {
%firstObj = EWorldEditor.getSelectedObject(0);
%firstBox = %firstObj.getWorldBox();
%min = getWord(%firstBox,2);
%max = getWord(%firstBox,5);
%firstHalf = (%max-%min)/2;
%firstZ = %min+%firstHalf;
for (%i=1;%i<%count;%i++) {
%obj = EWorldEditor.getSelectedObject(%i);
// Extract the position
%curPos = getWords(%obj.getTransform(), 0, 2);
// Get half the height
%curBox = %obj.getObjectBox();
%min = getWord(%curBox,2);
%max = getWord(%curBox,5);
%half = (%max-%min)/2;
if (%bottom)
%offset = %half-%firstHalf;
else
%offset = %firstHalf-%half;
// Set our z to the first objs z plus offset
echo("firstZ=" @ %firstZ);
echo("firstHalf=" @ %firstHalf);
echo("half=" @ %half);
%newZ = %firstZ+%offset;
echo("newZ=" @ %newZ);
// Update the position
%curTrans = %obj.getTransform();
%curTrans = setWord(%curTrans, 2, %newZ);
// Set the transform
%obj.setTransform(%curTrans);
}
}
}Add this to editor.cs after the 'exec(EditorGui.cs') line:
exec("./editorSnap.cs");Add this in EditorGui.cs at the end of the function EditorGui::init.
snapInit();
And add this in the same file at the end of the function EditorMenuBar::onMenuItemSelect:
case "Snap": snapMenu(%itemId, %item);
#4
This is simply awesome :) It will save me a lot of time placing my race tracks. Thanks a million !
Gilles Jr Lafrance
Lafbros
03/11/2003 (10:02 pm)
Keith,This is simply awesome :) It will save me a lot of time placing my race tracks. Thanks a million !
Gilles Jr Lafrance
Lafbros
#5
Excellent resource! Thanks for sharing it!
One question... I tried to get it to work in the editor, selecting an object, placing it near another object, and then selecting one of the snap options from the menu.. However, nothing happens.. the objects stay as they are and I'm not noticing any "snapping" when moving the objects...
Is there some trick to using this correctly?
Thanks!
Mike
04/16/2004 (3:00 pm)
Keith,Excellent resource! Thanks for sharing it!
One question... I tried to get it to work in the editor, selecting an object, placing it near another object, and then selecting one of the snap options from the menu.. However, nothing happens.. the objects stay as they are and I'm not noticing any "snapping" when moving the objects...
Is there some trick to using this correctly?
Thanks!
Mike
#6
Nice work, by the way. I really like this feature. :)
04/16/2004 (5:04 pm)
You have to select both objects, then choose snap.Nice work, by the way. I really like this feature. :)
#7
This is a great resource. Thanks for posting it. I have a couple of questions, however, which I was wondering if you would mind thinking about. First, when I select two object, and then snap them together, it seems like it takes quite a while for the engine to respond. I'm not quite sure why this would be a time consuming operation. Second, the objects wind up unselected at the end. This is probably someting I should just tweek in my version of the code, but it seems like defaulting to the objects still being selected would be more useful.
Thanks again,
04/19/2004 (8:32 am)
Keith,This is a great resource. Thanks for posting it. I have a couple of questions, however, which I was wondering if you would mind thinking about. First, when I select two object, and then snap them together, it seems like it takes quite a while for the engine to respond. I'm not quite sure why this would be a time consuming operation. Second, the objects wind up unselected at the end. This is probably someting I should just tweek in my version of the code, but it seems like defaulting to the objects still being selected would be more useful.
Thanks again,
#8
The only thing you need to do to use this in 1.5.2 is use the creator/editor directory instead of common/editor.
02/12/2008 (4:14 pm)
Thanks a lot for this. I just added it to 1.5.2 and it works great.The only thing you need to do to use this in 1.5.2 is use the creator/editor directory instead of common/editor.
Torque Owner Matthew Jones
Matt