How to make a sprite avoid an object?

  • I found this game called Mousetrap, link below.

    Does anybody know the basic algorithm on how the mouse knows which way to go? The mouse just seems to head off in a random direction at first but doesn't seem random at all when you are close to blocking him from leaving.

    Thanks

    Tony

    sheppardsoftware.com/braingames/mousetrap/mousetrapAS2.htm

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You can try the A* plugin for path finding.

  • Thanks jayderyu

    I've been reading up on the pathfinder theory...pretty intense. It looks like what I want, except that it is opposite of what I'm looking for. The pathfinder example shows ways an object can find a path from where it is at to where you click. I would like to click on the screen somewhere and have the object avoid going in that direction :-) I'll keep studying it and maybe I can reverse something to get it to work. Thanks again!

  • Hey dinofun. I'm just throwing out some ideas here, but what about checking the object's orientation in relation to where you DON'T want it to go and adjust its movement accordingly? It'd be like having an angle black-list, I suppose. It could move toward an angle that sets it apart from the bad spot... ?

  • Thanks space Ape

    that sounds like something I should look into. I'm still playing with the A Star plugin which shows a lot of promise. So far I can get a sprite to move towards a wall and avoid objects in its way. Next I need to find a way to get the sprite to "say" to itself "That wall is closer but there is an object in the way so maybe I should go the other way". If I run that into a deadend I'll look at using angles somehow. Thanks again!

  • dinofun: as for deciding on distances, you could use the system function that calculates distance (if you haven't already!) and then check if there's a collision between the object and the wall. One way of doing that would be to use an invisible sprite (like a straight line-of-sight line) and see if that invisible line "hits" any objects in between. If there is a collision in line-of-sight, change the angle and check it again until it finds a free route.

    That could be done without using plugins, I believe.

  • space Ape: that is pretty interesting. I've got time invested in the A star plugin and feel like I'm heading in the right direction <img src="smileys/smiley1.gif" border="0" align="middle" /> so want to pursue that for a bit. It's a nice plugin and you can tell the sprite where to go and it will avoid the "blockers" along the way. I just need to have it figure out how to go "away" from me. Thanks again!

  • dinofun: Good luck, dude! Let us know how it works out. I haven't messed with that A* plugin but it sounds pretty good.

  • I don't think the mouse is moving away from the player. He's going for the shortest path to the edge. He still goes towards where you are clicking if it's the shortest path.

  • ramones: I think you're right, pretty intriguing algorithmn though...

    What I have now is pretty simple. In the demo you have to block the green hexagon from getting to the dinosaur. You can drap the dinosaur to another spot if you want.

    dropbox.com/s/apsig8if86gd5lb/mousetrapdemo.capx

    dropbox.com/s/apsig8if86gd5lb/mousetrapdemo.capx

  • I was playing with the a* plugin yesterday: mouseyPathfinder.capx (r114)

    I was having trouble with the mouse moving into the newly spawned blocker so I ended up separating the blocker spawning and the mouse moving. I see from your capx that you can manually block the path with the 'Astar: Block path' action. That's cool.

  • Wow ramones, that looks exactly like what I was looking for. Very nice. I'll study your capx and see what I can learn. Thank you!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)