Game Development Community

Looking to test out an algorithm

by Philip Gregory · in Torque 3D Professional · 02/22/2012 (11:28 pm) · 25 replies

I've thought up an interesting new algorithm I want to try out--the only problem is, I can't find where I need to start in the source or script files. Without previewing what I'm trying to do (I wanna use this for a learning experience and release it as a resource), I want to write a function for the object editor that will affect selected object(s) when used a hot key (that I also want to code in).

If you know where I need to look in the source or script files, I would greatly appreciate it!

Thanks!

About the author

Yes... Those are ribs. Current projects: Breaking out of the box in T2D, devoting efforts and talents to Middle School student tech projects (including basic Python programming!)

Page «Previous 1 2
#1
02/24/2012 (6:15 am)
Maybe I'd get more results by explaining my end goal.

The end goal of this is to use the hot keys "CRTL + Insert" to instantly copy the the selected object or objects in the direction the camera is facing (simple x, y, or z axis). When the object(s) or copied, they instantly "snap" together at the end of the original objects. This would make creating city scenes and the life of a breeze. (For example, being able to instantly have the buildings snap together, or trying my goal of extending the Burg Demo level quickly).

This idea come from a "3d Chat Program" I used to play around on in the late 90s called Active Worlds. We used 3d .raw objects to build towns and what not and could easily create streets, walls, floors, and more using this method.

Any help would be appreciated!
#2
02/24/2012 (7:16 am)
There should be an editor.bind.ed.cs file in each of the editor folders under the tools directory. That is where you would want to put in your hooks.
#3
02/24/2012 (4:38 pm)
Ca-ching. I'll take a look at it after the weekend (12 hour midnights suck). I'm sure using my Sherlock skills that I'll be able to come across something in there to help me.

Thanks Ryan!
#4
02/24/2012 (7:15 pm)
Thanks to Ryan, I've put a place holder in ./Tools/World Editor/Scripts/editor.bind.ed.cs @ line 63 for the function. I've commented out the place holder and algorithm I want. It's messy at best but here's what it looks like.

//K001 Steak's function Holder
//This function will add CRTL + Insert keys to perform object duplication snapping.
//function objectDuplicationSnapping ( %val )
//{
   //Get the selected object's
   //(Insert code here)
   //Register the axis the camera is facing the most
   // X + or -, Y + or -, or Z + or -
   //(Insert code here)
   //Duplicate the object
   //(Insert code here)
   //Get the original objects size
   //(Insert code here)
   //Move the duplicate object "forward" (using the axis the camera is facing) based on the size of the original object
   //(Insert code here)
//}

Edit: This is based on the assumption that the argument %val represents selected object(s).
#5
02/24/2012 (9:08 pm)
Alright, let me give this my best shot:

At the top of WorldEditor::onClick, in tools/worldEditor/scripts/worldEditor.ed.cs, add this bit:

$editorObj = %obj; echo("Selected " @ %obj);

And now for your function:

function objectDuplicationSnapping(%val)
{
   // Get selected object
   %obj = $editorObj;
   
   // Figure out which way the cam is facing
   %dir = "";
   %bestRatio = 0;
   %camVec = LocalClientConnection.camera.getForwardVector();
   %camVec = VectorNormalize(%camVec);
   //--- Left?
   %left = "-1 0 0";
   %leftDot = VectorDot(%camVec,%left);
   if (%leftDot / 1 > %bestRatio)
   {
     %dir = "left";
     %bestRatio = %leftDot / 1;
   }
   //--- Right?
   %right = "1 0 0";
   %rightDot = VectorDot(%camVec,%right);
   if (%rightDot / 1 > %bestRatio)
   {
     %dir = "right";
     %bestRatio = %rightDot / 1;
   }
   //--- Up?
   %up = "0 0 1";
   %upDot = VectorDot(%camVec,%up);
   if (%upDot / 1 > %bestRatio)
   {
     %dir = "up";
     %bestRatio = %upDot / 1;
   }
   //--- Down?
   %down = "0 0 -1";
   %downDot = VectorDot(%camVec,%down);
   if (%downDot / 1 > %bestRatio)
   {
     %dir = "down";
     %bestRatio = %downDot / 1;
   }
   //--- Up?
   %up = "0 0 1";
   %upDot = VectorDot(%camVec,%up);
   if (%upDot / 1 > %bestRatio)
   {
     %dir = "up";
     %bestRatio = %upDot / 1;
   }
   //--- Down?
   %down = "0 0 -1";
   %downDot = VectorDot(%camVec,%down);
   if (%downDot / 1 > %bestRatio)
   {
     %dir = "down";
     %bestRatio = %downDot / 1;
   }
   //--- front?
   %front = "0 1 1";
   %frontDot = VectorDot(%camVec,%front);
   if (%frontDot / 1 > %bestRatio)
   {
     %dir = "front";
     %bestRatio = %frontDot / 1;
   }
   //--- back?
   %back = "0 -1 0";
   %backDot = VectorDot(%camVec,%back);
   if (%backDot / 1 > %bestRatio)
   {
     %dir = "back";
     %bestRatio = %backDot / 1;
   }
   
   %dupVec = "0 0 0";
   if (%dir $= "Left")
     %dupVec = %left;
   else if (%dir $= "Right")
     %dupVec = %right;
   else if (%dir $= "Up")
     %dupVec = %up;
   else if (%dir $= "Down")
     %dupVec = %down;
   else if (%dir $= "Front")
     %dupVec = %front;
   else if (%dir $= "Back")
     %dupVec = %back;
     
   // Alright, now with that out of the way...
   // The object should be moved in the direction of "%dupVec"
   
   // Duplicate the object:
   // This assumes we're trying to copy a TSStatic.
   
   // %clone = new (%obj.getClassName()) { 
      //  ^^^ I wonder if that'd work too...
   %clone = new TSStatic() {
      datablock = %obj.getDataBlock();
      position = "0 0 0";
      rotation = %obj.rotation;
   };
   
   // Get the original object's size from its box.
   // Let's do this based on the direction we're moving it.
   %size = 0;
   %box = %obj.getWorldBox();
   if (%dir $= "Left" || %dir $= "Right") // X-Axis
   {
      %v1 = getWord(%box,0);
      %v2 = getWord(%box,3);
      %size = mAbs(%v1 - %v2);
   }
   if (%dir $= "Front" || %dir $= "Back") // Y-Axis
   {
      %v1 = getWord(%box,1);
      %v2 = getWord(%box,4);
      %size = mAbs(%v1 - %v2);
   }
   if (%dir $= "Down" || %dir $= "Up") // Z-Axis
   {
      %v1 = getWord(%box,2);
      %v2 = getWord(%box,5);
      %size = mAbs(%v1 - %v2);
   }
   
   // Almost done here!
   // Now we figure out where to place the duplicate.
   %offsetVec = VectorScale(VectorNormalize(%dupVec)),%size);
   %position = VectorAdd(%obj.getPosition(),%offsetVec);
   
   %clone.setTransform(%position);
   
   // Winner?
}

Warning: Completely absolutely 100% untested! I'm not sure if it'll work or not, no access to Torque at the moment. But it should be a good starting point! Let me know if you've got questions or if something goes awry.
#6
02/24/2012 (11:29 pm)
Thanks Bryce! I'll try that code when I get home from work in the morning. I'll let you know how it goes.
#7
02/25/2012 (7:51 pm)
@Bryce - I still haven't had the chance to try it, but I gorgot to ask if I need to put the function where I placed my place holder in ./Tools/World Editor/Scripts/editor.bind.ed.cs @ line 63
#8
02/25/2012 (8:00 pm)
Sure, you can put it there. As long as you have it properly bound, you're good.

You might want to test it out by setting $editorObj to the object you want to clone (e.g. $editorObj = 5414; ) and calling the objectDuplicationSnapping function through the console. If everything goes right, you should get a copy of the object snug up against the original in the direction you're facing the camera.
#9
02/26/2012 (6:06 am)
Trying to run the function through the console but it cannot find the function. Even tried "releasing" the script in Torsion.
#10
02/26/2012 (8:37 am)
Maybe there's a syntax error somewhere in there. Open up the console log file and do a Ctrl+F find for " ## ", without quotes. That'll show the first syntax error, most likely somewhere in that function. Post that here.
#11
02/27/2012 (6:23 am)
Fixed the syntax errors. One was on my part, the others were misplaced parenthesis. Now I'm having trouble getting the object to duplicate. I've tried using %obj as an argument, the object name, and the object ID. Here's the output for all three:
Quote:
==>objectDuplicationSnapping(%obj);
tools/worldEditor/scripts/editors/worldEditor.ed.cs (552): Unable to find object: '' attempting to call function 'getDataBlock'
TSStatic::_createShape() - No shape name!
TSStatic::onAdd() - Shape creation failed!
tools/worldEditor/scripts/editors/worldEditor.ed.cs (552): Register object failed for object (null) of class TSStatic.
tools/worldEditor/scripts/editors/worldEditor.ed.cs (561): Unable to find object: '' attempting to call function 'getWorldBox'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (584): Unable to find object: '' attempting to call function 'getPosition'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (586): Unable to find object: '0' attempting to call function 'setTransform'
==>objectDuplicationSnapping(%obj);
tools/worldEditor/scripts/editors/worldEditor.ed.cs (552): Unable to find object: '' attempting to call function 'getDataBlock'
TSStatic::_createShape() - No shape name!
TSStatic::onAdd() - Shape creation failed!
tools/worldEditor/scripts/editors/worldEditor.ed.cs (552): Register object failed for object (null) of class TSStatic.
tools/worldEditor/scripts/editors/worldEditor.ed.cs (561): Unable to find object: '' attempting to call function 'getWorldBox'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (584): Unable to find object: '' attempting to call function 'getPosition'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (586): Unable to find object: '0' attempting to call function 'setTransform'
==>objectDuplicationSnapping(LMA_WallA);
tools/worldEditor/scripts/editors/worldEditor.ed.cs (552): Unable to find object: '' attempting to call function 'getDataBlock'
TSStatic::_createShape() - No shape name!
TSStatic::onAdd() - Shape creation failed!
tools/worldEditor/scripts/editors/worldEditor.ed.cs (552): Register object failed for object (null) of class TSStatic.
tools/worldEditor/scripts/editors/worldEditor.ed.cs (561): Unable to find object: '' attempting to call function 'getWorldBox'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (584): Unable to find object: '' attempting to call function 'getPosition'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (586): Unable to find object: '0' attempting to call function 'setTransform'
==>objectDuplicationSnapping(5061);
tools/worldEditor/scripts/editors/worldEditor.ed.cs (552): Unable to find object: '' attempting to call function 'getDataBlock'
TSStatic::_createShape() - No shape name!
TSStatic::onAdd() - Shape creation failed!
tools/worldEditor/scripts/editors/worldEditor.ed.cs (552): Register object failed for object (null) of class TSStatic.
tools/worldEditor/scripts/editors/worldEditor.ed.cs (561): Unable to find object: '' attempting to call function 'getWorldBox'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (584): Unable to find object: '' attempting to call function 'getPosition'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (586): Unable to find object: '0' attempting to call function 'setTransform'
Window focus status changed: focus: 0
Using background sleep time: 200
DirectInput deactivated.
#12
02/27/2012 (7:53 am)
Ah, I see where I went wrong. Replace this:

%clone = new TSStatic() {
      datablock = %obj.getDataBlock();
      position = "0 0 0";
      rotation = %obj.rotation;
   };

With

%clone = new TSStatic() {
      shapeName = %obj.shapeName;
      position = "0 0 0";
      rotation = %obj.rotation;
   };

I forgot that TSStatics use shapeNames, not datablocks :P
#13
02/27/2012 (6:59 pm)
I get a different error this time. To try to fix it I tried to use %obj instead of %val as the argument passed to the function in the code but the end result is the same.

Quote:
==>objectDuplicationSnapping(%obj);
TSStatic::_createShape() - No shape name!
TSStatic::onAdd() - Shape creation failed!
tools/worldEditor/scripts/editors/worldEditor.ed.cs (552): Register object failed for object (null) of class TSStatic.
tools/worldEditor/scripts/editors/worldEditor.ed.cs (561): Unable to find object: '' attempting to call function 'getWorldBox'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (584): Unable to find object: '' attempting to call function 'getPosition'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (586): Unable to find object: '0' attempting to call function 'setTransform'
==>objectDuplicationSnapping(5061);
TSStatic::_createShape() - No shape name!
TSStatic::onAdd() - Shape creation failed!
tools/worldEditor/scripts/editors/worldEditor.ed.cs (552): Register object failed for object (null) of class TSStatic.
tools/worldEditor/scripts/editors/worldEditor.ed.cs (561): Unable to find object: '' attempting to call function 'getWorldBox'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (584): Unable to find object: '' attempting to call function 'getPosition'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (586): Unable to find object: '0' attempting to call function 'setTransform'
==>objectDuplicationSnapping(LMA_WallA);
TSStatic::_createShape() - No shape name!
TSStatic::onAdd() - Shape creation failed!
tools/worldEditor/scripts/editors/worldEditor.ed.cs (552): Register object failed for object (null) of class TSStatic.
tools/worldEditor/scripts/editors/worldEditor.ed.cs (561): Unable to find object: '' attempting to call function 'getWorldBox'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (584): Unable to find object: '' attempting to call function 'getPosition'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (586): Unable to find object: '0' attempting to call function 'setTransform'
#14
02/27/2012 (7:09 pm)
I changed line 552 to %clone = new TSStatic(LMA_WallA) and tried %clone = new TSStatic(5061).

Same results for both:
Quote:
=>objectDuplicationSnapping();
tools/worldEditor/scripts/editors/worldEditor.ed.cs (552): Cannot re-declare object [LMA_WallA].
tools/worldEditor/scripts/editors/worldEditor.ed.cs (561): Unable to find object: '' attempting to call function 'getWorldBox'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (584): Unable to find object: '' attempting to call function 'getPosition'
tools/worldEditor/scripts/editors/worldEditor.ed.cs (586): Unable to find object: '0' attempting to call function 'setTransform'
#15
02/27/2012 (7:09 pm)
This function assumes that the variable $editorObj contains the ID of the object. So try

$editorObj = [theobject];
objectDuplicationSnapping();

That function doesn't even need the %val argument, because it's using $editorObj to see what's selected.

And the () after the new TSStatic contains the name of the duplicate, which is essentially creating a duplicate with the same name as the original. That's why you get the "cannot redeclare" error. Just leave it as ().
#16
02/27/2012 (7:29 pm)
This function is also assuming that all the objects you are cloning are TStatics. I think you are going to want to put a switch in on the object type before you create the clone so that they are created as the correct object type.
#17
02/28/2012 (12:36 am)
@ Bryce - Do I put that bit of cod ein my console or insert line 1 above the function in the script? I plan on getting this thing to work one way or another when I'm off during the week.
#18
02/28/2012 (5:36 am)
Just try it out in the console, and then we'll work on getting it integrated with the editor once everything checks out.
#19
02/28/2012 (11:19 am)
Success. All hail Bryce.
#20
02/28/2012 (11:30 am)
Of course the first time I write a piece of code that just about works on the first run, it's not for my own project :P
Page «Previous 1 2