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

Author Topic: Clamp to Screen  (Read 3678 times)

0 Members and 1 Guest are viewing this topic.

deexener

  • Newbie
  • *
  • Posts: 2
    • View Profile
Clamp to Screen
« on: April 27, 2011, 04:04:36 am »
Hello!

This is my first time posting and I'm in a bit of a pickle. I started to use SFML for a game and I'm working on the basics of the library. Now, I'm probably totally missing something here but is their a way to bind a character to the screen? As in not allow them to go off screen?

In XNA their is a function called Clamp that is used to do this and I tried to recreate it here but it doesn't work.

Code: [Select]

float Clamp(float value, float min, float max)
{
    if(value < min)
    {
        return min;
    }
    if(value > max)
    {
        return max;
    }
    if(min <= value || value >= max)
    {
        return value;
    }
}

//in game loop
    playerPositionX = Clamp(playerPositionX, 0, windowWidth);
    playerBoundingBoxY = Clamp(playerPositionY, 0, windowHeight);



Any help would be much appreciated. As I said, I'm probably just not seeing the obvious answer.[/code]

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Re: Clamp to Screen
« Reply #1 on: April 27, 2011, 05:11:54 am »
To start, your function is done wrong. Decipher the commented code below for more info.
Secondly, you should always return something by default which fixes the problem referenced above anyways.
Code: [Select]
float Clamp(float value, float min, float max)
{
    if(value < min)
    {
        return min;
    }
    if(value > max)
    {
        return max;
    }
    // WRONG: (original)
    //if(min <= value || value >= max)
    //{
    //    return value;
    //}
    // CORRECT: (edited)
    //if(min <= value || max >= value)
    //{
    //    return value;
    //}

    return value; // default return
}


Unless you show more code, the only other potential problem I see is that your player variables have different names.
Code: [Select]
   playerPositionX = Clamp(playerPositionX, 0, windowWidth);
    playerBoundingBoxY = Clamp(playerPositionY, 0, windowHeight); // Intended to be playerPositionY?

deexener

  • Newbie
  • *
  • Posts: 2
    • View Profile
Clamp to Screen
« Reply #2 on: April 27, 2011, 05:49:15 am »
Thanks for the reply! And thanks for pointing out my mistakes >.<

But I'm still not able to get it working correctly. I did change the function and the playerBoundingBoxY is supposed to be playerPositionY, thought code::blocks auto-entered the correct one while typing. my bad.


even after those corrections i'm still able to travel outside the window. I'll keep at it and try it in a new project because my code is a mess of different tutorials right now.

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Clamp to Screen
« Reply #3 on: April 27, 2011, 01:59:55 pm »
If you're still having problems and post a minimal, but compilable, piece of code that demonstrates an attempt to clamp the character to the bounds of the screen,
I'm confident that someone will be able to help you.

Fred_FS

  • Newbie
  • *
  • Posts: 48
    • View Profile
Clamp to Screen
« Reply #4 on: April 27, 2011, 05:38:34 pm »
Are you using any views? So maybe your player is jsut going out of the view and not out of the window.

Make sure, that you do set the new player position. Maybe you forgot it and the correct position is computated, but not displayed.

But I can only guess. You have to give us more infos(as wizard mentioned: create a minimal application).

 

anything