Jump to content
  • Advertisement
Sign in to follow this  
Munchkin9

Unity Mathematical Issue

This topic is 2454 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!