Author Topic: Correct movement while aiming for AIPlayers  (Read 464 times)

Automatic AIPlayer movement (through setMoveDestination and setMoveObject) usually works fine, but it breaks completely when aiming at something else (using setAimLocation or setAimObject). The implementation in aiPlayer.cc seems to try to work around this (in some way or another):

        // Rotate the move into object space (this really only needs
         // a 2D matrix)
         Point3F newMove;
         MatrixF moveMatrix;
         moveMatrix.set(EulerF(0, 0, -(rotation.z + movePtr->yaw)));
         moveMatrix.mulV( Point3F( movePtr->x, movePtr->y, 0 ), &newMove );
         movePtr->x = newMove.x;
         movePtr->y = newMove.y;


But it doesn't succeed in doing so at all. This doesn't work. I've been trying to work around it in several ways, and the most promising solution seems to be just manually setting setMoveX and setMoveY. How would I properly calculate the forward/backward and left/right movement factors relative to yaw for a certain target though? So far I've tried calculating angles and adding/subtracting them up, as well as a test with matrix multiplication. I've had it work occasionally, but everything just breaks down entirely after a while + whenever the bot is aiming at somebody (seems to just move toward them at weird angles usually).

This is what I'm using. This is in a schedule loop on an AIConnection who's control object is a Bot.

Code: [Select]

//Moving
%moveVec = vectorSub(%movePos, %pos);

%moveY = vectorComponent(%moveVec, %forVec);
%moveX = vectorComponent(%moveVec, vectorCross(%forVec, "0 0 1"));

if(vectorLen(%moveX SPC %moveY) > 1) {
%move = vectorNormalize(%moveX SPC %moveY);
%moveX = getWord(%move, 0);
%moveY = getWord(%move, 1);
}

%client.setMove("y", %moveY);
%client.setMove("x", %moveX);



//Looking
%lookVec = vectorNormalize(vectorSub(%lookPos, %eyePoint));

%yaw = mATan(getWord(%lookVec, 0), getWord(%lookVec, 1));
%pitch = mASin(getWord(%lookVec, 2));// - 0.15; // - 0.15 makes the bot look down slightly, to mimic players, however it makes it inaccurate.

%facingDir = mATan(getWord(%forVec, 0), getWord(%forVec, 1));

%yaw -= %facingDir;
%pitch -= mASin(getWord(%eyeVec, 2));

if(%yaw > $pi)
%yaw -= 2*$pi;
if(%yaw < -$pi)
%yaw += 2*$pi;

%yaw = mClampF(%yaw/5, -$pi/30, $pi/30); //These just limit the turn rate of the bot.
%pitch = mClampF(%pitch/5, -$pi/30, $pi/30);

%client.setMove("yaw", %yaw);
%client.setMove("pitch", -%pitch);




function vectorComponent(%vec1, %vec2) { //Returns the component/projection of vec1 in the direction of vec2
return vectorDot(%vec1, vectorNormalize(%vec2));
}

%movePos is the position to move to, %lookPos is the point to look at. Everything else should be self-explanatory.


Edit: Added in code for looking at a position.
« Last Edit: August 18, 2014, 04:38:13 PM by boodals 2 »