How to make a sprite avoid an object?

Get help using Construct 2

Post » Wed Dec 26, 2012 7:07 pm

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
http://www.sheppardsoftware.com/braingames/mousetrap/mousetrapAS2.htmdinofun2012-12-26 19:13:50
B
14
S
6
G
3
Posts: 71
Reputation: 3,811

Post » Wed Dec 26, 2012 7:39 pm

You can try the A* plugin for path finding.
B
87
S
18
G
9
Posts: 2,455
Reputation: 14,834

Post » Thu Dec 27, 2012 2:16 am

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!
B
14
S
6
G
3
Posts: 71
Reputation: 3,811

Post » Thu Dec 27, 2012 2:45 am

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... ?
B
16
S
6
G
2
Posts: 152
Reputation: 3,764

Post » Thu Dec 27, 2012 3:15 am

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!
B
14
S
6
G
3
Posts: 71
Reputation: 3,811

Post » Thu Dec 27, 2012 8:06 pm

@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.
B
16
S
6
G
2
Posts: 152
Reputation: 3,764

Post » Fri Dec 28, 2012 3:38 am

@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 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!
B
14
S
6
G
3
Posts: 71
Reputation: 3,811

Post » Fri Dec 28, 2012 4:05 pm

@dinofun: Good luck, dude! Let us know how it works out. I haven't messed with that A* plugin but it sounds pretty good.
B
16
S
6
G
2
Posts: 152
Reputation: 3,764

Post » Fri Dec 28, 2012 7:03 pm

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.
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,000

Post » Sat Dec 29, 2012 4:59 am

@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.

https://www.dropbox.com/s/apsig8if86gd5lb/mousetrapdemo.capx
https://www.dropbox.com/s/apsig8if86gd5lb/mousetrapdemo.capx
B
14
S
6
G
3
Posts: 71
Reputation: 3,811

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 25 guests