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

Author Topic: Checking if something is near you with SFML  (Read 1872 times)

0 Members and 1 Guest are viewing this topic.

Gio

  • Newbie
  • *
  • Posts: 16
    • View Profile
Checking if something is near you with SFML
« on: June 06, 2010, 10:23:47 pm »
I am writing a Rougelike Game in C++ with SFML and want to know how to make it so that the game checks what object is up to 20 pixels above/below or 13 pixels left/right of you. Any code or advice would be greatly appreciated.
~~Gio~~

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Checking if something is near you with SFML
« Reply #1 on: June 07, 2010, 04:27:03 am »
If you have your objects in an appropriate container (http://cplusplus.com/reference/stl/ - vectors are probably the simplest to learn), you can just for loop through it.

object would be your vector of objects
Code: [Select]
for (int i = 0; i < object.size(); i++)
{
    //if object is lower than 20 pixels above the player and is higher than the player...
    if (object[i].getY > playerY - 20 && object[i].getY < playerY)
    {
        //object must be one unit above the player
    {
    //check other sides
}


It's more than likely that you will need something more complex than that, but it's the basic idea. Bear in mind that this if condition will be true any time the object is in the row above the player, you will have to cross check it with X(columns) for a proper result.

Might I suggest using square tiles/objects/player for simplicity?

rogue**

 

anything