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

Author Topic: Check collision before moving  (Read 1454 times)

0 Members and 1 Guest are viewing this topic.

Elit3d

  • Newbie
  • *
  • Posts: 3
    • View Profile
Check collision before moving
« on: August 21, 2018, 10:45:07 pm »
My player currently has a simple collision on it that works very well for my game

Quote
if ((*charIter)->sprite.getGlobalBounds().intersects(obj.rect))


but I am going to be including AI and I need a checker to see if it can move a certain direction, if it can't it won't try to move there. Not sure where to start, so if anyone can suggest something, id greatly appriciate it
« Last Edit: August 21, 2018, 10:46:39 pm by Elit3d »

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Check collision before moving
« Reply #1 on: August 27, 2018, 06:20:44 pm »
You can make a function that does the same check on where the sprite is going to be instead of where it is. Pass the predicted coordinates to it like so:

bool can_I_go_there(sf::FloatRect FR) { if (FR.intersects(obj.rect)) { return true; } else { return false; } }

However, this is not exactly the ideal way to have pathfinding in your game. You should look into proper pathfinding implementations.