Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [2.0] How to stop moving sprites from stacking onto each other?  (Read 1656 times)

0 Members and 1 Guest are viewing this topic.

NinjaCat

  • Newbie
  • *
  • Posts: 2
    • View Profile
[2.0] How to stop moving sprites from stacking onto each other?
« on: January 15, 2013, 11:41:02 pm »
Hello, I'm quite new to SFML, but as far as I've searched, I had no luck finding people with a similar problem.

Anway, I have a group of sprites moving towards another single sprite. Everything works fine, but they stack on top of each other.

So I made some collision checking, and it would work fine, if I wanted for the sprites to stop moving completely, when colliding. I'm trying to make them walk/bounce away from each other when they get too close, while not stopping the movement towards their target.

If there any way of achieving this in an uncomplicated manner?

(I've managed to get similar results(by bouncing them in a direction, opposite/left/right of the movement direction), but the group of sprites would occasionally "teleport" between each other.)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: [2.0] How to stop moving sprites from stacking onto each other?
« Reply #1 on: January 16, 2013, 02:12:02 am »
It's called collision detection and collision response - there's no magic that suddenly make things happen, you have to implement everything on your own. ;)
Just google the two given terms and you'll find hundreds of articles about it - some good, some not so.

Or you can go and grab an existing physics library for 2D graphics, like for example Box2D.

Anyways physics in games is one part that's not always easy to achieve. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [2.0] How to stop moving sprites from stacking onto each other?
« Reply #2 on: January 16, 2013, 10:01:06 am »
So I made some collision checking, and it would work fine, if I wanted for the sprites to stop moving completely, when colliding. I'm trying to make them walk/bounce away from each other when they get too close, while not stopping the movement towards their target.
A simple (not physically correct) approach for bouncing back, let's say sprite A and B collide with each other:

Let cA be the position of A's center, and cB the position of B's center. If the two sprites collide, you can move each sprite away from the other's center: Translate A along the vector cA-cB, and B along cB-cA. If you decrease the vector's magnitude over time, the repelling velocity decreases continuously.

Of course there are also more complex approaches like the Separate Axis Theorem. To calculate physical bounce, you have to take the momentum into account, or you can directly use Box2D as stated by eXpl0it3r. But a new library also requires some time to get used to.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

NinjaCat

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: [2.0] How to stop moving sprites from stacking onto each other?
« Reply #3 on: January 16, 2013, 12:14:09 pm »
Thanks for the replies, I will try the vector method.

But I made it a bit unclear.. I don't need it to be physically accurate, it's just so they don't overlap. The sprites should probably bounce the same amount (needed for the collision to end). Anyway, thanks for the suggestions, I'll get to work now  :)

EDIT: The vector solution worked perfectly, though I had to reduce the size of the vector, since it was moving the sprites too far away.
« Last Edit: January 16, 2013, 02:03:48 pm by NinjaCat »