Sign in to follow this  
Munchkin9

Unity Mathematical Issue

Recommended Posts

I have a fully working game using Missile Command like features in a spaceship duel system. Recently though I have decided to add AI to the game, mainly so that I don't need someone else to test new features and to make it more accessible (the game already requires xbox360 controllers). To start off I want to make the AI ships do a basic following task of the closest other ship around them (can be awfully pretty when two ships start oscillating around each other). Currently the ships both player and AI are instances of the same class, player ships acquiring inputs from the controllers, while the AI ships get their inputs from a AI class that has an instance for every AI. So simply put: the AI ships receive the same type of controls as player ships do (this might be important).

Now the current issue is that the AI ships follow just fine except in certain situations. I wish I had some pictures but I will try to explain: The AIs have to decide to turn left or right depending on their current direction (I have this) and the direction the target ship is (I have this as well). All this is calculated in radians on a 0 to 2(pi) range. Simply telling the ships to compare current directions and turning left if the target angle is greater than current angle and right if its less works fine if the ships are on the same side of (pi) (0 to pi and pi to 2(pi)). But if the angles are something like (in degrees for simplicity) 10 degrees for the current angle and 350 for desired angle the fastest way to turn is right, past the zero mark, but a computer does not realize this and instead sees that the desired angle is greater than the current angle is so it turns left. This is glaringly obvious in game as illogical movement.

I have tried a number of ways trying to get around this such as making exceptions and changing how the ranges are split (instead of 0 to 2pi I tried 0 to pi and -pi to 0) but nothing seems to fix this or at most makes a new angle where the AI acts weird.

So I'm stumped. I am asking the community to help me out, please.
-Munchkin9

Share this post


Link to post
Share on other sites
[code]
If ((GoalAngle - CurrentAngle) modulo 360) > 180 then
Decrease the current angle
else
Increase the angle
[/code]

You might also want to set the current angle to the goal angle if the difference is less than one timestep's move.

The right scale to use for the difference is from -pi to +pi but the angles can have anything because the difference is not affected.

Share this post


Link to post
Share on other sites
One of the things I do when I run into a problem like this is hard-code the numbers to numbers that I know will fail, then follow the debugger through the process . . . and soon you will discover the problem. You can also use and assert statement in the code to stop the debugger at a certain point.

Share this post


Link to post
Share on other sites
[quote name='Skywola' timestamp='1317492771' post='4868033']
One of the things I do when I run into a problem like this is hard-code the numbers to numbers that I know will fail, then follow the debugger through the process . . . and soon you will discover the problem. You can also use and assert statement in the code to stop the debugger at a certain point.
[/quote]

The problem with this is that I know the problem already just not how to fix it (which is rare).

@dawoodoz: what does modulo 360 do? because apart from that it is what I am doing.

Share this post


Link to post
Share on other sites
Why are you using angles for this? These problems do not exists when working with vectors. In this case, you can for example use[url="http://mathworld.wolfram.com/PerpDotProduct.html"] this operation[/url] to know if the direction of the ship is at the right or left of the current ship querying the sign of the result. The only special case you may want to consider is when the two ships have opposite directions.

Share this post


Link to post
Share on other sites
[quote name='apatriarca' timestamp='1317640977' post='4868535']
Why are you using angles for this? These problems do not exists when working with vectors. In this case, you can for example use[url="http://mathworld.wolfram.com/PerpDotProduct.html"] this operation[/url] to know if the direction of the ship is at the right or left of the current ship querying the sign of the result. The only special case you may want to consider is when the two ships have opposite directions.
[/quote]

I'll admit I mainly don't use vectors because I don't know too much about them. My math education barely, and I mean barely, covered vectors, it was more of a passing mention. As far as I understand though I'm not sure vectors would help me too much here. I have a lot of things relying on the angle of the ships compared to each other, their "aimer's" (they can not shoot in a cone behind them), and their speeds (heading towards or away from the planet). However, it might be that I simply programed it so that all these things rely on angles when they could have relied on vector. No matter the case, being this far into the program I would prefer to just stick to angles. I'm going to try and use the suggested code and see if that works.

Share this post


Link to post
Share on other sites
I agree that if you are quite far in the development of your game it's probably better to continue to work with angles, but I suggest to consider working with them in future projects since the code using vector math is usually shorter, faster and cleaner than the equivalent code using angles. A good knowledge of vector math is also needed to work in 3D. I also suggest to use radians instead of degrees since trigonometric functions usually use them.

Share this post


Link to post
Share on other sites
hrmm
if the angle of travel is 10 degrees and the desired angle is 350. there is a simple comparison.

if the angle desired is more than 180 larger than the original travel angle, turn right. otherwise turn left.

make sense?

basically

if (((desired angle - travel angle) > PI) OR (desired angle < travel angle)) turn right.
else turn left.

Or to simpliy more,
if (desired angle != travel angle)
{
if (desired angle- travel Angle < PI ) turn left
else turn right
}


I think this should work... If the desired angle and travel angle are PI apart then the ship will always turn right to correct. (special case of needing to change direction by 180 degrees is handled here)

Share this post


Link to post
Share on other sites
I would like to thank everyone here. I have solved the problem. It was actually more complex than I initially thought: I had not altered all my angles the same way so they weren't matching up in the same range causing some strange issues. As well as other problems on top of that. All to say, I have now solved it thanks to the help I received here, thank you.

-Munchkin9

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  

  • Forum Statistics

    • Total Topics
      628294
    • Total Posts
      2981876
  • Similar Content

    • By CpawsMusic
      Hey there! My name is Cpaws and I compose music, sound effects and overall sound design for video games and film. I'm looking to work on a horror game mainly for fun and as an addition to my portfolio. I've used Unity & Wwise before for audio implementation.
      If you're interested in working together, don't hesitate to contact me at cpawsmusic@gmail.com
      Here's a few demo reels of my past projects: 
      Here's some snippets of some quick game music I've composed: https://soundcloud.com/cpawsmusic/sets/cpaws-video-game-film-music
      Here's my portfolio/website: https://CpawsMusic.com/
      E-mail: cpawsmusic@gmail.com
    • By muhamad rabee
      My first mobile game made by unity

      iphone: https://itunes.apple.com/us/app/aa-countdown/id1314223584?ls=1&mt=8

      android: https://play.google.com/store/apps/details?id=com.mr.AACountDown

      I appreciate every suggestion
    • By Simplepg
      https://play.google.com/store/apps/details?id=simple.gplay.GamesInABox
    • By Simplepg
      https://play.google.com/store/apps/details?id=simple.gplay.GamesInABox
    • By ForgedInteractive


      Who We Are
      We are Forged Interactive, a small team of like-minded game developers with the sole purpose of making games we love! We're a team of artists, animators, programmers, level designers, writers, composers, producers, and other creative minds. We want to make games that you, the modern gamer want to play! We hope to build a community that enjoys our games as much as we love creating them. With your feedback and support we will be able to achieve that.

      About the Game
      GAME NAME is a fun, action-packed army builder with unique characters, challenges and engaging levels. Set forth on an adventure to protect friends, family and countrymen from new adversaries. Once defeated your enemies turn coat and join you in your adventures. Players can enjoy a range of troops and abilities based on their gameplay style which become more important as maps introduce more challenging terrain, enemies and bosses. Strong orc knights, dangerous shamans, and even a dragon are out on the prowl. Knowing when to fight and when to run, and how to manage your army is essential. Your actions alone decide the fate of this world.

      Previous Work by Team
      Although we are working towards our first game as a team, our team members themselves have past experience in the industry.
      This includes members who have worked on titles including:
      Final Fantasy Kingsglaive, FIFA, Xcom 2 and Civilization.

      Who are we looking for? 3D Modellers Concept Artists Marketing Specialists Level Designer

      What do we expect? Reference work or portfolio. Examples what have you already done and what projects you have worked on academic or otherwise. The ability to commit to the project on a regular basis. If you are going on a two-week trip, we don't mind, but it would be good if you could commit 10+ hours to the project each week. Willingness to work with a royalty based compensation model, you will be paid when the game launches. Openness to learning new tools and techniques
      What can we offer? Continuous support and availability from our side. You have the ability to give design input, and creative say in the development of the game. Shown in credits on websites, in-game and more. Insight and contacts from within the Industry.
      Contact
      If you are interested in knowing more or joining. Please email or PM us on Skype. Myself or Colin will reply to you within 48 hours.

      E-mail: Recruitment@ForgedInteractive.com
      Skype: ForgedInteractive

      Regards,
      David and Colin

      Follow us on:

      Facebook: https://www.facebook.com/ForgedInteractive/
      Twitter: @ForgedInteract
      Youtube: https://www.youtube.com/channel/UCpK..._as=subscriber
      Reddit: https://www.reddit.com/user/Forged_Interactive/
  • Popular Now