# 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

• ### Similar Content

• Developing a game? Don't want to make art yourself or hire expensive professional artists to do it? We've got you covered. Our artists will create custom 2D sprites and animations for the lowest prices.
Visit https://www.CustomGameArt.com today for 40% Off.
We make custom 2D video game graphics and animations of any kind in any style. We create pixel art for mobile games, pc games, avatars, gifts, music videos, any personal requests, and anything else. If you need tons of graphics, contact us on our website or at OfficialCGA@gmail.com for special promotions and bundle deals for game developers.

• Hey all,

So I have this unfathomable obsession with the games LIMBO and INSIDE, two games produced by Playdead Studios. But as a sound designer, I'm specifically into the audio of the game, its use of ambient tracks and specific sounds to create this level of emotion I've never felt in a platformer before.

I took a stab at redesigning the sound and ambiance of one of LIMBO's trailers to see if I could replicate such emotionality with my own creativity. Check it out below. All feedback is appreciated.
Thanks!

• Been a bit quiet recently, but we've been busy bug fixing and tweaking things... Now we have lots more 'Particle effects' in the game, specifically here the Flamethrower and Enemy attacks!
• By nilkun
Hello everyone!
First time posting in the forum.
I've just completed my first game ever ( C++ / SDL ), and I am feeling utterly proud. It is a small game resembling Missile Command. The code is a mess, but it is my mess! In the process of making the game, I developed my own little game engine.
My question is, where would be a good place to spread the news to at least get some people to try the game?
• By owenjr
Hi, I'm a Multimedia Engineering student. I am about to finish my dergree and I'm already thinking about what topic to cover in my final college project.
I'm interested in the procedural animation with c++ and OpenGL of creatures, something like a spider for example. Can someone tell me what are the issues I should investigate to carry it out? I understand that it has some dependence on artificial intelligence but I do not know to what extent. Can someone help me to find information about it? Thank you very much.

Examples:
- Procedural multi-legged walking animation
- Procedural Locomotion of Multi-Legged Characters in Dynamic Environments

• Hi guys, I'm having a problem rendering with DWrite, and I don't understand why, can you help me figure it out?
As you can see in the image below, if you look carefully you'll notice that the top of R8 is cut (missing 1 row of pixels), the bottom of R11 is cut again, the 4 in R14 is rendered weird compared to the 4 in R4 and so on, if you look closely you'll spot more yourself.
I can't figure out why 😕
Under the image I'll also leave the code, in case I'm doing something wrong like with type conversion or stuff. Any help is much appreciated

• Hello,
In the hopes that my thread is not off-topic or offensive in any way, I dare to ask the following "noob" question:
what would be the correct way to create sprite animations from 2D *.bmp files?
with the help of some tools i found , I was able to extract files containing what seems to be animation frames and frame shadow masks for animating a horse.
Attached are the archived assets.
My goal is to recreate the demo level of the game using Unity, for educational purposes.
I have started by loading the background map layer (also a large bmp file), and next step is to load a 2d character object and animate on the perspective.

Horse_Brown.rar

• Hello, my name is Alberto Muratore and I'm a young game developer specialized in programming.

Last summer I finished working on Abstract Arena, my first completed project published on Steam. During the development, lasted more than 1 year, I created every aspect of the game except for the audio sector. I previoulsy joined small competitions and had collaborations within the amateur scene, since I started having fun with game development when I was a kid. In the recent months I also started writing videogames reviews for a english-italian website about indie games.

Abstract Arena Steam page: http://store.steampowered.com/app/678230/Abstract_Arena/
Abstract Arena website: http://www.abstractarena.com/

I'm currently offering my skills as freelancer in the role of programmer: what I'm searching for is a project that only lacks of the coding part. I'm very good at using the GameMaker: Studio engine, and I already own a license to export on Android platform. I can write code for any genre of 2D games, and I already have experience with the peculiarities of the Android platform (multiple touch controls, accelerometer). I will be able to start working full time only during September, so be aware of that.

The plan is the following:
1 - you have an idea about your next project (2D game of any kind, for Windows and/or Android platform),
2 - you (or your team) create all of the graphics and audio resources,
3 - I write the whole code for the game (gameplay, menus and anything within the game) putting everything togheter [during September],
4 - if you like my work you can choose how much to pay for it, and you (or your team) keep the rights to sell the game without sharing any percentage with me.

Thank you for reading my announcement
If you have any question, please write me at the following address: albertomurat@gmail.com

• I am a talented 2D/3D artist with 3 years animation working experience and a Degree in Illustration and Animation. I have won a world-wide art competition hosted by SFX magazine and am looking to develop a survival game. I have some knowledge of C sharp and have notes for a survival based game with flexible storyline and PVP. Looking for developers to team up with. I can create models, animations and artwork and I have beginner knowledge of C sharp with Unity. The idea is Inventory menu based gameplay and is inspired by games like DAYZ.
Here is some early sci-fi concept art to give you an idea of the work level. Hope to work with like minded people and create something special. email me andrewparkesanim@gmail.com.
Developers who share the same passion please contact me, or if you have a similar project and want me to join your team email me.
Many thanks, Andrew.

• By mike44
Hi
saw in dependency walker that my app still needs msvcp140d.dll even after disabling debug.
What did I forget in the VS2017 release settings? After setting to multithreaded dll I get linker errors.
Thanks

• 17
• 11
• 12
• 9
• 49
• ### Forum Statistics

• Total Topics
631395
• Total Posts
2999758
×

## Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!