Sign in to follow this  
Byron

Unity To avoid or not avoid....?

Recommended Posts

I need some advice... I am writing a game in the vein of Space Harrier, tempest and Unity (I was lucky enough to have the dev build of Unity on my desk while working at Lionhead). Now, in this game things are moving rapidly towards the player and there are also path based aliens flying around. Currently the aliens don't avoid the 'static' obstacles so they just pass through them. Should I worry about this? Do I need to add obstacle avoidance for the aliens - I can put it in but it adds an extra complexity to the code and I am not convinced that the end results will end up looking any better. I could always do a Star Wars and have the Aliens explode on contact with the obstacles.... but I don't really want to do that. Any thoughts? All views welcome. You can see various videos of the game in action in my Gamedev journal: Gamedev Journal

Share this post


Link to post
Share on other sites
I think I am just a little worried that it could result in the alien bouncing all over the place as it attempts to avoid one obstacle just to get in the path of another since the obstacles are moving quite fast.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
But are the obstacles really moving? Sure, they appear to move because you're character is running, but probably they are really not moving. If it were me, and I like to keep things as simple as possible, I would just make it part of level design to make sure enemy paths don't collide with things.

Share this post


Link to post
Share on other sites
What you call obstacle avoidance wouldn't be that difficult in this case I think. You could give the alien a set maneuverability, that would determine at wich range from the static object they could still react, if the range is too small simple let them collide (and explode), it would add a great feeling of pace to the game I think.

Share this post


Link to post
Share on other sites
Kiome - I like that idea. It might give a lot of dynamic to the later levels.

"Anonymous Poster" - One design flaw I have is that the obstacles are on a random positional release so I can't really design a path to avoid them.

Share this post


Link to post
Share on other sites
Just a thought on the actual implementaion of the avoidance - or more correctly the reaction to the avoidance.

All the obstacles effectivley have a velocity of 0,-1,0 (Z is up in this world) so to avoid major problems I am thinking that I should only project along the X axis otherwise the Alien would be seen to be moving incredibly fast along Y as it will be trying to outrun the obstacle (which might be fun :))

Should the direction on X projection be random or should I try and make the Aliens a little more intelligent by looking ahead to see if the new position puts them into danger... mind you just having them avoid might bring out some emergent behaviour.

Share this post


Link to post
Share on other sites
Given that the alien spaceship has a maximum velocity, then for each obstacle, there exists a cone of positions which means certain death (because nothing can move out of that cone fast enough to avoid the obstacle. When moving your spacecraft around on a given iteration, consider these cones to be impassable obstacles (because you don't want to enter them).

To make movement smoother, use weights: the alien wants to go somewhere with a weight A, and wants to avoid obstacles with a weight Bi, where Bi is infinite when trying to move into a danger cone, and diminishes when moving away from the cone (it's zero if not on the path at all).

Share this post


Link to post
Share on other sites
Quote:

To make movement smoother, use weights: the alien wants to go somewhere with a weight A, and wants to avoid obstacles with a weight Bi, where Bi is infinite when trying to move into a danger cone, and diminishes when moving away from the cone (it's zero if not on the path at all).


This sounds interesting, can you expand upon this a bit more with an example? No, seriously - I would like to explore this a bit more.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
To make movement smoother, use weights: the alien wants to go somewhere with a weight A, and wants to avoid obstacles with a weight Bi, where Bi is infinite when trying to move into a danger cone, and diminishes when moving away from the cone (it's zero if not on the path at all).


Wow. If your gonna do that, why not take it one step further and make a cone of fire in front of the player?

Share this post


Link to post
Share on other sites
Quote:
Original post by Byron
This sounds interesting, can you expand upon this a bit more with an example? No, seriously - I would like to explore this a bit more.


Any artificial intelligence has objectives. In our case, the objectives would be surviving (not getting hit by a bullet or obstacle) and getting closer to the player (to shoot him). Now, the AI is about to move, and must choose the distance and velocity with which to move: a good approximation of the decision-making approach is to decide that for every position in the universe there exists an energy, and the AI must move towards the position of lowest energy. This is more or less equivalent to weighting the various directions by a danger factor (I'll explain this below).

Energy decreases when moving closer to the player, and increases when entering the path of an obstacle. Therefore, the energy can be computed as

E(x) = α Σ EOi(x) + β EP(x)

Where EOi is the energy field created by obstacle i and EP is the energy field created by the player. Then, we create some constraints on the energies:

  1. The player energy field decreases when moving towards the player. Several functions of radius are possible. Linear [Ep(r) = r] is quite simple: the AI will take the same risks to reach the player regardless of its position. Quadratic [Ep(r) = r²] will have the AI take greater risks the further away from the player. Exponential is even worse than quadratic. Logarithmic, on the contrary, causes the AI to take great risks when close to the player, but small risks when far from him. I believe Quadratic is the best approach here.

  2. The obstacle (and bullet) energy field has a non-lethality constraint: there is a cone out of which it is impossible to move once you've entered, because velocity is limited. Therefore, the energy field is infinite for an obstacle inside that cone.

  3. Even when not in lethal danger, the alien wants to stay out of the path of obstacles, unless of course the player is on the other side. Therefore, we decide that the closer the alien gets to the danger cone, the higher the energy is. Good functions for this are 1/r and 1/r², with r the distance to the cone. This has the neat property of converging to infinity when getting real close to the cone. I suggest using 1/r².


Therefore, the total energy field can be computed at a given point by finding the closest point on every obstacle cone, then summing up the inverses of these distances, and adding the square of the distance to the player.

Now, to get to the minimum of an energy field, the fastest way is to move along with the gradient. Since we've chosen our functions well, the gradient of the energy field is the sum of the gradients of the individual fields, which are: a vector that points towards the player (of length proportional to the distance), and a vector that points away from the closest point on a cone (of length inversely proportional to the distance). Adjust everything by α and β.

The naive approach is to say that the velocity of the alien is equal to a fraction of the energy field gradient. This causes problems when the gradient is very small (such as in the path of an obstacle). A better approach is to decide that the velocity has the same direction as the gradient, but has a constant magnitude that does not depend on the gradient. An even better and smoother approach is to decide that acceleration is equal to the gradient, and that the velocity is reduced every frame due to friction. This way, the AI will move towards the minimum, react violently to violent changes in energy (near obstacles) but keep a fluid trajectory with few abrupt changes in direction or velocity.

Now, if you look closely, we've taken the directions (move away from obstacles, towards player: the gradients of each field), weighted each of them by a factor that depends on the mission and a factor that depends on the distance to the objective, and used the resulting output to choose the direction. We now have an AI which moves away from obstacles if they get too close, and otherwise moves towards the player.

Share this post


Link to post
Share on other sites
This sounds similar to the Lenard-Jones potential function. I have considered using such a function in the past after reading about it and you are right in this case it does provide me with the decision making I need if I am to implement avoidance in the aliens.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this