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

Author Topic: How to 'get distance' between two entities?  (Read 6474 times)

0 Members and 1 Guest are viewing this topic.

OrientatedCookie

  • Newbie
  • *
  • Posts: 18
    • View Profile
How to 'get distance' between two entities?
« on: May 01, 2015, 05:50:19 pm »
Hello, can anyone give me some advice or point me in the right direction on how to get the distance between two sprites? What I want is to get the difference between two sprite's global position. I looked through the documentation and forums, but had no luck. I tried to do this but honestly have little idea:
sf::RectangleShape spritebox = sprite.getGlobalbounds();
sf::RectangleShape sprite2box = sprite2.getGlobalBounds();

if (spritebox < sprite2box)
{
window.close();
}


 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to 'get distance' between two entities?
« Reply #2 on: May 01, 2015, 05:58:06 pm »
I tried to do this but honestly have little idea:
That's a very random attempt. You try to check if one rectangle is less than the other -- there's not even such an operator, let alone are rectangles the right thing if you're talking of positions...

You should use the rectangle's centers. To calculate the distance, the function thor::length() can be helpful. You should have a look at basic vector algebra though; it's crucial to understand when working with SFML, even more so when developing a game.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

OrientatedCookie

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to 'get distance' between two entities?
« Reply #3 on: May 01, 2015, 08:56:51 pm »
Indeed it is a very random attempt. I'm a university mathmatician graduate with 5 years experience in coding.