Jump to content


Photo

Astardll - Quick A* Algorithm Pathfinding


  • Please log in to reply
206 replies to this topic

#41 Jugado

Jugado

    GMC Member

  • New Member
  • 38 posts

Posted 07 April 2007 - 06:13 AM

Do not worry about my previous question. I have another one.
I am not familiar with paths, so let me ask you this:

In your code:


with obj_ball{
path_end();
if instance_exists(obj_goto){with obj_goto{instance_destroy()}}
//follow path returned by AStarDLL at speed 6
temppath=astar_getpath(obj_ball.x,obj_ball.y,mouse_x,mouse_y);
if string(temppath)!=''{
myvar=floor(mouse_x/global.astarR_cellwidth)*global.astarR_cellwidth
myvar2=floor(mouse_y/global.astarR_cellheight)*global.astarR_cellheight
instance_create(myvar,myvar2,obj_goto)
path_start(temppath,8,0,true);}}


you start with path_end(). According to the manual this function just stops the process of following the path but does not erase the path, is it correct?

If so, this means, previous paths are preserved?

Sorry for my silly questions.

Thanks
  • 0

#42 homebrewpc

homebrewpc

    GMC Member

  • New Member
  • 651 posts

Posted 07 April 2007 - 01:41 PM

When you use the astar_getpath() function it returns the path it created. So if you do something like:

path=astar_getpath(1,1,10,10)

Then the variable path will contain the path, as long as you dont set the variable path again, this path will always contain the path for 1,1 to 10,10.

You're right path_end() ends the path the object is currently following. I called this first and so when you click a new spot and the object is still following the previous path, it starts going to the new path instead.

Your questions aren't silly, I just couldn't understand the first one.

HomebrewPC
  • 0

#43 Jugado

Jugado

    GMC Member

  • New Member
  • 38 posts

Posted 07 April 2007 - 09:13 PM

So, it is not necessary to delete a path before to create new one?
path_delete(myPath);

Thanks for your answers.
  • 0

#44 homebrewpc

homebrewpc

    GMC Member

  • New Member
  • 651 posts

Posted 07 April 2007 - 10:17 PM

If you need the path later then you don't need to delete it. But if you're not going to use it anymore, you should probably delete it to free memory.

HomebrewPC
  • 0

#45 sleija

sleija

    GMC Member

  • New Member
  • 174 posts

Posted 09 April 2007 - 12:40 AM

I've heard that A* can be implemented into platform AI (can't find the topic, but it was by Potnop). Is this possible with this DLL?
  • 0

#46 homebrewpc

homebrewpc

    GMC Member

  • New Member
  • 651 posts

Posted 09 April 2007 - 01:20 AM

It should work, it depends on how you want to implement it. Like if you have an enemy that needs to move across a platform to your character, then yes it would.

HomebrewPC
  • 0

#47 Jugado

Jugado

    GMC Member

  • New Member
  • 38 posts

Posted 10 April 2007 - 07:40 AM

homebrewpc,

There is something funny with your dll. Imagine a small city with some blocks(see picture). I have a car which has to travel from point A to B. If point B is in a street (right-left) everything works well but if point B is in an avenue (up-down) each time the cars pass by an intersection, it tries, just tries like one node, to take an avenue instead to go straight by the street. It seems there is an extra node in each intersection. Any clue what is happening?

Thanks


Posted Image
  • 0

#48 homebrewpc

homebrewpc

    GMC Member

  • New Member
  • 651 posts

Posted 10 April 2007 - 02:04 PM

In relevance to the green block thing, is your object centered? I'm working on a simple GM script to keep the object centered in a cell when it is following a path, that might solve that problem.

As for the starting to go diagonal thing, that is probably a result of the order in which I check the points around a cell, and the fact that left/right/down/up are worth the same as diagonals. So if the DLL gets a chance to take a diagonal that seems faster it will automatically take it.

Tell you what, I will finish that center script, and then make it an option to set the value of up/down/left/right and diagonals. That will probably fix the problem, because then it will look at those diagonals and "think" "Oh, those cost more, so it's faster to go straight".

HomebrewPC
  • 0

#49 Jugado

Jugado

    GMC Member

  • New Member
  • 38 posts

Posted 10 April 2007 - 06:31 PM

In relevance to the green block thing, is your object centered? I'm working on a simple GM script to keep the object centered in a cell when it is following a path, that might solve that problem.

As for the starting to go diagonal thing, that is probably a result of the order in which I check the points around a cell, and the fact that left/right/down/up are worth the same as diagonals. So if the DLL gets a chance to take a diagonal that seems faster it will automatically take it.

Tell you what, I will finish that center script, and then make it an option to set the value of up/down/left/right and diagonals. That will probably fix the problem, because then it will look at those diagonals and "think" "Oh, those cost more, so it's faster to go straight".

HomebrewPC

<{POST_SNAPBACK}>


Excellent. Thanks for taking time to add more options. About the green blocks, that is what I talked before about the center problem. I have also these brick blocks which I use for collision with the car. I have to add the green ones because I have the problem shown in the picture, the center of the car goes one the sidewalk and the bricks.

With the new option you will add, I guess the problem will be solved.

Thanks
  • 0

#50 homebrewpc

homebrewpc

    GMC Member

  • New Member
  • 651 posts

Posted 10 April 2007 - 06:45 PM

So In the next version I will add:

-Option to set values for up/down/left/right and diagonals
-Center Path script (all in GML, no DLL interaction)
-Option to allow diagonals or not
-Option to cut through corners of solid objects or not

No problem,
HomebrewPC

EDIT: Uploaded a new version with all of above.

Edited by homebrewpc, 10 April 2007 - 07:34 PM.

  • 0

#51 Jugado

Jugado

    GMC Member

  • New Member
  • 38 posts

Posted 10 April 2007 - 08:41 PM

homebrewpc,

I got this error with the new version:

"Unexpected error occurred when running the game"

I am running GM7

Thanks for your effort!!!
  • 0

#52 databot

databot

    admin of moonlight games

  • New Member
  • 309 posts

Posted 10 April 2007 - 08:54 PM

is there a altnerate download site, i am having problems downloading
  • 0

#53 DFortun81

DFortun81

    The Fortunate One

  • GMC Member
  • 888 posts

Posted 10 April 2007 - 10:45 PM

For those of you who want to Center your paths, here is a modification of the script provided in the Example and the script that centers it:

Replace the old astar_getpath script with this:
//astar_getpath(startx,starty,endx,endy,center?)
{
var ar0,ar1,ar2,ar3,pathstring;
ar0 = floor(argument0 / global.astarR_cellwidth);
ar1 = floor(argument1 / global.astarR_cellheight);
ar2 = floor(argument2 / global.astarR_cellwidth);
ar3 = floor(argument3 / global.astarR_cellheight);

// Find the Path from the DLL
pathstring = external_call(global.astarR_getpath,ar0,ar1,ar2,ar3);
if(pathstring != '')
    {
    // Replace temporary function names
    pathstring = string_replace_all(pathstring,"a(",")astar_addpoint(");
    pathstring = string_replace_all(pathstring,"e(","astar_addpoint(");
    pathstring = string_replace_all(pathstring,"b(",")astar_endpath(");
    global.astarR_path = path_add();
    execute_string(pathstring);
    
    // Center the Path?
    if(argument4)path_center_grid(global.astarR_path,global.astarR_cellwidth,glo
bal.astarR_cellheight);
    
    // Return the Path Id
    return global.astarR_path;
    }

// Otherwise, return a blank string
return ''; 
}

Here is the script that is also needed.
//path_center_grid(path,w,h);
// Written by DFortun81
{
var i,x1,y1,s1;
for(i = 0;i < path_get_number(argument0);i += 1;)
    {
    x1 = floor(path_get_point_x(argument0,i) / argument1) * argument1 + argument1 / 2;
    y1 = floor(path_get_point_y(argument0,i) / argument2) * argument2 + argument2 / 2;
    s1 = path_get_point_speed(argument0,i);
    path_change_point(argument0,i,x1,y1,s1);
    }

return 1;
}

Enjoy,
-DF81
  • 0

#54 Jugado

Jugado

    GMC Member

  • New Member
  • 38 posts

Posted 10 April 2007 - 11:13 PM

DFortun81,

Thanks for the script, however I tried it and my car still goes on the edge between "blocked are" and "free area"

Thanks again
  • 0

#55 DFortun81

DFortun81

    The Fortunate One

  • GMC Member
  • 888 posts

Posted 10 April 2007 - 11:17 PM

Do you have your car sprite(s) centered correctly?
Are your walls aligned with the grid?

-DF81
  • 0

#56 Jugado

Jugado

    GMC Member

  • New Member
  • 38 posts

Posted 10 April 2007 - 11:25 PM

Do you have your car sprite(s) centered correctly?
Are your walls aligned with the grid?

-DF81

<{POST_SNAPBACK}>


My car sprite has the center in the center of the image and the walls are aligned to grid. As you can see in the picture, if I use the brick wall for blocking (I used it for collision with car), the car goes over the buildings. So I had to add an extra wall, green one to avoid it.

Posted Image
  • 0

#57 DFortun81

DFortun81

    The Fortunate One

  • GMC Member
  • 888 posts

Posted 10 April 2007 - 11:28 PM

Notice in the script heading that I added an extra argument:
astar_getpath(startx,starty,endx,endy,center?);

Did you forget to set center to true?
[In Global Left Pressed, in the obj_control object]

-DF81
  • 0

#58 Jugado

Jugado

    GMC Member

  • New Member
  • 38 posts

Posted 10 April 2007 - 11:39 PM

Notice in the script heading that I added an extra argument:
astar_getpath(startx,starty,endx,endy,center?);

Did you forget to set center to true?
[In Global Left Pressed, in the obj_control object]

-DF81

<{POST_SNAPBACK}>


I did it but I tried again and I got the same result:

myPath=astar_getpath(car.x,car.y,target.x,target.y,true);

  • 0

#59 Marchal_Mig12

Marchal_Mig12

    The Rhouan

  • GMC Member
  • 1153 posts

Posted 11 April 2007 - 12:17 AM

Can it support moving obstacle?
  • 0

#60 DFortun81

DFortun81

    The Fortunate One

  • GMC Member
  • 888 posts

Posted 11 April 2007 - 12:24 AM

I did it but I tried again and I got the same result:

myPath=astar_getpath(car.x,car.y,target.x,target.y,true);

<{POST_SNAPBACK}>

I'd have to see it to be able to fix it, I guess.

Can it support moving obstacle?

<{POST_SNAPBACK}>

Yes, you can add and remove obstacles. I'd suggest reading the help file that comes with this to figure out how to do it.

-DF81
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users