Scaling an entire mission
by Lee Latham · in Torque Game Engine · 04/20/2008 (3:14 am) · 8 replies
Is it possible to scale everything in a mission while still keeping the relative locations of the mission objects intact?
I'm wanting to tweak some of my missions, potentially, by scaling them in their entirety. Yeah, I know it's a little weird, but I think it's interesting just the same.
The problem is that apparently the locations of objects is often done using an arbitrary corner of the bounding box. Thus, when you scale them, they are thrown all out of whack.
That's okay, of course, but I was just wondering if anybody else had run up against that and had found an elegant solution. It occurs to me that it might be possible to do something at the source level to the coordinate space the mission exists in, or something along those lines. A little over my head, but it sure would be grand if anyone had an elegant suggestion!
Right now I'm manually moving all the mission objects--just to test a particular scale--and tedious in the extreme.
lee
I'm wanting to tweak some of my missions, potentially, by scaling them in their entirety. Yeah, I know it's a little weird, but I think it's interesting just the same.
The problem is that apparently the locations of objects is often done using an arbitrary corner of the bounding box. Thus, when you scale them, they are thrown all out of whack.
That's okay, of course, but I was just wondering if anybody else had run up against that and had found an elegant solution. It occurs to me that it might be possible to do something at the source level to the coordinate space the mission exists in, or something along those lines. A little over my head, but it sure would be grand if anyone had an elegant suggestion!
Right now I'm manually moving all the mission objects--just to test a particular scale--and tedious in the extreme.
lee
#2
04/20/2008 (11:34 am)
Well, it would, but the engine can have issues when you do that.
#3
I used php because of its string handling capabilities. The configuration information is set at the top of the script.
you need to put
// ---START SCALE TRANSLATION---
and
// ---END SCALE TRANSLATION---
in the .mis file to define what block of code is being scaledtranslated
04/20/2008 (7:24 pm)
Here's a little php script that I use to scale/translate objects in the mission file. It's saved me literally days of work. I used php because of its string handling capabilities. The configuration information is set at the top of the script.
you need to put
// ---START SCALE TRANSLATION---
and
// ---END SCALE TRANSLATION---
in the .mis file to define what block of code is being scaledtranslated
<?php
// This script scales and transposes the position of objects between the start and end tags.
// Make sure the start and end tags surround all and only the objects that need repositioning
// Written by Chris Gosling.
// ----------------------------------------------------
class Vector3D
{
var $x;
var $y;
var $z;
function Vector3D ($x, $y, $z)
{
$this->x = $x;
$this->y = $y;
$this->z = $z;
}
}
// ----------------------------------------------------
// ###################################################
// ################### CONFIGURE THE CONVESION HERE ###
// ###################################################
$START_TAG = "---START SCALE TRANSLATION---"; // Needs to go in mis file where scale/translation begins
$END_TAG = "---END SCALE TRANSLATION---"; // Needs to go in mis file where scale/translation ends
$IN_FILE = "input_file.mis"; // name of input file
$OUT_FILE = "output_file.mis.mis"; // name of output file
$MAX_LEN = 4096;
$PRECISION = 4;
// Use this for transposition
$Vbo = new Vector3D(-202.57, -228.236, 140.003); // Old origin point
$Vbn = new Vector3D(-212.82, -228.236, 140.003); // New origin point
// Use this for scaling
$OLD_SCALE = 1;
$NEW_SCALE = 1;
// ########################## END CONFIGURE ###
$SCALE_FACTOR = $NEW_SCALE / $OLD_SCALE;
print "\n\n--------------- START ----------------\n\n";
// open in file
$fin = fopen($IN_FILE, "r");
if ($fin === false)
{
print "Failed to open input file\n";
exit(0);
}
// open out file
$fout = fopen($OUT_FILE, "w");
if ($fout === false)
{
print "Failed to open output file\n";
fclose($fin);
exit(0);
}
// search for starting pos
$found = false;
while (!feof($fin))
{
$line = fgets($fin, $MAX_LEN);
// write to output file
if (fwrite($fout, $line) === false)
{
print "Error writing to output file\n";
fclose($fin);
fclose($fout);
exit(0);
}
// print "[".(++$count)."]";
if (strpos($line, $START_TAG) !== false)
{
print "\n\n** START FOUND **\n\n";
$found = true;
break;
}
}
if (!$found)
print "Failed to find Start Position\n";
else
{
// loop through
while (!feof($fin))
{
$line = fgets($fin, $MAX_LEN);
// ------------- ADJUST THE POSITION -------------
if (strpos($line, "position"))
{
// get the current vector
// position = "189.16 -568.59 158.2";
$bits = explode("\"", $line);
$coords = explode(" ", $bits[1]);
$Vo = new Vector3D($coords[0], $coords[1], $coords[2]);
// scale coords by scaling the difference between this coord and the building old coord
$Vdif = new Vector3D(($Vo->x - $Vbo->x) * $SCALE_FACTOR, ($Vo->y - $Vbo->y) * $SCALE_FACTOR, ($Vo->z - $Vbo->z) * $SCALE_FACTOR);
// add the scaled difference onto the new building vector to get the scaled and transposed vector
$Vn = new Vector3D($Vbn->x + $Vdif->x, $Vbn->y + $Vdif->y, $Vbn->z + $Vdif->z);
// reconstruct the line
$line = $bits[0]."\"".round($Vn->x, $PRECISION)." ".round($Vn->y, $PRECISION)." ".round($Vn->z, $PRECISION)."\";\n";
}
// ------------- ADJUST THE SCALE -------------
// scale = "1 1 1";
if (!($OLD_SCALE == 1 && $NEW_SCALE == 1))
{
if (strpos($line, "scale"))
{
// get the current vector
// position = "189.16 -568.59 158.2";
$bits = explode("\"", $line);
$scales = explode(" ", $bits[1]);
$Vo = new Vector3D($scales[0], $scales[1], $scales[2]);
// adjust the scale
$Vn = new Vector3D($Vo->x * $SCALE_FACTOR, $Vo->y * $SCALE_FACTOR, $Vo->z * $SCALE_FACTOR);
// reconstruct the line
$line = $bits[0]."\"".round($Vn->x, $PRECISION)." ".round($Vn->y, $PRECISION)." ".round($Vn->z, $PRECISION)."\";\n";
}
}
// write to output file
if (fwrite($fout, $line) === false)
{
print "Error writing to output file\n";
fclose($fin);
fclose($fout);
exit(0);
}
// while not at end pos
if (strpos($line, $END_TAG) !== false)
{
print "\n\n** END FOUND **\n\n";
break;
}
}
// write the rest of the file out
while (!feof($fin))
{
$line = fgets($fin, $MAX_LEN);
// write to output file
if (fwrite($fout, $line) === false)
{
print "Error writing to output file\n";
fclose($fin);
fclose($fout);
exit(0);
}
}
}
fclose($fin);
fclose($fout);
print "\n\n--------------- END ----------------\n\n";
?>
#4
But, if I'm understanding correctly, you're simply scaling the vectors by the scale factor, which now seems obvious--If I'm understanding correctly. Am I? I don't know PHP so I think I could easily be missing something.
Thanks man, like you said--this will save many days of unbelieveably tedious work!
04/20/2008 (7:48 pm)
ChrisG, you are cool beyond belief! I was just today thinking that some kind of conversion script would be required, but I couldn't figure out how to calculate the translations. I figured you would need to know the size of the object in question in world coordinates in order to compute the new position, and that would be over my head to code (in less than a month). But, if I'm understanding correctly, you're simply scaling the vectors by the scale factor, which now seems obvious--If I'm understanding correctly. Am I? I don't know PHP so I think I could easily be missing something.
Thanks man, like you said--this will save many days of unbelieveably tedious work!
#5



solid! Looks like I need to leave the lights out of the conversion, though. And that is ooookaaaay!
Thanks for sharing--now I merely have work to do, as opposed to mind numbing makes you want to gouge your eyeballs out wretchedness.
04/20/2008 (8:25 pm)
You rock Chris! Watch the little man "grow":


solid! Looks like I need to leave the lights out of the conversion, though. And that is ooookaaaay!
Thanks for sharing--now I merely have work to do, as opposed to mind numbing makes you want to gouge your eyeballs out wretchedness.
#6
The code comments may be a little confusing. I originally wrote it when I realised that a building I created and populated to suit kork was way too big for the modern solider guy, plus I wanted to move the building a little. The building center became the origin point. - So you'll lots of references to 'the building'.
You can move and scale the light positions with the script but you'll need to make your lights brighter or dimmer if scaling up or down.
04/22/2008 (3:04 am)
Quote:But, if I'm understanding correctly, you're simply scaling the vectors by the scale factorThat's pretty much it, plus offsetting by the new origin point.
The code comments may be a little confusing. I originally wrote it when I realised that a building I created and populated to suit kork was way too big for the modern solider guy, plus I wanted to move the building a little. The building center became the origin point. - So you'll lots of references to 'the building'.
Quote:Looks like I need to leave the lights out of the conversion
You can move and scale the light positions with the script but you'll need to make your lights brighter or dimmer if scaling up or down.
#7
The origin. A) Where would I find this information in my mission? B) How would I calculate the new one to feed to the script? And most importantly C) Does it even matter?
If I'm understanding it correctly, it really doesn't matter what those numbers are set to, other than for the convenience of having your level in a certain part of the cartesian coordinate system. Which...I'm not sure why that would really matter, gamewise. I actually left your default values in for $Vbo and $Vbn, and it worked great, except that my origin was not in the middle of the level.
BTW I had meant to say that your code was very nicely organized and commented. The only gap in understanding is in my sad math and php skills.
Also, I just counted and the scene above has over 220 objects, painstakingly placed in the mission editor. But over the last day I've done significant gameplay experimentation with several different scales, which has been a great deal of fun. :-)
04/22/2008 (5:02 am)
Thanks. Can I ask one more question? Mind you it worked perfectly first time straight out of the box--I'm just trying to understand it, because I like to understand things.The origin. A) Where would I find this information in my mission? B) How would I calculate the new one to feed to the script? And most importantly C) Does it even matter?
If I'm understanding it correctly, it really doesn't matter what those numbers are set to, other than for the convenience of having your level in a certain part of the cartesian coordinate system. Which...I'm not sure why that would really matter, gamewise. I actually left your default values in for $Vbo and $Vbn, and it worked great, except that my origin was not in the middle of the level.
BTW I had meant to say that your code was very nicely organized and commented. The only gap in understanding is in my sad math and php skills.
Also, I just counted and the scene above has over 220 objects, painstakingly placed in the mission editor. But over the last day I've done significant gameplay experimentation with several different scales, which has been a great deal of fun. :-)
#8
If you are trying to move just a building and its contents you would enter the old and new coordinates of the building, then all its contents are moved accordingly based on the difference of the vectors. The numbers can be anything as long as the difference is correct.
eg
$Vb0 = new Vector3D(0, 0, 0);
$Vbn = new Vector3D(10, 20, 30);
would move the objects the same as
$Vb0 = new Vector3D(100, 200, 300);
$Vbn = new Vector3D(110, 220, 330);
its just that its easier to get coords of the building (or some other key object) especially when they have obscure values to 4 decimal places.
04/22/2008 (11:51 pm)
The origin is arbitrary and should be left at 0,0,0 for both $Vbo and $Vbn if nothing's being moved. If you are trying to move just a building and its contents you would enter the old and new coordinates of the building, then all its contents are moved accordingly based on the difference of the vectors. The numbers can be anything as long as the difference is correct.
eg
$Vb0 = new Vector3D(0, 0, 0);
$Vbn = new Vector3D(10, 20, 30);
would move the objects the same as
$Vb0 = new Vector3D(100, 200, 300);
$Vbn = new Vector3D(110, 220, 330);
its just that its easier to get coords of the building (or some other key object) especially when they have obscure values to 4 decimal places.
Torque Owner Erik Madison