No clue about player pathfinding, HELP!


(Stop dreaming, Start living.) #1

I am currently making my first real game and also new to AS3 Flashpunk and also new… to this idea haha. I tried to do it myself first but failed pretty hard so I decided to ask the community.

I use an OGMO tile/grid based map for collision and whatnot, but how would I go about to implement real-time pathfinding. and that it goes around a platform. (Let’s say I draw a tiles with my mouse how would you make it so that the player auto detects them and loops around them)

See picture.


(David Williams) #2

I’ve only done done Pathfinding once, and I no longer have the sourcecode, but I used A Pathfinding*. It is grid-based and pretty quick.


(Stop dreaming, Start living.) #3

I gave it another thought, I think it is not exactly pathfinding because it does not need to find a path. Basically all it needs to know is when to move in which direction at the right time. Pff so hard :stuck_out_tongue: trying none-stop to get to a sollution.


(Stop dreaming, Start living.) #4

Got it working guys!.


(Jacob Albano) #5

Can you explain your approach briefly? It might help someone viewing this thread in the future to solve the same problem.


(Stop dreaming, Start living.) #6

Sorry I can not.

If I explain something I make sure I explain it so people really understand it.

THIS METHOD IS NOT FINISHED YET I NEED TO OPTIMIZE IT FOR MY PURPOSE AND WORK OUT THE BUGS.

Prototype.swf (54.4 KB)

My idea was to check if I have collision on one of the 4 sides, If one side got collided with a tile then change the player in that color for debugging purposes and move the player in a direction.

If it reached a corner it will stop colliding because… it is not colliding with anything. If this happens I will move the player 1 pixel down if the last collision was pink. and If the last collision was red I move it 1px to the left if it reaches another corner and so on and so on.

Then I will reset the 4 sides collision booleans (eg: pink is true, all others are false. reach corner will reset all 4 sides including the current pink one.)

This idea currently only works for straight platforms but I need to make it work for all my purposes. Tomorrow I will proceed with this idea and make it better.

IT WOULD BE BAD TO COPY MY CRAPPY CODE, JUST USE IT TO GET THE CONCEPT BEHIND IT.

Code:

private var _velocity:Point;

private var leftV:Boolean;
private var rightV:Boolean;
private var topV:Boolean;
private var bottomV:Boolean;
_velocity = new Point;
var movement:Point = new Point;

//if (Input.check(Key.LEFT)) movement.x--;
//if (Input.check(Key.RIGHT)) movement.x++;
//if (Input.check(Key.UP)) movement.y--;
//if (Input.check(Key.DOWN)) movement.y++;

if (leftV) movement.y++;
if (topV) movement.x--;
if (rightV) movement.y--;
if (bottomV) movement.x++;

_velocity.x = 50 * FP.elapsed * movement.x;
_velocity.y = 50 * FP.elapsed * movement.y;

x += _velocity.x;	
y += _velocity.y;
// Top-Left
if (collide("Level",x-width, y))
{
	// Left
	Image(this.graphic).color = 0xffff0000;
	leftV = true;
	
	rightV = false;
	topV = false;
	bottomV = false
}else if (collide("Level",x+width, y))
{
	// Right
	Image(this.graphic).color = 0xfffff600;
	rightV = true;
	
	leftV = false;
	topV = false;
	bottomV = false
}else if (collide("Level",x, y-height))
{
	// Top
	Image(this.graphic).color = 0xff0036ff;
	topV = true;
	
	leftV = false;
	rightV = false;
	bottomV = false
}else if (collide("Level",x, y+height))
{
	// Bottom
	Image(this.graphic).color = 0xffff00cc;
	bottomV = true;
	
	leftV = false;
	rightV = false;
	topV = false;
}
else
{
	Image(this.graphic).color = 0xffffffff;
	
	if (leftV && !rightV && !topV && !bottomV) {
		trace("Red/Left last check");
		moveBy(-1, 0);
	}
	if (!leftV && rightV && !topV && !bottomV) {
		trace("Yellow/Right last check");
		moveBy(1, 0);
	}
	if (!leftV && !rightV && topV && !bottomV) {
		trace("Blue/Top last check");
		moveBy(0, -1);
	}
	if (!leftV && !rightV && !topV && bottomV) {
		trace("Pink/bottom last check");
		moveBy(0, 1);
	}
	
	leftV = false;
	rightV = false;
	topV = false;
	bottomV = false;
}

Have fun.

In the end my good old brain was better than anything I could find on the internet… which was nothing :wink: