• Advertisement
Sign in to follow this  

Unity Mathematical Issue

This topic is 2336 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 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
Advertisement

If ((GoalAngle - CurrentAngle) modulo 360) > 180 then
Decrease the current angle
else
Increase the angle


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

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.


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 this operation 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

Why are you using angles for this? These problems do not exists when working with vectors. In this case, you can for example use this operation 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.


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
If you use unit-length complex numbers instead of angles, the whole thing is trivial.
rotation = goal / current;

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 devbyskc
      Hi Everyone,
      Like most here, I'm a newbie but have been dabbling with game development for a few years. I am currently working full-time overseas and learning the craft in my spare time. It's been a long but highly rewarding adventure. Much of my time has been spent working through tutorials. In all of them, as well as my own attempts at development, I used the audio files supplied by the tutorial author, or obtained from one of the numerous sites online. I am working solo, and will be for a while, so I don't want to get too wrapped up with any one skill set. Regarding audio, the files I've found and used are good for what I was doing at the time. However I would now like to try my hand at customizing the audio more. My game engine of choice is Unity and it has an audio mixer built in that I have experimented with following their tutorials. I have obtained a great book called Game Audio Development with Unity 5.x that I am working through. Half way through the book it introduces using FMOD to supplement the Unity Audio Mixer. Later in the book, the author introduces Reaper (a very popular DAW) as an external program to compose and mix music to be integrated with Unity. I did some research on DAWs and quickly became overwhelmed. Much of what I found was geared toward professional sound engineers and sound designers. I am in no way trying or even thinking about getting to that level. All I want to be able to do is take a music file, and tweak it some to get the sound I want for my game. I've played with Audacity as well, but it didn't seem to fit the bill. So that is why I am looking at a better quality DAW. Since being solo, I am also under a budget contraint. So of all the DAW software out there, I am considering Reaper or Presonus Studio One due to their pricing. My question is, is investing the time to learn about using a DAW to tweak a sound file worth it? Are there any solo developers currently using a DAW as part of their overall workflow? If so, which one? I've also come across Fabric which is a Unity plug-in that enhances the built-in audio mixer. Would that be a better alternative?
      I know this is long, and maybe I haven't communicated well in trying to be brief. But any advice from the gurus/vets would be greatly appreciated. I've leaned so much and had a lot of fun in the process. BTW, I am also a senior citizen (I cut my programming teeth back using punch cards and Structured Basic when it first came out). If anyone needs more clarification of what I am trying to accomplish please let me know.  Thanks in advance for any assistance/advice.
    • 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.
       
  • Advertisement