2D Sidescroller pathfinding using A*(A star) algorithm

Recommended Posts

Hello, I'm trying to implement enemy pathfinding algorihtm, but i have problem with empty tile collision when moving enemy to node.

For example this image shows how it should move like shown in example:

But it stucks at last tile:

It happens because enemy collides with right side of "air" tile and then it removes from node list because it "collided", but it works with not "air" tiles of course. How do fix this problem?

Code:

void Enemy::generateMoveToNode(AStar::Vec2i lastNode)
{
auto lastSave = AStar::Vec2i{ 0.0f, 0.0f };

while (!target.empty())
{
if (target.back().y == lastNode.y)
{
lastSave = target.back();
target.pop_back();
}
else
{
moveToNodes.push_back(lastSave);
moveToNodes.push_back(target.back());
generateMoveToNode(target.back());
return;
}
}

moveToNodes.push_back(lastSave);
}

void Enemy::updateTarget(std::shared_ptr<InputManager> inputManager)
{
if (moveToNodes.empty()) return;

// Calculate half sizes.
float halfWidthA = getSize(0) / 2.0f;
float halfHeightA = getSize(1) / 2.0f;
float halfWidthB = 32.0f / 2.0f;
float halfHeightB = 32.0f / 2.0f;

// Calculate centers.
auto centerA = glm::vec2(getPosition(0) + halfWidthA, getPosition(1) + halfHeightA);
auto centerB = glm::vec2((moveToNodes.front().x * 32.0f) + halfWidthB, (moveToNodes.front().y * 32.0f) + halfHeightB);

// Calculate current and minimum-non-intersecting distances between centers.
float distanceX = centerA.x - centerB.x;
float distanceY = centerA.y - centerB.y;
float minDistanceX = halfWidthA + halfWidthB;
float minDistanceY = halfHeightA + halfHeightB;

setKey(inputManager->getKeyBinding("Move Left"), false);
setKey(inputManager->getKeyBinding("Move Right"), false);
setKey(inputManager->getKeyBinding("Jump"), false);
setKey(inputManager->getKeyBinding("Duck"), false);

// If we are not intersecting at all, return (0, 0).
if (abs(distanceX) >= minDistanceX || abs(distanceY) >= minDistanceY)
{
if (moveToNodes.front().y > ceil(getPosition(1) / 32.0f))
setKey(inputManager->getKeyBinding("Jump"), true);
else if (moveToNodes.front().y < ceil(getPosition(1) / 32.0f))
{
if (getCanClimb())
setKey(inputManager->getKeyBinding("Duck"), true);
}
else
{
if (moveToNodes.front().x < ceil(getPosition(0) / 32.0f))
setKey(inputManager->getKeyBinding("Move Left"), true);
else if (moveToNodes.front().x > floor(getPosition(0) / 32.0f))
setKey(inputManager->getKeyBinding("Move Right"), true);
}

updateInput(inputManager);
return;
}

// Calculate and return intersection depths.
float depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
float depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;

updateInput(inputManager);
moveToNodes.erase(moveToNodes.begin());
}

generateMoveToNode: recursive function to generate all nodes.

updateTarget: updates enemy every frame to check if it hits node and then removes it from list and checks next till no nodes left.

Edited by povilaslt2

Create an account

Register a new account

• 12
• 10
• 11
• 18
• 13
• Similar Content

• Hi everyone!

I am from a team that has received funding and is ready to create the next generation 2D Pokemon-inspired MMORPG called Phantasy World. This ad is for a volunteer position but could transition into something more. Our vision is to create a game that draws inspiration from the series but is dramatically different in both aesthetics and gameplay as the work would be our own original work. We are hoping that you can help us make this a reality. Currently we have 4-5 full timers on this project and would love it if you could join us!

We are looking for game developers familiar with the unreal engine and would be happy to work on a 2D top down game. Sprite artists are also welcome as we are in desperate need of talented artists! Join our discord and let's have a chat! https://discord.gg/hfDxwDX

Here's a teaser as to what our in game characters look like when moving in the game world! Hope to see you soon!

• By Ovicior
Hey,
So I'm currently working on a rogue-like top-down game that features melee combat. Getting basic weapon stats like power, weight, and range is not a problem. I am, however, having a problem with coming up with a flexible and dynamic system to allow me to quickly create unique effects for the weapons. I want to essentially create a sort of API that is called when appropriate and gives whatever information is necessary (For example, I could opt to use methods called OnPlayerHit() or IfPlayerBleeding() to implement behavior for each weapon). The issue is, I've never actually made a system as flexible as this.
My current idea is to make a base abstract weapon class, and then have calls to all the methods when appropriate in there (OnPlayerHit() would be called whenever the player's health is subtracted from, for example). This would involve creating a sub-class for every weapon type and overriding each method to make sure the behavior works appropriately. This does not feel very efficient or clean at all. I was thinking of using interfaces to allow for the implementation of whatever "event" is needed (such as having an interface for OnPlayerAttack(), which would force the creation of a method that is called whenever the player attacks something).

Here's a couple unique weapon ideas I have:
Explosion sword: Create explosion in attack direction.
Cold sword: Chance to freeze enemies when they are hit.
Electric sword: On attack, electricity chains damage to nearby enemies.

I'm basically trying to create a sort of API that'll allow me to easily inherit from a base weapon class and add additional behaviors somehow. One thing to know is that I'm on Unity, and swapping the weapon object's weapon component whenever the weapon changes is not at all a good idea. I need some way to contain all this varying data in one Unity component that can contain a Weapon field to hold all this data. Any ideas?

I'm currently considering having a WeaponController class that can contain a Weapon class, which calls all the methods I use to create unique effects in the weapon (Such as OnPlayerAttack()) when appropriate.

• Good Evening,
I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
I am really stucked right now because of the fundamental question:
Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on.
In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
Should I treat those debug objects as entities/components?
For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
Regards,
LifeArtist

• hello, i'm steve and im designer, i can design any idea and i'm looking for projects to help, if you are interesting write and we can talk about it