• Advertisement
Sign in to follow this  

Unity To avoid or not avoid....?

This topic is 4141 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
I think that adding the avoidance would be worth it. It would look a bit weird to see the aliens passing through solid objects :P.

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
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Manuel Berger
      Hello fellow devs!
      Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.
      My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

      In Pseudo-Code:
      angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
      index = (int) (angle / (PI / 5));
      PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

      Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

      What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

      I can't get my head around it. Any suggestions? Is the whole approach doomed?

      Thank you in advance for any input!
       
    • By Yosef BenSadon
      Hi , I was considering this start up http://adshir.com/, for investment and i would like a little bit of feedback on what the developers community think about the technology.
      So far what they have is a demo that runs in real time on a Tablet at over 60FPS, it runs locally on the  integrated GPU of the i7 . They have a 20 000 triangles  dinosaur that looks impressive,  better than anything i saw on a mobile device, with reflections and shadows looking very close to what they would look in the real world. They achieved this thanks to a  new algorithm of a rendering technique called Path tracing/Ray tracing, that  is very demanding and so far it is done mostly for static images.
      From what i checked around there is no real option for real time ray tracing (60 FPS on consumer devices). There was imagination technologies that were supposed to release a chip that supports real time ray tracing, but i did not found they had a product in the market or even if the technology is finished as their last demo  i found was with a PC.  The other one is OTOY with their brigade engine that is still not released and if i understand well is more a cloud solution than in hardware solution .
      Would there  be a sizable  interest in the developers community in having such a product as a plug-in for existing game engines?  How important  is Ray tracing to the  future of high end real time graphics?
    • By bryandalo
      Good day,

      I just wanted to share our casual game that is available for android.

      Description: Fight your way from the ravenous plant monster for survival through flips. The rules are simple, drag and release your phone screen. Improve your skills and show it to your friends with the games quirky ranks. Select an array of characters using the orb you acquire throughout the game.

      Download: https://play.google.com/store/apps/details?id=com.HellmodeGames.FlipEscape&hl=en
       
      Trailer: 
       
    • By khawk
      Watch the latest from Unity.
       
    • By GytisDev
      Hello,
      without going into any details I am looking for any articles or blogs or advice about city building and RTS games in general. I tried to search for these on my own, but would like to see your input also. I want to make a very simple version of a game like Banished or Kingdoms and Castles,  where I would be able to place like two types of buildings, make farms and cut trees for resources while controlling a single worker. I have some problem understanding how these games works in the back-end: how various data can be stored about the map and objects, how grids works, implementing work system (like a little cube (human) walks to a tree and cuts it) and so on. I am also pretty confident in my programming capabilities for such a game. Sorry if I make any mistakes, English is not my native language.
      Thank you in advance.
  • Advertisement