Author Topic: turnTo(SimpleVector)  (Read 1424 times)

Offline AGP

  • quad
  • ******
  • Posts: 1526
    • View Profile
turnTo(SimpleVector)
« on: January 30, 2012, 07:37:05 pm »
I have a ship, and it follows a dummy object directly in front of it. When it reaches a certain distance from the center of the world, I would like to make it turn directly towards the player's ship. For that, I would like to calculate the degree between a direction vector composed of the ship's transformed center and its dummy's transformed center and the player's ship's transformed center. How would I go about doing that?

I've written two simple methods, a turnTo which simply calculates the number of iterations (in x degree segments) that the ship will turn on its Y axis, and a turnAround which turns the number of iterations that turnTo calculated (as follows). So how do I implement turnTo()?

Code: [Select]
      protected void turnAround() { //RUNS THE TURN-AROUND LOOP FOR THE ENEMY FIGHTER
if (!turning && i == numberOfTurnSegs) {
      turning = true;
      i = 0;
}
tieFighter.rotateAxis(enemyShip.getYAxis(), (float)Math.toRadians(10));
gameInstance.moveTowards(enemyShip, tieGuide.getTransformedCenter(), .1f); //THIS MOVES IT FORWARD
i++;

if (i == numberOfTurnSegs) {
      gameInstance.moveTowardsMinusY(enemyShip, tieGuide.getTransformedCenter(), 1f); //GET IT AWAY FROM SKY SO NO MORE COLLISION
      turning = false;
      lastTurn = System.currentTimeMillis();
}
      }

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: turnTo(SimpleVector)
« Reply #1 on: January 30, 2012, 07:50:24 pm »
Pfffft...a degree between three vectors?...i don't really get it...

Offline AGP

  • quad
  • ******
  • Posts: 1526
    • View Profile
Re: turnTo(SimpleVector)
« Reply #2 on: January 30, 2012, 07:52:08 pm »
OK, the player's ship also follows a dummy. But the enemy ship has a center and a direction, and I would like for the enemy's direction to point to the player's center.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: turnTo(SimpleVector)
« Reply #3 on: January 30, 2012, 08:40:17 pm »
I'm not sure if i really got it...so you want to make the enemy turn towards the player based on some condition and the actual problem is, that this has to happen not immediately but in a <some unknown> number of steps  ???

Offline AGP

  • quad
  • ******
  • Posts: 1526
    • View Profile
Re: turnTo(SimpleVector)
« Reply #4 on: January 30, 2012, 09:17:53 pm »
Exactly. Say an iteration of the game loop will rotate the enemy by 10 degrees (this will vary, of course, based on the amount of time elapsed since the last frame). How many of these iterations will I need? Come to think of it, just getting the total amount degrees of rotation would be even better than counting iterations. :- )

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: turnTo(SimpleVector)
« Reply #5 on: January 30, 2012, 09:19:25 pm »
Using the Matrix.interpolate()-method isn't an option then...?

Offline AGP

  • quad
  • ******
  • Posts: 1526
    • View Profile
Re: turnTo(SimpleVector)
« Reply #6 on: January 30, 2012, 09:24:56 pm »
I don't know, how would I use that in this case? Again, all I need is the total rotation (in either degrees or radians) that the enemy ship will have to do in order to fly at the player ship (or even just to where the player's ship was when it begain rotating!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: turnTo(SimpleVector)
« Reply #7 on: January 30, 2012, 09:38:07 pm »
The total rotation should be getRotationMatrix() of the direction vector between the two. I thought that interpolate() might help to get a smooth transition between the two.

Offline AGP

  • quad
  • ******
  • Posts: 1526
    • View Profile
Re: turnTo(SimpleVector)
« Reply #8 on: January 30, 2012, 09:43:48 pm »
OK, but what's "the direction vector between the two?" I have three (or four if you want to count the player's dummy's center) SimpleVectors. How do I get a Matrix from that?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: turnTo(SimpleVector)
« Reply #9 on: January 30, 2012, 09:46:59 pm »
Not sure...i'm somewhere lost between ships and dummy objects....isn't the direction vector just the vector between the two positions? It might be needed to adjust it after a few iterations though...

Offline AGP

  • quad
  • ******
  • Posts: 1526
    • View Profile
Re: turnTo(SimpleVector)
« Reply #10 on: January 30, 2012, 09:54:15 pm »
I have four SimpleVectors: the two ships' centers and the dummy objects that each ship follows (chases). So I have two SimpleVectors per ship, essentially one for where the ship is and another for where it's headed. I want to turn the enemy ship towards the player's ship (no need to compensate for the player ship's movement).

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: turnTo(SimpleVector)
« Reply #11 on: January 30, 2012, 09:59:34 pm »
Something like...

Code: [Select]
Simplevector dir=enemy.getTranslation();
dir.sub(player.getTranslation());
enemy.setRotation(dir.getRotationMatrix());
...
enemy.translate(enemy.getRotation().getZAxis());

Of course, this doesn't take into account that the player actually moves...that what i meant with "It might be needed to adjust it after a few iterations though"...

Offline AGP

  • quad
  • ******
  • Posts: 1526
    • View Profile
Re: turnTo(SimpleVector)
« Reply #12 on: January 30, 2012, 10:09:45 pm »
Thanks a lot. I'll try one with interpolate now to smooth the turn.