Author Topic: Tell if an object is in a shadow?  (Read 1241 times)

How can you tell if an object is in a shadow?

I saw ports code but it never works for me.

function isPointInShadow(%pos, %ignore) {
        %ray = containerRayCast(%pos,
                vectorAdd(%pos, vectorScale(getSunVector(), 250)),
                $TypeMasks::StaticShapeObjectType |
                $TypeMasks::FxBrickObjectType |
                $TypeMasks::VehicleObjectType |
                $TypeMasks::PlayerObjectType |
                $TypeMasks::ItemObjectType,
                %ignore
        );

        return %ray !$= 0;
}


Yes, I have the other codes he has for it. It just keeps giving me a true statement. In shadow or not.

Basically you fire a raycast from the object in the sun's direction and see if it hits anything.

edit: That's what the code you posted appears to do.

It doesn't seem to detect it correctly.



   function getDayCycleTime()
   {
      if(!$EnvGuiServer::DayCycleEnabled || !isObject(DayCycle))
      {
         error("ERROR: DayCycle not enabled.");
         return -1;
      }
      %time = $Sim::Time / DayCycle.dayLength + DayCycle.dayOffset;
      return %time - mFloor(%time);
   }
   
   function getSunVector()
   {
      %azim = mDegToRad($EnvGuiServer::Sunational socialistmuth);
      if($EnvGuiServer::DayCycleEnabled && isObject(DayCycle))
      {
         %time = getDayCycleTime();
         %badspotIsWeird = 0.583;
         if (%time < %badspotIsWeird)
            %elev = mDegToRad((%time / %badspotIsWeird) * 180);
         else
            %elev = mDegToRad(180 + ((%time - %badspotIsWeird) / (1 - %badspotIsWeird)) * 180);
      }
      else
         %elev = mDegToRad($EnvGuiServer::SunElevation);
      %h = mCos(%elev);
      return %h * mSin(%azim) SPC %h * mCos(%azim) SPC mSin(%elev);
   }

   function isPointInShadow(%pos, %ignore)
   {
      %ray = containerRayCast(%pos,
         vectorAdd(%pos, vectorScale(getSunVector(), 250)),
         $TypeMasks::StaticShapeObjectType |
         $TypeMasks::FxBrickObjectType |
         $TypeMasks::VehicleObjectType |
         $TypeMasks::PlayerObjectType |
         $TypeMasks::ItemObjectType,
         %ignore
         );
      return %ray !$= 0;
   }

function SimSet::SD_SurvivalTick(%this)
   {
      if(%this.gametype $= "Global Warming")
      {
         for(%i=0;%i<%this.getCount();%i++)
         {
            %cl = %this.getObject(%i);
            if(isObject(%pl=%cl.player))
            {
               if(!isPointInShadow(%pl.getWo,0))
               {
                  %pl.addHealth(-1);
                  %pl.isBeingRoasted = 1;
               }
            }
         }
      }
   }

if(!isPointInShadow(%pl.getWo,0))

There's your problem.

Code: [Select]
if(!isPointInShadow(%pl.position))

Oh wow, people actually use things I make? Well then.

Oh wow, people actually use things I make? Well then.
ya you make cool stuff yo

Forgot to say I fixed it long ago.

The code was used wrong for detection, it also seems that if the sun is north is somehow causes issues.

This works:

               if(!isPointInShadow(%pl.getHackPosition(),%pl))
               {
                  %pl.isBeingRoasted = 1;
                  %pl.addHealth(-1);
                  if(%pl.getMountedImage(0) != SD_RoastingFireImage.getID())
                     %pl.mountImage(SD_RoastingFireImage,1);
               }
               else
               {
                  %pl.isBeingRoasted = 0;
                  if(%pl.getMountedImage(1) == SD_RoastingFireImage.getID())
                     %pl.unmountImage(1);
                  %cl.incScore(0.05);
               }

Oh wow, people actually use things I make? Well then.
I use your stuff too.

How do you use getDayCycleStageName? What do you input into the function?

How do you use getDayCycleStageName? What do you input into the function?

getDayCycleStageName(getDayCycleStage(getDayCycleTime())) returns "dawn", "day", "dusk", "night" or "error".