This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-08-18 11:37:48

Jodymoses
Scratcher
Registered: 2012-05-03
Posts: 100+

A simple ai pathfinder

How do you make a simple ai pathfinder for e.g an enemie that doesnt need to touch a wall to mayby turn or glide


http://i46.tinypic.com/2hxwx9s.png
I Have Huge Ideas, But Only If You Listen And Talk To Me...

Offline

 

#2 2012-08-18 11:42:29

Jodymoses
Scratcher
Registered: 2012-05-03
Posts: 100+

Re: A simple ai pathfinder

Bump


http://i46.tinypic.com/2hxwx9s.png
I Have Huge Ideas, But Only If You Listen And Talk To Me...

Offline

 

#3 2012-08-18 12:01:56

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: A simple ai pathfinder

There's really no such thing as simple pathfinding.  You generally stuck with needing some kind of recursion.  I have found a way to do it without recursion in a generally straight-forward way.  You have to essentially cover the entire field with ghosted rectangles, in which every rectangle can get to any point in an adjacent rectangle using a straight-line path.

Example (gray spots are walls, colored rectangles are the "invisible" rectangles):
http://i50.tinypic.com/zpb7s.png

Then, to circumvent the recursion algorithm, you have to manually create a list of "if I am in rectangle X and I want to get to rectangle Y, I should point at rectangle Z".  Once the sprite is in the rectangle it wants to be in, it can take a straight-line path to the exact point.  This approach can take a very long time to program if you have a lot of rectangles, but it gets the job done.

Edit: the ability to import lists can come in handy, as it is easier to type up these incredibly long lists than to manually create them.

Last edited by MoreGamesNow (2012-08-18 12:05:19)


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#4 2012-08-18 13:22:03

flashgamer001
Scratcher
Registered: 2012-07-31
Posts: 8

Re: A simple ai pathfinder

If it always takes the same path, you can also set the exact path. Not true AI though.
Example:

when clicked
if
level
=
1
set
alive
to
1
go to x:
0
y:
0
show
repeat until
alive
=
0
glide
1
secs to x:
5
y:
0
glide
1
secs to x:
5
y:
5
glide
1
secs to x:
0
y:
5
glide
1
secs to x:
0
y:
0
hide


Let the sparks fly...

Offline

 

#5 2012-08-19 05:12:40

Jodymoses
Scratcher
Registered: 2012-05-03
Posts: 100+

Re: A simple ai pathfinder

Ok but i mean e.g a pac man ghost that follows a pacman using tiles


http://i46.tinypic.com/2hxwx9s.png
I Have Huge Ideas, But Only If You Listen And Talk To Me...

Offline

 

#6 2012-08-19 05:33:01

BirdByte
Scratcher
Registered: 2012-07-07
Posts: 1000+

Re: A simple ai pathfinder

All AIs are different. Try this.

forever
Replace this with your loop...
delete
all
of
paths
change x by
20
Width of path.
if
touching
walls
?
add
100000
to
paths
else
add
distance to
player
to
paths
change x by
-20
Width of path, negative.
change y by
-20
Width of path, negative.
if
touching
walls
?
add
100000
to
paths
else
add
distance to
player
to
paths
change y by
20
Width of path.
change x by
-20
Width of path, negative.
if
touching
walls
?
add
100000
to
paths
else
add
distance to
player
to
paths
change x by
20
Width of path.
change y by
20
Width of path.
if
touching
walls
?
add
100000
to
paths
else
add
distance to
player
to
paths
change y by
-20
Width of path, negative.
delete
all
of
directions
add
1
to
directions
add
2
to
directions
add
3
to
directions
add
4
to
directions
set
sorted
to
0
repeat until
sorted
=
1
set
sorted
to
1
set
counter
to
1
repeat
3
if
item
counter
of
paths
>
item
counter
+
1
of
paths
set
sorted
to
0
set
temp
to
item
counter
+
1
of
paths
replace item
counter
+
1
of
paths
with
item
counter
of
paths
replace item
counter
of
paths
with
temp
set
temp
to
item
counter
+
1
of
directions
replace item
counter
+
1
of
directions
with
item
counter
of
directions
replace item
counter
of
directions
with
temp
change
counter
by
1
point in direction
90
*
item
1
of
directions
move
20
steps
Width of path.
point in direction
90


http://i50.tinypic.com/312u714.jpg

Offline

 

#7 2012-08-19 05:36:16

BirdByte
Scratcher
Registered: 2012-07-07
Posts: 1000+

Re: A simple ai pathfinder

BirdByte wrote:

All AIs are different. Try this.

forever
Replace this with your loop...
delete
all
of
paths
change x by
20
Width of path.
if
touching
walls
?
add
100000
to
paths
else
add
distance to
player
to
paths
change x by
-20
Width of path, negative.
change y by
-20
Width of path, negative.
if
touching
walls
?
add
100000
to
paths
else
add
distance to
player
to
paths
change y by
20
Width of path.
change x by
-20
Width of path, negative.
if
touching
walls
?
add
100000
to
paths
else
add
distance to
player
to
paths
change x by
20
Width of path.
change y by
20
Width of path.
if
touching
walls
?
add
100000
to
paths
else
add
distance to
player
to
paths
change y by
-20
Width of path, negative.
delete
all
of
directions
add
1
to
directions
add
2
to
directions
add
3
to
directions
add
4
to
directions
set
sorted
to
0
repeat until
sorted
=
1
set
sorted
to
1
set
counter
to
1
repeat
3
if
item
counter
of
paths
>
item
counter
+
1
of
paths
set
sorted
to
0
set
temp
to
item
counter
+
1
of
paths
replace item
counter
+
1
of
paths
with
item
counter
of
paths
replace item
counter
of
paths
with
temp
set
temp
to
item
counter
+
1
of
directions
replace item
counter
+
1
of
directions
with
item
counter
of
directions
replace item
counter
of
directions
with
temp
change
counter
by
1
point in direction
90
*
item
1
of
directions
move
20
steps
Width of path.
point in direction
90

Oops! Fixed. Use this one instead.  smile


http://i50.tinypic.com/312u714.jpg

Offline

 

#8 2012-08-19 16:24:43

Jodymoses
Scratcher
Registered: 2012-05-03
Posts: 100+

Re: A simple ai pathfinder

And you use it in...


http://i46.tinypic.com/2hxwx9s.png
I Have Huge Ideas, But Only If You Listen And Talk To Me...

Offline

 

#9 2012-08-20 02:13:59

BirdByte
Scratcher
Registered: 2012-07-07
Posts: 1000+

Re: A simple ai pathfinder

Jodymoses wrote:

And you use it in...

The enemy sprite.


http://i50.tinypic.com/312u714.jpg

Offline

 

#10 2012-08-20 11:15:15

Jodymoses
Scratcher
Registered: 2012-05-03
Posts: 100+

Re: A simple ai pathfinder

2 things ,1, thank you very much ,if i make a project with this i will give a very high credit to you and 2 can anyone please explain this script in detail


http://i46.tinypic.com/2hxwx9s.png
I Have Huge Ideas, But Only If You Listen And Talk To Me...

Offline

 

#11 2012-08-21 00:56:28

Jodymoses
Scratcher
Registered: 2012-05-03
Posts: 100+

Re: A simple ai pathfinder

Isn't paths not going to any use because no motion blocks are used?


http://i46.tinypic.com/2hxwx9s.png
I Have Huge Ideas, But Only If You Listen And Talk To Me...

Offline

 

#12 2012-08-21 10:50:24

Jodymoses
Scratcher
Registered: 2012-05-03
Posts: 100+

Re: A simple ai pathfinder

...


http://i46.tinypic.com/2hxwx9s.png
I Have Huge Ideas, But Only If You Listen And Talk To Me...

Offline

 

#13 2012-08-21 15:05:14

sonicfan12p
Scratcher
Registered: 2011-11-16
Posts: 1000+

Re: A simple ai pathfinder

Jodymoses wrote:

Isn't paths not going to any use because no motion blocks are used?

What do you mean by no motion blocks used? I see several...


Why are the secret organizations getting all the attention?  mad

Offline

 

#14 2012-08-21 16:33:14

Jodymoses
Scratcher
Registered: 2012-05-03
Posts: 100+

Re: A simple ai pathfinder

I mean that 'paths' isn't used in a motion block , correct


http://i46.tinypic.com/2hxwx9s.png
I Have Huge Ideas, But Only If You Listen And Talk To Me...

Offline

 

#15 2012-08-22 12:34:44

Jodymoses
Scratcher
Registered: 2012-05-03
Posts: 100+

Re: A simple ai pathfinder

Is it?


http://i46.tinypic.com/2hxwx9s.png
I Have Huge Ideas, But Only If You Listen And Talk To Me...

Offline

 

#16 2012-08-22 12:40:06

sonicfan12p
Scratcher
Registered: 2011-11-16
Posts: 1000+

Re: A simple ai pathfinder

I don't have the time to interpret that entire script, birdbyte? Perhaps you could explain this massive script to Jodymoses?


Why are the secret organizations getting all the attention?  mad

Offline

 

#17 2012-08-22 14:16:32

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: A simple ai pathfinder

I'm not going to try to understand the script, but it seems that "paths" affects "directions", which affects movement:

point in direction
90
*
item
1
of
directions
move
20
steps


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#18 2012-08-22 15:26:10

BirdByte
Scratcher
Registered: 2012-07-07
Posts: 1000+

Re: A simple ai pathfinder

This might be of interest to you.  smile


http://i50.tinypic.com/312u714.jpg

Offline

 

#19 2012-08-23 06:37:25

jontmy00
Scratcher
Registered: 2011-11-28
Posts: 1000+

Re: A simple ai pathfinder

Jodymoses wrote:

Is it?

"Paths" are lists, under "Variables".


FOR ALL THE NEWS ON UPDATES FOR SIMPLISTICRAFT, CLICK HERE.

Offline

 

#20 2012-08-23 07:13:48

eventexception
Scratcher
Registered: 2011-04-08
Posts: 500+

Re: A simple ai pathfinder

http://wiki.scratch.mit.edu/wiki/Artificial_Intelligence

http://scratch.mit.edu/projects/Lucario621/803990

Offline

 

#21 2012-08-25 13:23:10

Jodymoses
Scratcher
Registered: 2012-05-03
Posts: 100+

Re: A simple ai pathfinder

Why do you add the below?

when clicked
if
touching {walls}
add
10000
to Paths


http://i46.tinypic.com/2hxwx9s.png
I Have Huge Ideas, But Only If You Listen And Talk To Me...

Offline

 

#22 2012-08-25 17:27:34

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: A simple ai pathfinder

Jodymoses wrote:

Why do you add the below?

when clicked
if
touching {walls}
add
10000
to Paths

Assuming he's following a sort of recursive algorithm that's trying to find the shortest path, making one of the path-ways "10000" basically means such a path will never be optimal.  Rather than adding extra conditionals to take walls into account, such routes are simply ignored because they are so high (or bad).


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#23 2012-08-27 00:53:46

Jodymoses
Scratcher
Registered: 2012-05-03
Posts: 100+

Re: A simple ai pathfinder

Bump


http://i46.tinypic.com/2hxwx9s.png
I Have Huge Ideas, But Only If You Listen And Talk To Me...

Offline

 

#24 2012-08-27 00:57:50

Jodymoses
Scratcher
Registered: 2012-05-03
Posts: 100+

Re: A simple ai pathfinder

Can anyone explain this further (scripts)


http://i46.tinypic.com/2hxwx9s.png
I Have Huge Ideas, But Only If You Listen And Talk To Me...

Offline

 

Board footer