Pathfinding (I need help!)

I cannot find anything regrading a pathfinder in playcanvas… So i came to the forums instead :slight_smile:

So, I’m making a tower defense game (as a project for school) and I decided to use PC for it, as it seemed suitable for what I’m doing, and I am mildly familar with JS, and can learn it rather quickly and complete the game before early 2019.

I have an example just to find out code for how I will spawn enemies, and I need pathfinding help.

Say:

enemy spawns at entity A
enemy needs to pathfind to entity B
then to C
D, E, F…
hit a teleporter, to another teleporter

pathfind to G, H, I etc…
teleporter again, then exit the screen as a “life lost”

I know, may sound confusing just reading it, but theres no better way to explain it really. So I’ll just ask for if I spawn a “box entity” on top of entity “A”, then I need this to pathfind itself to entity “B”. The rest I can figure out I would assume.

However, if you understood what I said above about all pathfinding, teleporters and any sort of code for spawning enemies and having them walk around the level, feel free to explain it to me, so I can take it with my own understanding :smiley:

I also need ideas for towers, however this will come much later, as I want a basic version first (1 tower, 1 wave, 5 enemies, bang). Then build off of that… (I’ll come back to this eventually)

Thanks for any replies in advance!

There isn’t a pathfinding library/feature that is native to PlayCanvas yet so you have to roll your own.

Typically, tower defence games are based on a grid like world which will you to apply most pathfinding algorithms like A Star (https://www.gamedev.net/resources/_/technical/artificial-intelligence/a-pathfinding-for-beginners-r2003).

Pathfinding is not a trivial area and will take a while to understand and implement.

I’ve figured something out now to do with

this.entity.translate(x,y,z);

Now all I need is to see how to get the object to stop at a point, rotate, and continue to another point…

Would this work via entity.getPosition, then saying if Test.getPosition = entity.getPosition { this.entity.rotate(x); }

etc.
?

That will work for straight paths where there are no obstacles between the entity and the target position.

If you are going that route, then you can look at this example which does something similar: https://developer.playcanvas.com/en/tutorials/point-and-click-movement/

The highlighted bit is what I’m questioning. Is there a way I can grab the position of another entity, call this A, and make it so that when these objects touch something occurs…

The way I thought of it was an if statement, but any form of entity.getPosition doesn’t want to work in order for me to use this.

The whole “Point and Click Movement” project isn’t exactly what I’m looking for I think, as the model I’m using is straight paths and 90’ rotations only.

Granted, but you are still moving to a target point at a constant speed and rotating the entity to face hat direction so the core logic is the same. The only difference is that you are using known target points vs a user defined one.

There’s also another sample where a camera follows a series of waypoints: https://developer.playcanvas.com/en/tutorials/camera-following-a-path/

Yes there is.

var Foobar = pc.createScript('foobar');

Foobar.attributes.add('anotherObject', {type: 'entity'});

Foobar.prototype.someFunction = function () {
   var anotherObjectPosition = this.anotherObject.getPosition(); 
};

I’ll look at that one in a bit.

Before I do however, would saying

"if(this.entity.x === -1.5) { this.entity.rotate(90) }:wink:

work?

Because in this case, I can make it with this concept instead of anything else.

Bare in mind that object is moving with constant velocity in horizontal&vertical directions

It will only work if this.entity.x is EXACTLY -1.5. Right down to the last decimal point.

You have to bear in mind of how floats are calculated and represented in memory as well. Doing the following:

var aNumber = 2 * -0.75;
if (aNumber === -1.5) {
    // This will never be true!
}

This will explain more: https://stackoverflow.com/questions/1088216/whats-wrong-with-using-to-compare-floats-in-java

How do you do a if (something is not equal to something else) ?

is it not =/ or something similar?

and how to while loops work?

KhanAcademy is a good place to learn the core basics: https://www.khanacademy.org/computing/computer-programming/programming#concept-intro

That should cover comparisons and loops a lot more thoroughly than I could in a single forum post.

There should be also a tower defense game somewhere in playcanvas games, or you can check the roguelike projects

I couldn’t find anything. I’m trying to figure it now, and I’m at a point where I can get the object to move (easy enough)… but need to know if how to make it so the object stops at a certain x value…

I’m using this.entity.x < -1.532 (which it is up to -1.531) in a while loop, yet nothing happens.

While, if i put the value that “this.entity.x” should be (-3) in there, it moves the object, as -3 is always < -1.532 …

is this.entity.x doing nothing?

Just in case, I also said var y = this.entity.getPosition; //gathers the world position of the entity.

If this.entity.x is not the correct thing to use for this, what would be?

Edit: here if my while loop:

while (this.entity.x < -1.532) {
this.entity.translate(0.01,0,0);
}

https://playcanvas.com/project/333752/overview/last-line-of-defense
https://playcanvas.com/project/159/overview/roguelike

That property doesn’t exist as you can see from the entity API https://developer.playcanvas.com/en/api/pc.Entity.html

If you want to get the world x position of the entity, then it would be this.entity.getPosition().x