Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (degenerate_762, AbrahamR, AndrewAMD, ozgur), 667 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Avoiding object while walking to player #172283
12/11/07 17:37
12/11/07 17:37
Joined: Mar 2005
Posts: 969
ch
Loopix Offline OP
User
Loopix  Offline OP
User

Joined: Mar 2005
Posts: 969
ch
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!

Re: Avoiding object while walking to player [Re: Loopix] #172284
12/12/07 05:39
12/12/07 05:39
Joined: Sep 2003
Posts: 281
Arkansas\USA
raiden Offline
Member
raiden  Offline
Member

Joined: Sep 2003
Posts: 281
Arkansas\USA
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


"It doesn't matter if we win or lose, it's how we make the game."
--------------------
Links: 3DGS for Dummies
Re: Avoiding object while walking to player [Re: raiden] #172285
12/12/07 15:46
12/12/07 15:46
Joined: Mar 2003
Posts: 4,264
Wellington
Nems Offline

.
Nems  Offline

.

Joined: Mar 2003
Posts: 4,264
Wellington
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

Re: Avoiding object while walking to player [Re: Nems] #172286
12/12/07 19:16
12/12/07 19:16
Joined: Sep 2003
Posts: 733
Whitefish, Montana
JazzDude Offline
User
JazzDude  Offline
User

Joined: Sep 2003
Posts: 733
Whitefish, Montana
Very nice...Hurrah for Raiden!!!

Re: Avoiding object while walking to player [Re: JazzDude] #172287
12/12/07 21:45
12/12/07 21:45
Joined: Mar 2005
Posts: 969
ch
Loopix Offline OP
User
Loopix  Offline OP
User

Joined: Mar 2005
Posts: 969
ch
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.

Last edited by Loopix; 12/12/07 22:45.
Re: Avoiding object while walking to player [Re: Loopix] #172288
12/12/07 23:08
12/12/07 23:08
Joined: Sep 2003
Posts: 281
Arkansas\USA
raiden Offline
Member
raiden  Offline
Member

Joined: Sep 2003
Posts: 281
Arkansas\USA
Thanks everybody for the gracious replies!

-raiden


"It doesn't matter if we win or lose, it's how we make the game."
--------------------
Links: 3DGS for Dummies
Re: Avoiding object while walking to player [Re: raiden] #172289
12/15/07 02:34
12/15/07 02:34
Joined: Feb 2006
Posts: 2,185
mpdeveloper_B Offline
Expert
mpdeveloper_B  Offline
Expert

Joined: Feb 2006
Posts: 2,185
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

Last edited by mpdeveloper_B; 12/15/07 02:36.

- aka Manslayer101

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1