How can I get a 2d sprite to move to a position clicked on the screen, without bumping into other objects? I'm programming in java using the android API library

I have created a surface view and loaded my sprites as a bitmap object.

  • 1
    \$\begingroup\$ You might try doing an internet search on this topic before asking questions here. \$\endgroup\$ – Jonathan Dickinson Dec 9 '11 at 7:46
  • \$\begingroup\$ @JonathanDickinson I think this is a valid question for gamedev. The question is about finding a way from A to B without bumping into other obstacles (if I understood correctly). \$\endgroup\$ – bummzack Dec 9 '11 at 8:14
  • \$\begingroup\$ @bummzack in which case it's a duplicate. \$\endgroup\$ – Jonathan Dickinson Dec 9 '11 at 8:19
  • \$\begingroup\$ @JonathanDickinson feel free to mark it as such then. \$\endgroup\$ – bummzack Dec 9 '11 at 8:25

It depends on your game. If you have a dense environment with restricted moving paths (eg. something like a maze), then you should look into path-finding. If you have only few and spaced obstacles which may even be moving, then look into steering-behaviors, especially obstacle-avoidance.

Update: While path-finding usually works with a starting location and a target-location (eg. your sprites current position and the position you clicked), this might not be so obvious with steering-behaviors. In the case of steering-behaviors, you can use a combination of behaviors. Eg. start with a behavior like "Arrival" where the target is where you last clicked. During this behavior, check for obstacles in the way and switch to the obstacle-avoidance behavior if you detect an obstacle in your path. As soon as the way is clear, re-apply the arrival behavior. Alternatively you could also implement your own behavior that takes everything into account.

  • \$\begingroup\$ Thanks for the information this definitely gives me much to think about and a bit of testing to do. \$\endgroup\$ – kdavis8 Dec 10 '11 at 3:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.