help in a 'stealth' type game

Started by guv, Sat 29/08/2009 19:47:57

Previous topic - Next topic

guv

well...not really stealth...i have an idea, when your a spy and you have a usual 'adventure' game style gameplay, and the other is a style which mimics 'the classroom' if anyone has ever played it.

i would like to know how to tell the engine when the characters get 'seen' by the enemy?

what i mean is i want a triangular area in front of the enemy and when the player walks into it, they get caught.


any help appreciated,
an example would make me as happy as a soldier when he recieves his funny hat.

Akatosh

#1
A really really cheap way would be to use a character for this. Like, make one that's essentially a giant triangle (representing the guards' cone of sight), set it to 100% transparency, and always have it hover in front of the enemy, with the small end pointing towards the guard's head. Then you can just check if the player and the cone character collide, and if they do, have unpleasant things happen. Of course, you need to tell the engine to ignore transparent areas for purpose of collision detection; I think there's an option in the main menu to do that.

This is the easiest way I can think of right now. Anything else would involve some checking of character x and y values. You can simplify it somewhat via use of vectors, though.

guv

could you explain vectors to me please?

Khris

Alright, I'll try to be as concise as possible:

In 2D space, a vector is a combination of two numbers, usually x and y. It represents a direction or movement along the x and y axis. The length of the vector is the square root of the sum of the squares of x and y:
v.Length = Math.Sqrt( v.x^2 + v.y^2 );
To normalize a vector, both its x and y are divided by its length. The result is called a unit vector, its length is 1. Usually, directions are represented by unit or normalized vectors.

Vector (x;y) turned 90 degrees
- to the left: (-y;x)
- to the right: (y;-x)
180 degrees: (-x;-y)

On to the enemy's field of view:
Position: (ex;ey)
Viewing direction (dx;dy) (normalized)


For my example, I'm going to use a field of view where A_B = E_L2. This is pretty similar to e.g. Commandos.

First we need to decide how long E_L2 is going to be. I'll say l = 100.
Given an angle a, the direction vector d is simply ( cos(a);sin(a) ).
So in order to get A and B, we need to move from E in direction d by 100, then 90 degrees from that by 50 to either side.

L2 = E + l*(dx;dy)
A = L2 + (l/2)*(-dy;dx)
B = L2 + (l/2)*(dy;-dx)

Now that we have the three corners of the triangle, we can check whether the player is inside it.
To do that, we pick a side of the triangle, then check whether the player is on the same side as the third corner. Only if this is true for all three sides, the player is inside.


E.g., is the player P on the same side as E in respect to AB?

I'm sure there's a proper, vector-based way to do this, maybe someone can help out here?
Until then, I'll calculate the function that produces the line going through A and B, then put px and ex in that and check if f(ex)-ey and f(px)-py have the same sign. The downside is that I have to check for the special case of ax = bx to avoid a division by zero error.

ax != bx:

f = m*x + t
m = (by-ay)/(bx-ax)  // m

ay = m*ax + t
t = ay - m*ax  // t

r1 = m*ex + t - ey
r2 = m*px + t - px

ax = bx:

r1 = ax-ex;
r2 = ax-px;

Do r1 and r2 have the same sign?

r1*r2 >= 0: yes
r1*r2 < 0: no

If you have problems with turning all that into code, I can help you out there, but atm I'm still curing my hangover :)

Wonkyth

Depending on your understanding maths, a website you might find useful is Multivariable Calculus Online (http://math.etsu.edu/Multicalc/)
"But with a ninja on your face, you live longer!"

guv

thanks, i realise this is probably a bit of an ambitious project for my first game :P

i think i understand vectors, but im still fairly new at this so i dont understand how i could manipulate it to be code?

any help appreciated again. thanks

SMF spam blocked by CleanTalk