View topic - Detecting unit depth in water.



All times are UTC + 1 hour


Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: 27 Dec 2010, 08:28 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
So I want to do some cool stuff with animation but I need a way to tell if the unit is underwater or even better the specific unit depth in the water. Now, I am currently using:
Code:
setSFXoccupy(setSFXoccupy_argument)
{   
terraintype = setSFXoccupy_argument;   

      if(terraintype == 2)
      {   
      SET MAX_SPEED to [4];    
      
      speedMult    =   5;
      }
         else
         {   
         SET MAX_SPEED to [1.0];   
         speedMult    =   2.5;
         }
         
   get PRINT(get GROUND_WATER_HEIGHT(get PIECE_XZ(base)));
   
   if(get GROUND_WATER_HEIGHT(get PIECE_XZ(base)) <= -50000)
   {   hide head;   }
   else
   {   show head;   }
}


Fun shit is that the head hides as soon as the toe touches the water. Also sfx occupy doesn't seem to to trigger except when the unit enters or leaves and fun shit is that the getwater height even though it is printing shit like:
Quote:
[ 326] Value 1: 431640, 2: 0, 3: 0, 4: 0
[ 336] Value 1: 431640, 2: 0, 3: 0, 4: 0
[ 592] Value 1: -38516, 2: 0, 3: 0, 4: 0
[ 608] Value 1: -938666, 2: 0, 3: 0, 4: 0
[ 768] Value 1: -173538, 2: 0, 3: 0, 4: 0
[ 800] Value 1: -61020, 2: 0, 3: 0, 4: 0
[ 928] Value 1: -151035, 2: 0, 3: 0, 4: 0

the head hides as soon as the unit touches water :|

So maybe I need to be doing this different. I am not against setting up a gadget to test when units are in the water and then checking unit depth(assuming such a thing is possible) however, I wanted to ask around.

Here is what I am going to try and do. Effectively, I want to detect when a unit reaches a specific depth(this is an amphibious unit) at this point in time the unit will switch over to a swim mode animation with speed boost. However, AS IT stands now I can only pick up in or out of water, rather silly and sadly kind of stupid. So I was hoping someone here may have some idea. There are no examples of such a thing in spring and before someone says pelican, that is a hover unit and it doesn't have to check actual depth.


Top
 Offline Profile  
 
PostPosted: 27 Dec 2010, 09:13 
Spring 1944 Developer
User avatar

Joined: 11 Oct 2005, 06:18
Location: Ukraine
SetSFXOccupy is not good for what you want.

I used the following code to make an amphib unit:
Code:
WaterCheck()
{
   var GroundHeight;
   while(TRUE)
   {
      GroundHeight=get GROUND_WATER_HEIGHT(get PIECE_XZ(hull));
      if (GroundHeight<=FLOAT_DEPTH)
      {
         bFloating = TRUE;
         move hull to y-axis FLOAT_DEPTH speed [5];
         turn hull to x-axis 0 speed <90>;
         turn hull to z-axis 0 speed <90>;
         if(bMoving)
         {
            emit-sfx SFXTYPE_WAKE1 from wake;
         }
         set MAX_SPEED to WaterSpeed;
         set UPRIGHT to 1;
      }
      else
      {
         bFloating = FALSE;
         if (GroundHeight<0)
         {
            move hull to y-axis GroundHeight now;
         }
         else
         {
            move hull to y-axis [0] now;
         }
         set MAX_SPEED to LandSpeed;
         set UPRIGHT to 0;
      }
      sleep 150;
   }
}


The unit is a vehicle, and upon reaching certain depth, it "floats up": wheels no longer touch the bottom, it becomes upright and gets much lower speed (too bad I can't make it stop leaving tracks on the bottom). WaterCheck() is started from Create().

This works reliably as far as I can tell. It can be optimized of course, for example setSFXOccupy might be used to disable the looping check when unit is out of the water and enable it again when it enters the water. Not that there is any noticeable performance loss with dozens of that unit on a map as it is.

And technically my vehicle is a hover, because there is no other (simple) way to make it float on the water surface. However upright=0 hovers behave like vehicles on land.


Top
 Offline Profile  
 
PostPosted: 27 Dec 2010, 17:43 
Conflict Terra Developer
User avatar

Joined: 21 Jan 2010, 06:21
Location: Tucson
Oh how I wish this conversation happened months ago. I may be working with these examples soon for something I wanted to do a long time ago.


Top
 Offline Profile  
 
PostPosted: 27 Dec 2010, 17:48 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
sanada, I will tell you what one of my programming instructors once told me.

Never be afraid to ask, odds are someone has already solved the problem and you will likely waste a lot of time in redundant effort when you simply could have asked.


Top
 Offline Profile  
 
PostPosted: 29 Dec 2010, 00:03 
User avatar

Joined: 08 Jun 2009, 16:59
Location: Croatia
I had a similar problem with XTA amphibious ships. This code controls wether the ship is deep enough to sail or use it's wheels for land:
Code:
MotionControl()
{
   var waterdepth;
   while( TRUE )
   {
      waterdepth = get GROUND_WATER_HEIGHT(get UNIT_XZ);
      if( waterdepth < [-6.9] ) {
         set UPRIGHT to 1;
      }
      else
      {
         set UPRIGHT to 0;
         if( waterdepth < 0)
            move body to y-axis ([6.9]+waterdepth) now;
         else
            move body to y-axis [0.0] now;      
      }
      if( ( !Floating ) AND (waterdepth < [-6.9]) )
      {
         move body to y-axis [0.0] now;
         Floating = 1;
         call-script transform();
         if (bMoving) start-script swim();
      }
      else if( Floating AND (waterdepth > [-6.9]) )
      {
         call-script reform();
         Floating = 0;
         turn base to x-axis <0.0> now;
         turn base to z-axis <0.0> now;
         if (bMoving) call-script walk();
      }
      sleep 66;
   }
}


Top
 Offline Profile  
 
PostPosted: 03 Jun 2012, 12:09 
Journeywar Developer & Mapper
User avatar

Joined: 24 Jan 2006, 21:12
Location: There is no god - and reality is his prophetess
Is there a lua version of this somewhere?


Top
 Offline Profile  
 
PostPosted: 03 Jun 2012, 15:12 
User avatar

Joined: 19 Aug 2009, 15:09
Location: Sol 3
You can check out the shiva in BAR (look for "Swimming Animation")
Or find the skeleton script from the Cursed, which the shiva script is based on, regarding changing behaviour in water.


Top
 Offline Profile  
 
PostPosted: 04 Jun 2012, 05:14 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
for an alternative to the "poll y-position every blub moments" try:
UnitEnteredWater()
UnitLeftWater()
http://springrts.com/wiki/LuaCallinReturn


Top
 Offline Profile  
 
PostPosted: 04 Jun 2012, 07:19 
Journeywar Developer & Mapper
User avatar

Joined: 24 Jan 2006, 21:12
Location: There is no god - and reality is his prophetess
knorke wrote:
for an alternative to the "poll y-position every blub moments" try:
UnitEnteredWater()
UnitLeftWater()
http://springrts.com/wiki/LuaCallinReturn


to awesome, to late.. i did it again .. : (


Top
 Offline Profile  
 
PostPosted: 04 Jun 2012, 11:13 
Moderator

Joined: 12 Oct 2007, 08:24
yuritch wrote:
too bad I can't make it stop leaving tracks on the bottom
Just do Spring.SetUnitLeaveTracks(unitID, false) in the unit script.


Top
 Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group

Site layout created by Roflcopter et al.