Jump to content

how to make moving sprite "smart" and evade collisions?


udisun
 Share

Recommended Posts

i have a game in which monsters are generated and are moving to the center of the map

on the way they have obstacles and i need them to be a little bit smarter and "know" they are about to collide so they will try to go around that obstacle. 

 

does anyone know how one can achieve that?

Link to comment
Share on other sites

I'm not specialist but have three clues :

 

- you could have wider bouding boxes than their related visible obstacle sprite. So your monster will actually collide the obstacle but farther than its visible image to the player's perspective. You can then have a collision callback function to change monster moving directions (say, rotate 30° left or right, then try moving ahead with the same constraints).

The user won't see your monster collide the obstacles, but maybe hesitate over their way to follow. Not sure they'll look smart  :lol:

 

- you could either implement vector fields attached to your obstacles.

Article here : http://buildnewgames.com/vector-field-collision-avoidance/ (caution it's not plain javascript but pseudo-code) and exemple here : https://mod.it/YCb1159n

I think it's not that CPU consuming. Your monsters won't never collide your obstacles then.

Will they look smart ? it probably depends upon your obstacle positions.

 

- you can use a A-star algorithm if your obstacles have fixed positions. here is the Phaser plugin : https://github.com/appsbu-de/phaser_plugin_pathfinding

Your monsters will use the best, so the smartest, way to reach their goal without colliding any obstacle.

Link to comment
Share on other sites

Thank you for these tips, i actually thought about the first one as a "fake" smart monsters, and i'm using this line to move my monster:

monster.rotation = this.game.math.degToRad(90) + this.game.physics.moveToObject(monster, player, 60);

Using moveToObject like this actually changes the rotation of my monster while it is colliding with obstacles, so it's really cool.

I'm going to investigate the other options you've suggested and report back.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...