if enemy can see player
BlitzMax Forums/BlitzMax Beginners Area/if enemy can see player
| ||
Hi. I've started a simple overhead shooter. But I want the enemies to shoot at the player depending if their angle is facing him. What would be the best way and least resource intensive way to do this? ![]() thanks Last edited 2012 |
| ||
I have some line intersection code that would probably do the job. It's fast, too. |
| ||
Look like METAL GEAR SOILD on NES :) |
| ||
Nice looking GFX! |
| ||
@GfK I'm not great at reading code, but thanks I'll try and see if I can implement that code. @Hotshot2005 Thanks, it doesn't play like it though, haha @GaryV Thanks, I'm not an artist but it's the best I can do. |
| ||
I'm having trouble with this, this is what I have, but I can see it's wrong, but Im not sure how to fix it: Basically, a basic cone of sight: Last edited 2012 |
| ||
Here's a cone of sight demo that should be fairly robust and easy to implement in your game.. |
| ||
Actually I've just realised there's some redundant code in the PointInViewAngle function. Since 'angle' is clamped to 0 to 360If ( minAngle > maxAngle ) If (( angle >=0 And angle <= maxAngle ) Or ( angle >= minAngle And angle <= 360 )) Return True End If can just be.. If ( minAngle > maxAngle ) If ( angle <= maxAngle Or angle >= minAngle ) Return True End If |
| ||
@matibee Thanks, that's exactly what I was looking for and I've implemented a version of it in my code. You have saved me life. |
| ||
You're welcome :) |
| ||
I'm wondering if I can get any more help with my mathematics. I've got square roots coming out of my ears. Now, that I can detect a player, I want to check against my 2d array map, to see if there is a solid block in the way between the player and the enemy which would prevent him seeing the player. the tileMap array stores the an int which is the type of tile, anything greater than 27 is a solid.( tileMap[x,y]) I've had many ideas here is one in pseudocode that didn't work. 'The distance between the enemy to the player distance = sqr(player1.x-enemy.x)*(player1.x-enemy.x) + (player1.y-enemy.y)*(player1.y-enemy.y) For(i = 0 to distance) if(tileMap[enemy.x+i,enemy.y+i] > 27 print "view of player blocked" exit loop end if next UPDATE: I've realised that this code is rubbish and doesn't take into account the direction/angle the enemy is facing in Last edited 2012 |
| ||
It would be useful to see the rest of your code. Can I assume your player and enemy positions are floating point values, where their integer positions match the tile array... ie Enemy( x1.444, y3.222) maps to tileMap[ 1, 3 ] ?? If that's the case you can probably use the direction vector ( vx = cos(angle), vy = sin(angle ) ) and incrementally add it to the enemy position Function VisibilityTest( enemy, distance-to-player ) checklength = 1 vx = cos( enemy.angle ) vy = sin( enemy.angle ) while checklength < distance-to-player if ( tilemap[ enemy.x + (checklength * vx), enemy.y + (checklength * vy) ] > 27 ) return blocked end if checklength :+ 1 wend return not-blocked end function Something like that anyway. If you can post code I can write and test in it would really help. |
| ||
my other similar idea that just came but didn't work was (by the way, both of these are in the excellent PointInViewAngle function) for(float i= 0 to distance) { if(tileMap[(x+(i*cos(angle)),y+(i*sin(angle))) > 27] print "view of player blocked" exit loop end if next UPDATE: this only really works if the enemy is facing to the right, Last edited 2012 Last edited 2012 Last edited 2012 |
| ||
Here's an update version of my code above, with an added line of sight test.. This only takes the centre of the enemy and player tiles into account though, but it should be close enough. It uses an implementation of Bresenhams line drawing algorithm from here: http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm You'll still have issues where it can 'see through' corners.. [*][.][.][.] [.][\][X][X] [.][X][\][.] [.][X][.][*] [*] can 'see' [*] even though there are walls [X] forming a corner between them. I'm afraid I'm too busy to help much more atm. :) Edit -- added line of sight drawing code... Last edited 2012 Last edited 2012 |