Avoiding object while walking to player

Posted By: Loopix

Avoiding object while walking to player - 12/11/07 17:37

Hello

I'd like to ask YOU if you can help me with some simple enemy AI stuff

I want my enemy to turn to the player when close enough and walking towards him (I can manage this part...) Whilst the enemy walks towards the player, the enemy should do 2 c_traces in a narrow angle (towards the front) and then (when hitting an obstacle) check wich trace was the longer and pan towards that side to avoid the object using the shortest pan turn. As soon as it's again clear between enemy and player, the enemy should again pan towards the player.

Although this can't be too hard, I'm too stupid to get it into working c-script

Thanks for any help!
Posted By: raiden

Re: Avoiding object while walking to player - 12/12/07 05:39

Hi Lukas

I got you fixed up bro, I couldn't resist the challenge of writing this and it works really well.

Code:

//variable defintions
var face_pos[3];//used to set position then distance
var face_ang[3];//used to subtract pan angles for turning ent
var face_trn[3];//limits the turning of the ent
//
//define definitions
define face_speed, 3;
//
define idle, 0;
define walk, 1;
var state;
//
define angDist, 25; // 25 degrees on trace angles
define checkN, 0; // normal tracing turn check
define checkR, 1; // collision tracing right turn check
define checkL, 2; // collision tracing left turn check
var turnState;
var dist1;
var dist2;
var check[3];
//
function testBall()
{
my.passable = on;
vec_scale(my.scale_x,0.10);
wait(1);
ent_remove(me);
}
//
function traceAngle(dist,ang_dist)
{
check.x = my.x + cos(my.pan+ang_dist) * dist;
check.y = my.y + sin(my.pan+ang_dist) * dist;
check.z = my.z;
//
ifdef dispTrace;
ent_create("sphere01.mdl",check.x,testBall);
endif;
//
return(c_trace(my.x,check.x,ignore_me));
}
//
action enemy {
while(!player) { wait(1); }
turnState = checkN; // start out normal checking and turning to the player
while(1) {
// normal checking first
if(turnState == checkN)
{
dist1 = traceAngle(100,-angDist); // right angle checking
dist2 = traceAngle(100,angDist); // left angle checking
}
// alter states if dist1 or dist2 returns > 0
if(dist1 > 0 && turnState == checkN) { turnState = checkR; }
if(dist2 > 0 && turnState == checkN) { turnState = checkL; }
// this is for normal turning to the player
if(turnState == checkN)
{
vec_diff(face_pos,my.x, player.x);
}
// dist1 returned a result above, set this state, now we turn to check.x
if(turnState == checkL)
{
vec_diff(face_pos,my.x,check.x);
dist1 = traceAngle(100,-angDist);
if(dist1 == 0) { turnState = checkN; } // back to normal state
}
// dist2 returned a result above, set this state, now we turn to check.x
if(turnState == checkR)
{
vec_diff(face_pos,my.x,check.x);
dist2 = traceAngle(100,angDist);
if(dist2 == 0) { turnState = checkN; } // back to normal state
}
// do the turning
vec_to_angle(face_ang,face_pos);
face_trn.pan = ang(face_ang.pan - my.pan);
if(face_trn.pan < 0) { my.pan += face_speed * time; }
else { my.pan -= face_speed * time; }
//
// your stuff here
//
wait(1);
}
}



I hope I explained everything ok in the comments, also the 100 passed as a parameter in traceAngle is the distance of tracing, so you can change that if you like. The code traces in 2 directions from the enemy and the angle is 25 degrees(which can also be changed in the define above)to the left and right of its x pos. When a distance is returned it sets the state and forces the turn vector(face_turn) to be the diffence of the new direction instead of the players position. Then the enemy turns to avoid the obstacle and once the trace is in the clear, its sets the state back to normal and begins tracing for new obstacles.

I know its a little backwards on the turning, you asked for a turn direction in the opposite, but I think this works pretty well.

Check out this demo to see it in action: Test Enemy Obstacle Avoidance

You'll see the enemy head towards the player to which you can't see, but watch how the enemy turns to go around the block in the middle.

I hope that helps you out bro.

-raiden
Posted By: Nems

Re: Avoiding object while walking to player - 12/12/07 15:46

Amazing, the comments and steps really help with understanding aspects for simple AI, only hope I can understand it enough to get to grips with obstacle avoidence, thanks to the both of you
Posted By: JazzDude

Re: Avoiding object while walking to player - 12/12/07 19:16

Very nice...Hurrah for Raiden!!!
Posted By: Loopix

Re: Avoiding object while walking to player - 12/12/07 21:45

I'm late...sorry!

Larry, I want to thank you so much for that awesome piece of code! This will not only be helpfull to me but also to many community members in need of effectiv object avoidance. Thats what I call real help!

Also it is very clever of you to setup the trace in seperate function as this allows to add "wait(?)" to improove the framerate.
Posted By: raiden

Re: Avoiding object while walking to player - 12/12/07 23:08

Thanks everybody for the gracious replies!

-raiden
Posted By: mpdeveloper_B

Re: Avoiding object while walking to player - 12/15/07 02:34

Quote:

Hi Lukas

I got you fixed up bro, I couldn't resist the challenge of writing this and it works really well.

Code:
insert raiden's awesome code here



I hope I explained everything ok in the comments, also the 100 passed as a parameter in traceAngle is the distance of tracing, so you can change that if you like. The code traces in 2 directions from the enemy and the angle is 25 degrees(which can also be changed in the define above)to the left and right of its x pos. When a distance is returned it sets the state and forces the turn vector(face_turn) to be the diffence of the new direction instead of the players position. Then the enemy turns to avoid the obstacle and once the trace is in the clear, its sets the state back to normal and begins tracing for new obstacles.

I know its a little backwards on the turning, you asked for a turn direction in the opposite, but I think this works pretty well.

Check out this demo to see it in action: Test Enemy Obstacle Avoidance

You'll see the enemy head towards the player to which you can't see, but watch how the enemy turns to go around the block in the middle.

I hope that helps you out bro.

-raiden




dude, i hate you i wish i would've known how to add that to my ai, except with them strafing around it rather than turning

i'm gonna have to kill you, then hire you for Manga Page lol
© 2024 lite-C Forums