Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (784) Games in Android Showcase (234) games submitted by our members Games in WIP (858) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 [Solved] Smooth transition between angles  (Read 4395 times) 0 Members and 1 Guest are viewing this topic.
songangel

Junior Devvie

Medals: 1
Exp: 4 years

 « Posted 2014-03-10 01:37:36 »

Hey guys,

I trying to create some simple AI for my enemy class.  I have an enemy moving 0 degrees initially, and I want to make him move smoothly to angle 270 (straight down in my coordinate system).  How can this be done?

With my current code, he ends up "taking the long way around" so to speak but it works fine for transitioning from 180 to 270 (left to down).  I'm looking for something that will transition smoothly no matter what the chosen direction is and preferably so he would take the shortest route to get there.

This seems like it should be simple, but it's driving me nuts!  I am posting my code below which doesn't quite work.  In the below example, the variable direction is fixed at 270.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `public void doBehavior(GameObject g)   {      int currentDirection = g.getDirection();            // Smooth transition from current direction to new                  if(currentDirection < direction)      {         g.setDirection(currentDirection + 1);      }            else if(currentDirection > direction)      {         g.setDirection(currentDirection - 1);      }         }`
LiquidNitrogen
 « Reply #1 - Posted 2014-03-10 01:41:46 »

you could calculate the clockwise and anticlockwise difference between currentdirection and direction. taking into account that it will go from 360deg to 0deg either way. if the result is < 0 then add 360, if its > 360 subtract 360.

then which ever is the smaller difference is which way to turn.
songangel

Junior Devvie

Medals: 1
Exp: 4 years

 « Reply #2 - Posted 2014-03-10 01:54:51 »

Ok here is what I have so far:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `   public void doBehavior(GameObject g)   {      int currentDirection = g.getDirection();      int diff1 = currentDirection - direction;      int diff2 = direction - currentDirection;            if(diff1 < 0) diff1 += 360;      else if(diff1 > 360) diff1 -= 360;         if(diff2 < 0) diff2 += 360;      else if(diff2 > 360) diff2-= 360;            // Now what do I do with these numbers?         }`
LiquidNitrogen
 « Reply #3 - Posted 2014-03-10 01:56:22 »

if diff1 < diff2 then turn left, else turn right (it could be opposite though ;P)
songangel

Junior Devvie

Medals: 1
Exp: 4 years

 « Reply #4 - Posted 2014-03-10 02:00:23 »

I get what you're saying, but my movement is based on sin and cos, so how would this look in code?  The only way I can see is to either add or subtract from the currentDirection which works if he's moving left but not for moving right.  ??
LiquidNitrogen
 « Reply #5 - Posted 2014-03-10 02:02:53 »

what causes it to not work? i think you need to do another check after turning, to make sure your current direction hasnt gone below 0 or above 360
songangel

Junior Devvie

Medals: 1
Exp: 4 years

 « Reply #6 - Posted 2014-03-10 02:04:23 »

In your last post you said: if diff1 < diff2 then turn left, else turn right (it could be opposite though ;P).  But what do you mean by turn left?  Do I add to the currentDirection?
LiquidNitrogen
 « Reply #7 - Posted 2014-03-10 02:07:22 »

if you change part of your original code to this, it should be close to working

Quote
if(diff1 < diff2)
{
g.setDirection(currentDirection + 1);
}

else if(diff1 > diff2)
{
g.setDirection(currentDirection - 1);
}
songangel

Junior Devvie

Medals: 1
Exp: 4 years

 « Reply #8 - Posted 2014-03-10 02:10:09 »

Hmmm this makes him turn and go up.  I tried reversing the plus and minus but he just does a loop.  God I wish I'd studied trig in school...
LiquidNitrogen
 « Reply #9 - Posted 2014-03-10 02:20:23 »

yeah i cant remember trig so well right now. i just looked up some old code which does what you want (not in java) but its so poorly written i cant understand it, all the variables are a,b,c, etc
songangel

Junior Devvie

Medals: 1
Exp: 4 years

 « Reply #10 - Posted 2014-03-10 02:23:49 »

Thanks for trying.  I guess I'll keep playing around and see what I can come up with.
songangel

Junior Devvie

Medals: 1
Exp: 4 years

 « Reply #11 - Posted 2014-03-10 02:28:37 »

After some playing around, I might have something that works.  At least for transitioning from 0 to 270 and then from 180 back to 270.  Haven't tried other angles yet but here it is for those that are interested.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `public void doBehavior(GameObject g)   {      int currentDirection = g.getDirection();             if(currentDirection == 0) currentDirection = 360;            if(currentDirection < direction)      {         g.setDirection(currentDirection + 1);      }            else if(currentDirection > direction)      {         g.setDirection(currentDirection - 1);      }         }`
LiquidNitrogen
 « Reply #12 - Posted 2014-03-10 02:32:00 »

oh i see. the angle range is -90 to 270 is it?? i was assuming the range is from 0-360.
songangel

Junior Devvie

Medals: 1
Exp: 4 years

 « Reply #13 - Posted 2014-03-10 02:42:23 »

Yeah the one thing I hate about libgdx.  Getting used to that fact that Y is up!
Opiop
 « Reply #14 - Posted 2014-03-10 02:43:11 »

That's kind of standard for most graphics environments...
LiquidNitrogen
 « Reply #15 - Posted 2014-03-10 02:46:57 »

if this doesnt help, im going back to sleep.
 1  2  3  4  5 `      if(diff1 < -90) diff1 += 360;      else if(diff1 > 270) diff1 -= 360;         if(diff2 < -90) diff2 += 360;      else if(diff2 > 270) diff2-= 360;`
songangel

Junior Devvie

Medals: 1
Exp: 4 years

 « Reply #16 - Posted 2014-03-10 03:03:14 »

Rest well sir.  What I have is working flawlessly for my little space shooter project.  Like I said, I haven't tried every angle yet, but it seems to work for what I need.  Marking this one solved.
Abuse

JGO Ninja

Medals: 71

falling into the abyss of reality

 « Reply #17 - Posted 2014-03-10 03:15:25 »

If you kept everything as vectors, it'd be a lot neater.
You'd just examine the sign of the determinant (cross product) of the 2 vectors to know if one is to the left, or right of the other.

pseudo code something like:
 1  2  3  4 `determinant = (b.x - a.x)*(c.y - a.y) - (b.y - a.y)*(c.x - a.x);if(determinant>0) turnLeft();else if(determinant<0) turnRight();else fire(); `
Pages: [1]
 ignore  |  Print

 hadezbladez (1630 views) 2018-11-16 13:46:03 hadezbladez (645 views) 2018-11-16 13:41:33 hadezbladez (1629 views) 2018-11-16 13:35:35 hadezbladez (336 views) 2018-11-16 13:32:03 EgonOlsen (2680 views) 2018-06-10 19:43:48 EgonOlsen (2946 views) 2018-06-10 19:43:44 EgonOlsen (1643 views) 2018-06-10 19:43:20 DesertCoockie (2354 views) 2018-05-13 18:23:11 nelsongames (2258 views) 2018-04-24 18:15:36 nelsongames (2952 views) 2018-04-24 18:14:32
 Deployment and Packagingby philfrei2019-02-17 20:25:53Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org