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

Author Topic: Colliding problem...  (Read 2639 times)

0 Members and 1 Guest are viewing this topic.

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Colliding problem...
« on: June 13, 2017, 09:03:19 pm »
Hello!I have a real bad time with this for a long time.The only last piece of that "map" is collideble and this is making me a lot of problems.If I solve this a lot of things will run out nicely.
The minimal Code:
int mapArray[45][5] =
{
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
...//and so
{1,0,0,0,0},
    };
//In the game loop
    if(Keyboard::isKeyPressed(Keyboard::Up))
    {
       upkey = true;
       upkeytimer.restart().asSeconds();
    }
    else if(!Keyboard::isKeyPressed(Keyboard::Up))
    {
       upkey = false;
    }
    if(upkey == true)
    {
    for(float up = upkeytimer.getElapsedTime().asSeconds();up < 2;up++) //Longer it's pressed longer it jumps
     {
        pos.y -=up;
     }
    }
    if(upkey == false && !SAdventurer.getGlobalBounds().intersects(STileGrass.getGlobalBounds())) //It collides only with the last piece,why?
    {
        pos.y += 2;
    }
//After window.clear()
for(int i = 0;i<43;i++)
    {
        for(int j = 0; j<5;j++)
    {
        if(mapArray[i][j] == 1)
        {
            STileGrass.setPosition(i * 70, (j * 70) + offsety);     //The "map" builder
            window.draw(STileGrass);
        }
    }
    }
window.display();
 
Guess Who's Back ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Colliding problem...
« Reply #1 on: June 13, 2017, 09:23:08 pm »
Quote
It collides only with the last piece,why?
Because STileGrass is the last tile, after the drawing loop of the previous iteration of the main loop?
Laurent Gomila - SFML developer

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: Colliding problem...
« Reply #2 on: June 13, 2017, 10:18:02 pm »
Ok,so how to make all the pieces collideble ?
Guess Who's Back ?

Hapax

  • Hero Member
  • *****
  • Posts: 3365
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Colliding problem...
« Reply #3 on: June 14, 2017, 01:28:52 am »
Loop through them and test each one.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: Colliding problem...
« Reply #4 on: June 14, 2017, 07:13:10 pm »
I don't really get it...what do you mean when saying:"Loop through them and test each one."Sorry if I sound like a noob :-[ .
Guess Who's Back ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Colliding problem...
« Reply #5 on: June 14, 2017, 09:01:32 pm »
Quote
what do you mean when saying:"Loop through them and test each one."
He means what you already do for drawing.
for(int i = 0;i<43;i++)
    {
        for(int j = 0; j<5;j++)
    {
        if(mapArray[i][j] == 1)
        {
            STileGrass.setPosition(i * 70, (j * 70) + offsety);     //The "map" builder
            /* put your collision check here instead of window.draw(STileGrass); */
        }
    }
    }

Quote
Sorry if I sound like a noob
You don't sound like a noob, you are one ;) But why would you be sorry for that? Everyone is a noob, before being... something else :P
Laurent Gomila - SFML developer

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: Colliding problem...
« Reply #6 on: June 15, 2017, 08:16:38 pm »
The collision check :
if(upkey == false && !SAdventurer.getGlobalBounds().intersects(STileGrass.getGlobalBounds()))
        {
        pos.y += 2;
        }
The "Loop through them and test each one" :
for(int i = 0;i<43;i++)
    {
        for(int j = 0; j<5;j++)
    {
        if(mapArray[i][j] == 1)
        {
            STileGrass.setPosition(i * 70, (j * 70) + offsety);
        if(upkey == false && !SAdventurer.getGlobalBounds().intersects(STileGrass.getGlobalBounds()))
        {
        pos.y += 2;
        }
        }
    }
    }
Well you see,the problem is that it isn't working(and I know I'm not doing it right,but I don't know what am I doing wrong),if the "up" key isn't pressed and the player doesn't touches the ground, the gravitation will come in,and it really goes like this...just not like I want,it amplifies(what the for loop is for),but it only is going right with the last piece of the map.
Another problem is that I don't really know if it is really a tile map,because a tile map is something like this: http://imgur.com/htqx9tZ
,while my "map" is something like:
http://imgur.com/a/xtIGP
Note:This are not screenshots,these are from google.
Guess Who's Back ?

 

anything