Jump to content

Raycasting Cone


Shard
 Share

Recommended Posts

Hey everyone.

 

So I am working on ray casting for my AI to see whether they can see the player or not and I want to do a three dimensional ray cast so that AI on rooftops, etc, can see the player below and I'm not sure how to implement this without calculating the shape of a cone and then ray casting in the shape of a cone, which would be an extremely expensive operation.

 

Also, I only care about the first thing that they see, so I can compare the results to my player to see if it was my player that the AI saw or not.

 

Any ideas?

simpleSigPNG.png

 

Programmer/Engineer/Student

www.reikumar.com

 

2.6 GHz Intel Core Duo - nVidia GeForce 8600 GT - Windows 7 64-bit - 4 Gigs RAM

C++ - Visual Studio Express - Dark GDK - Leadwerks SDK

Link to comment
Share on other sites

look at Chris Paulson's blog.

 

Dear lord does that look confusing. :blink:

 

I tried to follow through the code but I ended up getting lost and confused. I'm not sure at which part he does the actual ray casting to check if anything is seen or not.

simpleSigPNG.png

 

Programmer/Engineer/Student

www.reikumar.com

 

2.6 GHz Intel Core Duo - nVidia GeForce 8600 GT - Windows 7 64-bit - 4 Gigs RAM

C++ - Visual Studio Express - Dark GDK - Leadwerks SDK

Link to comment
Share on other sites

Dear lord does that look confusing. :blink:

 

I tried to follow through the code but I ended up getting lost and confused. I'm not sure at which part he does the actual ray casting to check if anything is seen or not.

 

Tell me the bit your stuck on and I'll help...

 

Here's an overview: -

 

for each enemy in view range
  check if enemy in FOV
      for each bone of enemy
         do line pick at pos of bone
         if can see then exit
      end
   end
end

I use the bones as this will give me the different important parts of a body. This way if only a head or a hand etc is visible a linepick will succeed.

Link to comment
Share on other sites

you could also make it so that it counts how many successful picks it achieves, and this could be used to determine how much of an enemy/player is visible.. which in turn could trigger different ai methods, if they only see a bit, the enemy might want to sneak up to investigate before shooting

Link to comment
Share on other sites

Where is the part that creates the vision cone and only looks for things inside the vision cone? I'm not seeing how that's done.

 

Was it:

 

box.X0 = pos.X - m_viewRange;

box.Y0 = pos.Y - m_viewRange;

box.Z0 = pos.Z - m_viewRange;

box.X1 = pos.X + m_viewRange;

box.Y1 = pos.Y + m_viewRange;

box.Z1 = pos.Z + m_viewRange;

 

If so then it's not really a cone right? It's a box. Which is ok but just clarifying it.

Link to comment
Share on other sites

Naaa, that just limits the number of entities he has to check. The vision cone code is:

 

if(angle <= (m_viewAngle/2.0))

 

At least that's what I get from a quick glance at the code, I could be wrong.

Windows 7 x64 - Q6700 @ 2.66GHz - 4GB RAM - 8800 GTX

ZBrush - Blender

Link to comment
Share on other sites

I guess I would either assume the ray that is being cast is a cone shape itself, or you could cast many rays within a cone. Rays are just a line with a given radius right? So once the cone is defined I would expect to see some sort of looping through some rays within this cone.

Link to comment
Share on other sites

Well, he doesn't really use a cone at all. He does raycasts between the target's eyes and the bones of the enemies. The only place a cone really comes into it is the check to see if the angle between the viewer's eyes and the enemy is within the view angle. So it's more like the many rays within a cone you mentioned, just not arbitrary rays.

Windows 7 x64 - Q6700 @ 2.66GHz - 4GB RAM - 8800 GTX

ZBrush - Blender

Link to comment
Share on other sites

Oh I see. First check is a bounding box to get just entities within the box around the character. Then check the angle between the player and each character to get the angle and if the angle is within a tolerance then that entity is at least within the view cone.

 

Would be cool if the bounding box could be made a cone shape. That would handle all that for us.

Link to comment
Share on other sites

You can make the box shaped like a frustum...it is a cuboid (i.e. 6 vertices like you can specify for a box) but shaped like a cone.

 

Hence why they use viewing frustums (mistakenly spelled frustrum in American A LOT), for camera viewing.

52t__nvidia.png nVidia 530M cpu.gif Intel Core i7 - 2.3Ghz 114229_30245_16_hardware_memory_ram_icon.png 8GB DDR3 RAM Windows7_Start.gif Windows 7 Ultimate (64x)

-----

IconVisualStudio16.png Visual Studio 2010 Ultimate google-Chrome.png Google Chrome PhotoshopLinkIndicator.png Creative Suite 5 icon28.gif FL Studio 10 MicrosoftOfficeLive.png Office 15

-----

csharp.png Expert cpp.png Professional lua_icon.png Expert BMX Programmer

-----

i-windows-live-messenger-2009.pngskype-icon16.pngaim_online.pnggmail.pngicon_48x48_prism-facebook.pngtunein-web.pngyahoo.giftwitter16.png

Link to comment
Share on other sites

Yeah, though it is a bit rough since it isn't fully conical, it gives you a really nice approximation. If it is good enough for a camera, it is definitely worth use for AI.

52t__nvidia.png nVidia 530M cpu.gif Intel Core i7 - 2.3Ghz 114229_30245_16_hardware_memory_ram_icon.png 8GB DDR3 RAM Windows7_Start.gif Windows 7 Ultimate (64x)

-----

IconVisualStudio16.png Visual Studio 2010 Ultimate google-Chrome.png Google Chrome PhotoshopLinkIndicator.png Creative Suite 5 icon28.gif FL Studio 10 MicrosoftOfficeLive.png Office 15

-----

csharp.png Expert cpp.png Professional lua_icon.png Expert BMX Programmer

-----

i-windows-live-messenger-2009.pngskype-icon16.pngaim_online.pnggmail.pngicon_48x48_prism-facebook.pngtunein-web.pngyahoo.giftwitter16.png

Link to comment
Share on other sites

Tell me the bit your stuck on and I'll help...

 

Here's an overview: -

 

for each enemy in view range
  check if enemy in FOV
      for each bone of enemy
         do line pick at pos of bone
         if can see then exit
      end
   end
end

I use the bones as this will give me the different important parts of a body. This way if only a head or a hand etc is visible a linepick will succeed.

 

 

Does this mean that using this method I would have to tell my AI Manager class how many enemies are around them and if they are within visible range? I ask this because I plan to have more than one "good player", like teammates following the player around.

 

Also, what if I want to check the amount of a character is visible? Like if only an arm or hand is seen, then I want the AI to be suspicious and investigate, etc.

simpleSigPNG.png

 

Programmer/Engineer/Student

www.reikumar.com

 

2.6 GHz Intel Core Duo - nVidia GeForce 8600 GT - Windows 7 64-bit - 4 Gigs RAM

C++ - Visual Studio Express - Dark GDK - Leadwerks SDK

Link to comment
Share on other sites

Does this mean that using this method I would have to tell my AI Manager class how many enemies are around them and if they are within visible range? I ask this because I plan to have more than one "good player", like teammates following the player around.

 

Also, what if I want to check the amount of a character is visible? Like if only an arm or hand is seen, then I want the AI to be suspicious and investigate, etc.

 

 

The view cone is done by the viewangle bit of code: -

 

	// observer vector
	TVec3 pos = EntityPosition( m_head,1);
	PositionEntity( m_pivot, pos,1);
	TVec3 tform = TFormVector( Vec3(0,0,1), m_body->pivot, NULL);
	AlignToVector(m_pivot, tform, 3, 1);

	TVec3 observerPos = EntityPosition( m_entity, 1);
	// pick vector
	float angle = angleToDest( m_pivot, m_targetPos );
	if(angle <= (m_viewAngle/2.0))
	{

 

The bit about the box ie ForEachEntityAABB is getting a list of nearby enemies. I base it on the idea of NPC/players being in teams for example red team, blue etc. I store the team in a entity key. Anyone in a different team is considered to be an enemy.

 

Currently my code has only seen/not seen logic and I exits at the first line pick success. You could do all of them and work out a % visible but this would effect the FPS serverly.

Link to comment
Share on other sites

This is the bit of code that works out teams: -

 

...
pos = EntityPosition( m_entity, 1);
box.X0 = pos.X - m_viewRange;
box.Y0 = pos.Y - m_viewRange;
box.Z0 = pos.Z - m_viewRange;
box.X1 = pos.X + m_viewRange;
box.Y1 = pos.Y + m_viewRange;
box.Z1 = pos.Z + m_viewRange;
ForEachEntityInAABBDo( box, (BP)checkIfEnemy, (byte*)this, ENTITY_MODEL );
...


int _stdcall checkIfEnemy( TEntity e, byte* extra )
{
Vision *vis = (Vision*)extra;
if (GetEntityType(e) != 3) return 1;		// Not a NPC/Player
string team = GetEntityKey(e,"team");

if (team == vis->m_team) return 1;		// On same team

vis->m_nearbyEnemy[ EntityDistance(vis->m_entity,e) ] = e;

return 1;
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...