Game Development Community

onUpdate problem

by David Ravel · in Torque Game Builder · 03/20/2009 (2:33 pm) · 8 replies

I just wrote this little bit of script to try and change the size of my player character as he moves to the background and foreground of my adventure game. My hope is that it will add a bit of depth to my game. The problem I'm running into is that when I put it into the game I lose control of the player character and he cannot move but once I remove it everything is fine. Also, could I rewrite this as a switch? I'm not sure since I have greater than and less than conditions together.

function Player::onUpdate(%this)
{
   %this.getPositionY();
   
   if (%this.positionY > 25)
   {
      %this.size = "16.0 16.0";
   }
   
   if (%this.positionY < 25 && %this.positionY > 15)
   {
      %this.size = "15.0 15.0";
   }
   
   if (%this.positionY <15 && %this.positionY > 5)
   {
      %this.size = "14.0 14.0";
   }
   
   if (%this.positionY < 5 && %this.positionY > 0)
   {
      %this.size = "13.0 13.0";
   }
   
   if (%this.positionY < 0 && %this.positionY > -5)
   {
      %this.size = "12.0 12.0";
   }
   
   if (%this.positionY < -5 && %this.positionY > -10)
   {
      %this.size = "11.0 11.0";
   }
   
   if (%this.positionY < -10 && %this.positionY > -15)
   {
      %this.size = "10.0 10.0";
   }
   
   if (%this.positionY < -15 && %this.positionY > -20)
   {
      %this.size = "9.0 9.0";
   }
   
   if (%this.positionY < -20 && %this.positionY > -25)
   {
      %this.size = "8.0 8.0";
   }
   
   if (%this.positionY < -25 && %this.positionY > -30)
   {
      %this.size = "7.0 7.0";
   }
   
   if (%this.positionY < -30 && %this.positionY > -35)
   {
      %this.size = "6.0 6.0";
   }
   
   if (%this.positionY < -35)
   {
      %this.size = "5.0 5.0";
   }
}

#1
03/20/2009 (2:54 pm)
David,
i'm not sure about losing control of the player,
you might be interested in using a technique called Linear Interpolation.

when position is 25, size is 16
when position is -35, size is 5

using thee parameters, you can write a single bit of code that gets the correct value for size with any value of position:


the general formula for linear interpolation is:
outVal = (inVal - inMin) / inRange * outRange + outMin

in your case, inMin = -35, outMin = 5.0,
inRange = (25 - -35) = 60, outRange = 16 - 5 = 11.

so

%outVal = (%this.positionY - -35) / 60 * 11 + 5.0
#2
03/20/2009 (3:59 pm)
hmm that looks promising but I'm pretty new to scripting and I'm not sure how I would implement that. Would I need to do as I was doing above and just have an onUpdate function and in that function I just run that last line and assign the player's size to %outVal?

Thanks Orion.

The fact that having this onUpdate function seems to disable my able to control the player is beyond bizarre but since I'm working off the Adventure Kit I guess I'll go query their forum.
#3
03/20/2009 (4:37 pm)
you could rewrite that big onUpdate() as something like this:
function linearInterp(%inVal, %inMin, %inMax, %outMin, %outMax)
{
   %inRange  = %inMax  - %inMin ;
   %outRange = %outMax - %outMin;
   
   %outVal   = (%inVal - %inMin) / %inRange * %outRange + %outMin;
   return %outval;
}

function Player::onUpdate(%this)
{
   %inVal     = %this.getPositionY();
   %outVal    = linearInterp(%inVal, -35, 25, 5, 16);

   %this.size = %outVal SPC %outVal;
}

unfortunately i have no idea about what onUpdate() is or should be doing; i don't really use TGB much.
#4
03/20/2009 (6:13 pm)
Thanks a lot Orion, I got it working without a problem. My losing control of the character was just an issue of some other functions interfering with the onUpdate function. I wound up having to fold a couple methods together to get it working right.
#5
03/28/2009 (9:24 am)
Hey Orion, I hope you still get a notification of this, but I'm running into a problem that is a result of using the linear interpolation method. My character is getting smaller as he moves to the background correctly but his speed is staying constant so he appears to be moving like lightening at his smallest size. Is there are an equally simple way of correcting this as I imagine a lot of people using this method run into this problem? Oh and just a note, in the formula I changed 5 to 2 so he would be even smaller at max range.

Thanks a lot,

Dave
#6
03/29/2009 (4:17 pm)
hey david -
so you're sort of creating a kind of 3D perspective, where an object gets farther away and should move slower and get smaller when it's far ?

i'm not sure what your exact application is, but could you also scale the character speed as he gets farther away ?

if so, linear interpolation could help you with that as well, if you use linearInterp() with the same input values but outMin and outMax would be values for speed instead of size - have the max value be how fast the character should move when close, and min be how fast they should move when far. (or possibly vice-versa)

i think that it will still be just an approximation of "correct" perspective effects, but it might look fine.

is that helpful ?
#7
03/29/2009 (4:36 pm)
yes I've been messing with that. My movement system has a few features that complicate things but I think I'll be able to make it work. I kind of stumbled into that solution forgetting how obvious it is.

Thanks
#8
03/29/2009 (4:47 pm)
cool, sounds like yr on the way!