Be a Supporter!

Hide Behind Objects In Stealth Game

  • 346 Views
  • 7 Replies
New Topic Respond to this Topic
Kwing
Kwing
  • Member since: Jul. 24, 2007
  • Offline.
Forum Stats
Member
Level 45
Game Developer
Hide Behind Objects In Stealth Game 2011-01-25 02:51:36 Reply

If you have an overhead game like The Classroom...

How would you code a game like that so you can hide behind objects if an object is between the player and the person looking around? I'm assuming this would require some script involving atan2 and maybe BitmapData, both of which I have a sketchy knowledge of. In fact, being somewhat of an amateur I don't expect to get the explanation but could someone at least prod me in the right direction?


If I offer to help you in a post, PM me to get it. I often forget to revisit threads.
Want 180+ free PSP games? Try these links! - Flash - Homebrew (OFW)

Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to Hide Behind Objects In Stealth Game 2011-01-25 08:37:05 Reply

You need to use some form of ray casting. You don't need trig, but you could use bitmaps, and there are many ways to approach this problem.

The way that would probably be easiest and work well enough for your needs, is to simply step along the ray in small increments and use hitTestPoint with shapeflag enabled against a container that contains all the objects that can potentially block the ray.

quick sample code (untested):

function raycast(container:DisplayObjectContainer, sx:Number, sy:Number, ex:Number, ey:Number):Boolean{
	var dx:Number = ex-sx;
	var dy:Number = ey-sy;
	
	var step:Number = 5; // change this accordingly to your requirements
	var length:Number = Math.sqrt(dx*dx + dy*dy);
	var numSteps:int = length/step;
	
	// normalize delta down to our step
	dx *= step/length;
	dy *= step/length;
	
	for(var i:int = 0; i < numSteps; i++){
		if (container.hitTestPoint(sx, sy, true)){
			return false;
		}
		sx += dx;
		sy += dy;
	}
	return true;
}

A smaller delta step along the ray would be more accurate but slower, and vice versa. There are many other faster ways of doing this of course that use entirely different schemes, but this should be good enough for your purposes.


#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}

BBS Signature
xKiRiLLx
xKiRiLLx
  • Member since: Feb. 8, 2007
  • Offline.
Forum Stats
Member
Level 09
Game Developer
Response to Hide Behind Objects In Stealth Game 2011-01-25 08:50:06 Reply

Tutorial on dynamic areas of sight

PSvils
PSvils
  • Member since: Feb. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Game Developer
Response to Hide Behind Objects In Stealth Game 2011-01-27 15:41:20 Reply

Stepping is a BAD idea. I even saw Bubble Tanks 3 do this with their Ray Gun...disappointing. Doing a for loop can be slow as hell when you have many objects on the screen. Better would be to do proper raycasting, via math.
It's not even that hard...

http://local.wasp.uwa.edu.au/~pbourke/ge ometry/lineline2d/

That will help you setup basic line-line intersection. Search up Line-Circle/Distance to point from line or something.
I have a lighting engine setup right now in my upcoming horror/action game, which is fairly fast (still looking at some clever tricks to optimize...) that uses this. Basically just make a line between the enemy and player, and check all objects with the line (if all objects can be described by lines and circles).

Bleh, I like talking about collision detection...somehow I'm interested in it. I'll be releasing a library sometime in the future which will have lots of basic math functions like this. Not at all to the extent of a game engine, cause I hate those, but something giving you the basic setup to make your own engine...

P.

tri-circle
tri-circle
  • Member since: Jun. 4, 2009
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Hide Behind Objects In Stealth Game 2011-01-28 11:59:18 Reply

The easiest way would be just to convert the object you want the character to hide by hind to a movie clip and then change the script to test to see if the object is touching the enemies line of vision and make it so the player can only be seen if there are no objects touching the line of vision.


Many of you think your the real deal. Some of you may be right. But it does not matter. It will never matter. Because I am much more...

BBS Signature
Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to Hide Behind Objects In Stealth Game 2011-01-28 13:15:30 Reply

At 1/27/11 03:41 PM, PSvils wrote: Stepping is a BAD idea. I even saw Bubble Tanks 3 do this with their Ray Gun...disappointing. Doing a for loop can be slow as hell when you have many objects on the screen. Better would be to do proper raycasting, via math.
It's not even that hard...

Sure. But stepping is almost always fast *enough* as long as it isn't being abused. And more objects isn't even a problem because flash merely treats whatever is being tested as a bitmap, and so you can have an arbitrary number of arbitrarily complex and moving graphics (including animated tweens) within the container being tested, without really being much slower.

So not only is it ridiculously easy to implement-- it supports complex shapes and broad-phase is instantly automagically taken care of. Unless he has dozens of entities that need to raycast against dozens of mathematically defined objects (which I rather doubt is this case), stepping should be more than enough for his purposes.


#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}

BBS Signature
PSvils
PSvils
  • Member since: Feb. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Game Developer
Response to Hide Behind Objects In Stealth Game 2011-01-28 16:50:13 Reply

I guess start with the simplest bitmap method, but in general I just like having max speed and max accuracy, which math would give you (so if he has thin walls, he might step over them and it might not work).

What I learned the hard way, was just do it. No matter what people tell you is the best way, just do it, and optimize later. I'm finally getting close to the alpha stage with my project, as soon as I told myself to let go from optimizing everything, usually it's not worth it.

So okay, do the bitmap method RedShift suggested, but I still recommend math, cause it's awesome :)

P.