# Fox vs Tux Exercise

I have problem writing the code for creature to face the direction it is moving.(not just 90 degree)

List of thing i tried to do:

-make them move in angle other than 90 degree

-make them face direction

Hello from New Zealand!

I have a few minutes left at this internet kiosk so hopefully I can help...

To turn the object try something that just rotates on the Y axis. Like:

/**

* Turns the object three degrees to the right

*

*/

public void turnRight() {

setRotateY(getRotateY()-3);

}

(you will need two methods one for turning in each direction)

and then to move forward in the direction that the object is facing try

/**

* Moves the object forward

* @param velocity

*/

public void moveUp(double speed) {

setX(getX()+((float)(speed*Math.sin(Math.PI*getRotateY()/180))));

setZ(getZ()+((float)(speed*Math.cos(Math.PI*getRotateY()/180))));

}

Hope that helps!

Here's a great series of posts on mathematics for rotation and physics in the game. It applies to other things as well (like AI).

http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/

Btw, it's a great blog to follow. They're indie developers and they follow a process of "open development". There are frequent updates to the blog talking about the development process and what goes into the game.

or.... you could do this

/** * Move the creature randomly */ public void move(double dir) { double rand = Math.random(); if (rand < 0.5) { rotateY-=25; } else if (rand < 1) { rotateY+=25; } x += (Math.sin(Math.toRadians(rotateY))*dir); z += (Math.cos(Math.toRadians(rotateY))*dir); if (x < scale) x = scale; if (x > 50-scale) x = 50 - scale; if (z < scale) z = scale; if (z > 50-scale) z = 50 - scale; }

I managed to make the fox to hunt town closest tux finally ...

double angle = Math.toDegrees(Math.atan2(this.getZ()-closestTux.getZ(),this.getX()-closestTux.getX()))+90;

And then to turn to that angle is to setRotateY to opposite of that angle ...

vkorpiklaani.net/capu/2011/comp126/labs/lab3/appletloader.html