Object rotation

Hey,

    I am creating a 3d first person shooter game which will have AI bots, how can i get the models of the bots to rotate? Like, it only moves with the models facing in one direction and i just wana no how do i get the model to turn or rotate to the direction its is moving, ie. if it turns left i also want it to face left…



Thanks for ur help

You could try storing the previous location, subtract it from the new location to get a direction vector. Then use the angle of the direction vector to set the orientation of the model.



There might be other ways as well. I assume you already have some sort if AI controller moving the model around?

nymon's suggestion is pretty clearly illustrated in the O'Reilly book Killer Game Programming in Java if you can pick that up.  It shows a simple behavior that allows one object to "chase" another (VERY basic) and it does exactly as nymon says, take current, subtract new, generate angle of rotation from direction vector, rotate, and move "forward" in direction of target.  The book is also on Safari if you have a membership.

TestSpatialLookAt might also be useful camera uses it to track a moving box

Ok, i kind of get it, get the current and previous location of the bot or the player?? sorry if question is a bit stupid, then i can use something like this to set its rotation??



Quaternion rot = new Quaternion();

this might interest yas, i found a method in the Java doc that allows the bot to rotate to look in the direction of a vector



public void lookAt(Vector3f position,

                  Vector3f upVector)



its from the spatial class

Go here:  Chapter 18. 3D Sprites



And download the source.  Look for the calcTurn() method in the Alien sprite.  It's doing what you want - though it is a very basic implementation.


Oh yes, that lookAt() should work for you, just pass it the direction vector you calculate.

ya, it works grand, thanks for ur help lads

glad to be of help  :slight_smile:

ok, i may need a bit more help after all, what is happening is that the bot keeps moving away from me and not towards me, and it doesnt seem to be rotating towards me. Should i have my method called in a certain position in the update or will any position do?



i determine the current location of the bot and the player to calculate the direction i want the bot to go in, below is the code im using, any ideas???





public void botMovement(Node player, Node bot, TerrainPage terrainPage , Vector3f prevLoc , Vector3f botLoc)

    {

    //prevLoc.set(player.getWorldTranslation());

   

    //botLoc.set(fighter.getWorldTranslation());

       

        botHeight = terrainPage.getHeight(botLoc);

       

        bot.lookAt(prevLoc,new Vector3f(0, /botHeight-1.0f/1, 0));

       

        //Vector3f direction = new Vector3f();

        direction = player.getWorldTranslation().subtract(bot.getWorldTranslation());

        botpos = bot.getWorldTranslation();

        botpos.addLocal(direction.mult(lifeTime * botSpeed));

        bot.setLocalTranslation(botpos);

       

   

   

   

   

    }

I think your problem may be with the lookAt(). Your telling it to look the direction of prevLoc but from the perspective of the origin. The prevLoc vector is not a direction, just a location. See what I'm saying? Move the look at call down a few lines and give it your direction vector instead. Let me know if that works.

Mmmm… let

I have tried both the ideas that yas gave me above, but it still moves away from me, i even tried add the bot and subtratcing the player vector from the bot vector but still no luck, i'll have know hair left at this rate as this problem is wrecking my head

http://www.jmonkeyengine.com/jmeforum/index.php?topic=4705.0



similar issue might be helpful

What behaviour are you seeing?



Also we forgot to normalize the direction. Not sure how this can be making the bots go in the opposite direction. In my example, this line should be:



direction = player.getLocalTranslation().subtract(bot.getLocalTranslation()).normalize();



Sometimes it helps to start from scratch. Take a pice of paper and always remember that there are / may be different coordinate systems.



Be careful with references to vectors that may have been modified.


Hey,



  basically it moves away from me, and it is up in y axis is always above the terrain no even if i put it to be at the same height as the terrain, but when i just have it on its own in the main update it seems to rotate to my direction