Shard Posted December 14, 2009 Share Posted December 14, 2009 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? Quote 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 More sharing options...
macklebee Posted December 14, 2009 Share Posted December 14, 2009 look at Chris Paulson's blog. Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
Shard Posted December 14, 2009 Author Share Posted December 14, 2009 look at Chris Paulson's blog. Dear lord does that look confusing. 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. Quote 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 More sharing options...
Chris Paulson Posted December 14, 2009 Share Posted December 14, 2009 Dear lord does that look confusing. 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. Quote Link to comment Share on other sites More sharing options...
NoOdle Posted December 14, 2009 Share Posted December 14, 2009 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 Quote Link to comment Share on other sites More sharing options...
Rick Posted December 14, 2009 Share Posted December 14, 2009 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. Quote Link to comment Share on other sites More sharing options...
Niosop Posted December 14, 2009 Share Posted December 14, 2009 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. Quote Windows 7 x64 - Q6700 @ 2.66GHz - 4GB RAM - 8800 GTX ZBrush - Blender Link to comment Share on other sites More sharing options...
Rick Posted December 14, 2009 Share Posted December 14, 2009 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. Quote Link to comment Share on other sites More sharing options...
Niosop Posted December 14, 2009 Share Posted December 14, 2009 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. Quote Windows 7 x64 - Q6700 @ 2.66GHz - 4GB RAM - 8800 GTX ZBrush - Blender Link to comment Share on other sites More sharing options...
Rick Posted December 14, 2009 Share Posted December 14, 2009 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. Quote Link to comment Share on other sites More sharing options...
TylerH Posted December 14, 2009 Share Posted December 14, 2009 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. Quote nVidia 530M Intel Core i7 - 2.3Ghz 8GB DDR3 RAM Windows 7 Ultimate (64x)----- Visual Studio 2010 Ultimate Google Chrome Creative Suite 5 FL Studio 10 Office 15 ----- Expert Professional Expert BMX Programmer ----- Link to comment Share on other sites More sharing options...
TylerH Posted December 14, 2009 Share Posted December 14, 2009 http://en.wikipedia.org/wiki/Frustum For those too lazy to search. Quote nVidia 530M Intel Core i7 - 2.3Ghz 8GB DDR3 RAM Windows 7 Ultimate (64x)----- Visual Studio 2010 Ultimate Google Chrome Creative Suite 5 FL Studio 10 Office 15 ----- Expert Professional Expert BMX Programmer ----- Link to comment Share on other sites More sharing options...
Rick Posted December 14, 2009 Share Posted December 14, 2009 So why wouldn't we just do that then? Wouldn't that handle our angle check to see if the entity is within the angle? If we set that as the for each AABB it should only return entities that are within that angle right? Quote Link to comment Share on other sites More sharing options...
TylerH Posted December 14, 2009 Share Posted December 14, 2009 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. Quote nVidia 530M Intel Core i7 - 2.3Ghz 8GB DDR3 RAM Windows 7 Ultimate (64x)----- Visual Studio 2010 Ultimate Google Chrome Creative Suite 5 FL Studio 10 Office 15 ----- Expert Professional Expert BMX Programmer ----- Link to comment Share on other sites More sharing options...
Shard Posted December 14, 2009 Author Share Posted December 14, 2009 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. Quote 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 More sharing options...
Niosop Posted December 14, 2009 Share Posted December 14, 2009 Then instead of returning true right away, increment a counter or fill an array or whatever then evaluate it later against your criteria and return either Seen, Partially Seen or Not Seen. Quote Windows 7 x64 - Q6700 @ 2.66GHz - 4GB RAM - 8800 GTX ZBrush - Blender Link to comment Share on other sites More sharing options...
Chris Paulson Posted December 15, 2009 Share Posted December 15, 2009 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. Quote Link to comment Share on other sites More sharing options...
Chris Paulson Posted December 15, 2009 Share Posted December 15, 2009 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; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.