Jump to content

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


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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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