Game Development Community

dev|Pro Game Development Curriculum

Snap to Objects TGEA 1.7/1.8

by J.C. Smith · 03/16/2009 (2:57 am) · 3 comments

Four years ago Davis Ray Sickman, Jr. wrote this resource:

http://www.garagegames.com/community/resource/view/5880

which added snap to functionality to the mission editor. This made it easier to match up things like roads or dungeon pieces and have them line up properly. This code did not work with recent versions of TGE or TGEA due to the tools changes. This is just a simple resource that gets it working with more recent versions of Torque. This should work fine for TGEA 1.7+, and may also work for TGE but I haven't looked at the code for TGE in a long while or tested it.

First edit your tools/missioneditor/scripts/menu.ed.cs file and look for the following code block:

// World Menu
   if(! isObject(%this.worldMenu))
   {
      %this.dropTypeMenu = new PopupMenu()
      {
         superClass = "MenuBuilder";
         class = "EditorDropTypeMenu";

         // The onSelectItem() callback for this menu re-purposes the command field
         // as the MenuBuilder version is not used.
         item[0] = "Drop at Origin" TAB "" TAB "atOrigin";
         item[1] = "Drop at Camera" TAB "" TAB "atCamera";
         item[2] = "Drop at Camera w/Rot" TAB "" TAB "atCameraRot";
         item[3] = "Drop below Camera" TAB "" TAB "belowCamera";
         item[4] = "Drop at Screen Center" TAB "" TAB "screenCenter";
         item[5] = "Drop at Centroid" TAB "" TAB "atCentroid";
         item[6] = "Drop to Ground" TAB "" TAB "toGround";
         
      };

Underneath it add this block of code...

%this.snapToMenu = new PopupMenu()
      {
         superClass = "MenuBuilder";
         class = "EditorSnapToMenu";

         // The onSelectItem() callback for this menu re-purposes the command field
         // as the MenuBuilder version is not used.
         item[0] = "Snap To X" TAB "" TAB "X";
         item[1] = "Snap To X+" TAB "" TAB "X+";
         item[2] = "Snap To X-" TAB "" TAB "X-";
         item[3] = "Snap To Y" TAB "" TAB "Y";
         item[4] = "Snap to Y+" TAB "" TAB "Y+";
         item[5] = "Snap to Y-" TAB "" TAB "Y-";
         item[6] = "Snap To Z" TAB "" TAB "Z";
         item[7] = "Snap to Z+" TAB "" TAB "Z+";
         item[8] = "Snap to Z-" TAB "" TAB "Z-";
         
      };

Later on in the same file find this line:

item[18] = "Drop Location" TAB %this.dropTypeMenu;

And underneath it add:

item[19] = "-"; 
         item[20] = "Snap To" TAB %this.snapToMenu;

Now open up the tools/missioneditor/scripts/menuhandler.ed.cs script and find this block of code:

function EditorDropTypeMenu::setupDefaultState(%this)
{
   // CodeReview - What the heck does this do?  Comment it.
   %this.onSelectItem(4, getField(%this.item[4], 0));
   Parent::setupDefaultState(%this);
}

Underneath it add the following...

function EditorSnapToMenu::onSelectItem(%this, %id, %text)
{ 
	EWorldEditor.SnapTo(%id);  
}

function EditorSnapToMenu::setupDefaultState(%this)
{
   Parent::setupDefaultState(%this);
}
function WorldEditor::snapTo(%this, %id)
{
   if(%this.getSelectionSize() > 2)
   {
       error("Please select two objects before selecting a Snap To funciton.");
       return;
   }
    %objTarget = %this.getSelectedObject(0);
    %objToSnap = %this.getSelectedObject(%this.getSelectionSize()-1);
    switch$(%id)
    {
        case 0:
            %objToSnap.setTransform(setWord(%objToSnap.getTransform(), 0, getWord(%objTarget.getTransform(), 0)));
        case 1:
            %objTargetXEdge = getWord(%objTarget.getTransform(), 0) + getWord(%objTarget.getObjectBox(), 0);
            %objToSnapXEdge = %objTargetXEdge - getWord(%objToSnap.getObjectBox(), 3);
            %objToSnap.setTransform(setWord(%objToSnap.getTransform(), 0, %objToSnapXEdge));
        case 2:
            %objTargetXEdge = getWord(%objTarget.getTransform(), 0) + getWord(%objTarget.getObjectBox(), 3);
            %objToSnapXEdge = %objTargetXEdge - getWord(%objToSnap.getObjectBox(), 0);
            %objToSnap.setTransform(setWord(%objToSnap.getTransform(), 0, %objToSnapXEdge));
        case 3:
            %objToSnap.setTransform(setWord(%objToSnap.getTransform(), 1, getWord(%objTarget.getTransform(), 1)));
        case 4:
            %objTargetXEdge = getWord(%objTarget.getTransform(), 1) + getWord(%objTarget.getObjectBox(), 1);
            %objToSnapXEdge = %objTargetXEdge - getWord(%objToSnap.getObjectBox(), 4);
            %objToSnap.setTransform(setWord(%objToSnap.getTransform(), 1, %objToSnapXEdge));
        case 5:
            %objTargetXEdge = getWord(%objTarget.getTransform(), 1) + getWord(%objTarget.getObjectBox(), 4);
            %objToSnapXEdge = %objTargetXEdge - getWord(%objToSnap.getObjectBox(), 1);
            %objToSnap.setTransform(setWord(%objToSnap.getTransform(), 1, %objToSnapXEdge));
        case 6:
            %objToSnap.setTransform(setWord(%objToSnap.getTransform(), 2, getWord(%objTarget.getTransform(), 2)));
        case 7:
            %objTargetXEdge = getWord(%objTarget.getTransform(), 2) + getWord(%objTarget.getObjectBox(), 2);
            %objToSnapXEdge = %objTargetXEdge - getWord(%objToSnap.getObjectBox(), 5);
            %objToSnap.setTransform(setWord(%objToSnap.getTransform(), 2, %objToSnapXEdge));
        case 8:
            %objTargetXEdge = getWord(%objTarget.getTransform(), 2) + getWord(%objTarget.getObjectBox(), 5);
            %objToSnapXEdge = %objTargetXEdge - getWord(%objToSnap.getObjectBox(), 2);
            %objToSnap.setTransform(setWord(%objToSnap.getTransform(), 2, %objToSnapXEdge));
    }
}

Your editors world menu will now have a SnapTo menu. To use this you should select two objects, these should be rotated in teh same direction, you don't want one object rotated at 90 degrees and the other at 87 for example. You then use the SnapTo menu and select how to align them.

Once you've fiddle with the alignments you should see how it works. But to give a quick example, if you wanted two objects to have the same Y and Z orientation but to be lined up side by side on the X access you would SnapTo Y, SnapToZ and then SnapToX+ (or SnapToX-). They will be lined up based on their bounding box.

#1
03/16/2009 (3:14 am)
wow thats one resource i can use when i finish all my walls it will save me some time on lining them all up to each other. :P i just hope it centers them as well as theres also a tower thats part of the walls that will need snapped together.
#2
03/18/2009 (6:50 pm)
Do you know if this will work in TGE 1.5.2 or not? or what changes would need to be made to get it to work in TGE? Thanks, this sounds like an awesome resource.
#3
08/18/2009 (7:51 pm)
I remember reading this resource about 3 months ago. Does this work with T3D? This should be standard with it IMO.