turnTo(SimpleVector)

Started by AGP, January 30, 2012, 07:37:05 PM

Previous topic - Next topic

AGP

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()?


      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();
}
      }

EgonOlsen

Pfffft...a degree between three vectors?...i don't really get it...

AGP

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.

EgonOlsen

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  ???

AGP

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. :- )

EgonOlsen

Using the Matrix.interpolate()-method isn't an option then...?

AGP

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!

EgonOlsen

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.

AGP

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?

EgonOlsen

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...

AGP

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).

EgonOlsen

Something like...


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"...

AGP

Thanks a lot. I'll try one with interpolate now to smooth the turn.