Game Development Community

Run away from me in a random direction

by gamer · in Torque Game Engine · 09/19/2007 (11:18 am) · 5 replies

When my enemy is hit by my bullet, he runs away in a random direction, how do I implement this? Running away in the direction of my forwardVector or eyeVector is easy, but I want to choose a random Vector v such that v is within 90 degrees of my forwardVector/eyeVector in both directions. How can this be done?
thanks

#1
09/19/2007 (6:13 pm)
U could try to rotate the character with the angle u want then move him forward, but I dont know much in math so I'm not sure that I can exactly tell u how to do that
OR
with is quite a stupid way to do that, is to have some points or objects on the Map that u could use as a compass to the characters, meaning that when he gets shot choose one of those object to Aim at (I guess u r using an AIPlayer object, setAimObject can do the trick) then clear the Aim object then move forward
that can do some help, at least till some1 gives u the real way to do it
#2
09/19/2007 (7:44 pm)
If your doing this in Code, look into MathUtils::randomDir. Or just take your AI's current velocity and do AI's current velocity *= -1; to make it turn right around in its tracks and run away the direction it came from.

If you doing this in script, use the bit-o-code i dropped in this thread to calculate the setTransform; but use the getRandom(#,#); in place of the %kickback. I have not done this myself, i only THINK it should work, as in this would be MY starting place doing the same thing...

EDIT:
No that will not work, not quite that easy, sorry. I dont have more time on my hands right now or I would look into it- But it might give you ideas.....
#3
09/19/2007 (8:58 pm)
So you want to rotate a vector a random amount within +/- 90 degrees.
(the vector is your forward vector, and the resulting vector is the direction the dude will run)

1. choose a random amount within +/- 90 degrees:
%degrees = getRandom(-90, 90);
%radians = mDegToRad(%degrees);

2. create a [script] matrix representing a rotation around Z by that amount:
%matrix  = "0 0 0 0 0 1" SPC %radians;

3. rotate your forward vector
%result  = MatrixMulVector(%matrix, %forwardVector);
// note, TDN has the arguments to MatrixMulVector incorrectly reversed from the above.
#4
09/19/2007 (9:11 pm)
Neat!
#5
09/20/2007 (10:44 am)
Then what you could do, if you're using AIPlayer, is scale that vector by however far you want the AI to run (50 metres?), add the AI's current position, and set the resulting location as his aim target using setMoveDestination. Then he'll move there, and you can still use his aim location to fire at something else along the way (alternatively, set the aim location there as well).