Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (855)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved] Smooth transition between angles  (Read 3918 times)
0 Members and 1 Guest are viewing this topic.
Offline 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);
      }
     
   }
Offline 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.
Offline 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?
     
   }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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)
Offline 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.  ?? 
Offline 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
Offline 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?
Offline 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);
      }
Offline 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...
Offline 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 Sad
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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.
Offline 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);
      }
     
   }
Offline 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.
Offline 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! 
Offline Opiop
« Reply #14 - Posted 2014-03-10 02:43:11 »

That's kind of standard for most graphics environments...
Offline 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;
Offline 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.
Offline Abuse

JGO Ninja


Medals: 70


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  
 
 

 
EgonOlsen (1573 views)
2018-06-10 19:43:48

EgonOlsen (1657 views)
2018-06-10 19:43:44

EgonOlsen (1145 views)
2018-06-10 19:43:20

DesertCoockie (1573 views)
2018-05-13 18:23:11

nelsongames (1174 views)
2018-04-24 18:15:36

nelsongames (1664 views)
2018-04-24 18:14:32

ivj94 (2421 views)
2018-03-24 14:47:39

ivj94 (1631 views)
2018-03-24 14:46:31

ivj94 (2719 views)
2018-03-24 14:43:53

Solater (884 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!