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

Author Topic: [modified] map collision  (Read 6387 times)

0 Members and 1 Guest are viewing this topic.

riadh

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
[modified] map collision
« on: December 22, 2012, 12:05:38 pm »
Hey , i think that i have found the problem when in the main , i delete map1.draw(app) , i mean i stop drawing the map i think it works better , the drawing of map takes a lot of time that's why the player don't fit in his place , so i think now i must modify the Draw function to optimize it , i think that i will use only unsigned value then try to do more ,
What do u think guys ?


(im using a collision with a y = 480 just to test  ( i tested it with the old collision and a big diffrence too :) )








Hello everybody , i'm new here , but i have searched and didn't found any similar topic where i can find a solution
first of all here is a video of my game, (in progress)
it's a video of yesterday


if you see the collision with the map is really ridicules :(( i have done it by my self and i know it's wrong
 can anyone help me ? i can post my code (it's a real mess :(  )

thanks for your support :)
you Friend from Tunisia
----------------------
i'm working with sfml 1.6 and here player update code

void Player::Update(sf::RenderWindow &Window , int downElement , int rightElement , int leftElement, int topElement , IntRect underBox , int underBoxValue )
{

    const Input & input = Window.GetInput();
    PlayerVelocity.x = 0 ;
    //cout<<"underBoxValue"<< underBoxValue <<endl;


    //PlayerVelocity.y += gravity * Window.GetFrameTime();
     //gravité
    if(  (!jump ) )
    {
         PlayerVelocity.y += gravity * Window.GetFrameTime();

    }
    else
    {
        PlayerVelocity.y  = 0 ;
    }






    if(input.IsKeyDown(Key::Right))
    {   if(rightElement != 1 && rightElement != 2)
        {
            PlayerVelocity.x = moveSpeed ;
        }

        currentFrameY = 1;
    }
      if(input.IsKeyDown(Key::Left))
    {   if(leftElement != 1 && leftElement != 2)
        {
             PlayerVelocity.x = -moveSpeed ;

        }

        currentFrameY = 0;
    }
      if(input.IsKeyDown(Key::Up) && jump )
    {

           PlayerVelocity.y = -jumpSpeed ;
           jump = false ;

      //  y -= moveSpeed  * Window.GetFrameTime();
    } //else


//  1ere methode pr fixé le joueur a sol CollisionManager::collsion2Box(this->GetBoundingBox(),underBox  )
      if(   (downElement == 1  || downElement == 2) )
    {

        PlayerVelocity.y = 0 ;
        PlayerPosition.y = underBox.Top - 50    ;
        jump = true ;


    }
    else if ( underBoxValue == -1 && jump )
    {
        //PlayerVelocity.y = jumpSpeed /2;
        jump = false ;
    }


        if ((topElement == 1 || topElement == 2)  && (!jump  ) && (leftElement != 1 && leftElement != 2)  && rightElement !=1 && rightElement!= 2  ) // ki yodhreb fi 7aja mel fou9
    {
        PlayerVelocity.y = +jumpSpeed / 3;
    }


    if(PlayerVelocity.x==0)
    {
        playerAnimation.setActive(false) ;
    }
    else playerAnimation.setActive(true) ;

    PlayerPosition += PlayerVelocity * Window.GetFrameTime();



    playerAnimation.setPosition(1, PlayerPosition.x);
    playerAnimation.setPosition(2, PlayerPosition.y  );
    playerAnimation.setCurrentFrame(2, currentFrameY);
    playerAnimation.Update(Window);

}
 

on the main
int underBoxValue  ;
        IntRect underBox = map1.getUnderBox(player , app, underBoxValue) ;

        player.Update(app , map1.getDownElement(player , app) , map1.getRightElement(player, app) ,map1.getleftElement(player , app) , map1.getTopElement(player, app) , underBox, underBoxValue);
 


on map.cpp

int Map::getDownElement(Player p , RenderWindow &Window)
{


    //if(screenCapture.GetPixel(p.getX(),p.getY())  ==  );

    int i =    round  (( p.GetBoundingBox().Left +5 ) / BLOCKSIZE ) ;
    int j =     round ((p.GetBoundingBox().Top  ) / BLOCKSIZE ) ;
    //int    pos = -1 ;

int pos ;

       pos = MapVector[j+1][i] ;

  if (pos  == 0 )
  {
      cout <<"deboredment"<<endl ;
      //p.playerAnimation.setPosition(1,50);
      p.killed(Window);

  }

    return pos  ;
}

int Map::getRightElement(Player p , RenderWindow &Window)
{


    //if(screenCapture.GetPixel(p.getX(),p.getY())  ==  );

    int i =    (( p.GetBoundingBox().Right +14)/ BLOCKSIZE ) ;
    int j =   ( ( p.GetBoundingBox().Bottom + 10) / BLOCKSIZE ) ;
    int    pos  ;



       pos = MapVector[j][i] ;



    return pos  ;
}

int Map::getleftElement(Player p , RenderWindow &Window)
{


    //if(screenCapture.GetPixel(p.getX(),p.getY())  ==  );

    int i =    (( p.GetBoundingBox().Left + 2)/ BLOCKSIZE ) ;
    int j =   ( (p.GetBoundingBox().Bottom + 10) / BLOCKSIZE ) ;
    int    pos ;



       pos = MapVector[j][i] ;

  if (pos  == 0 )
  {cout <<"deboredment"<<endl ; }

    return pos  ;
}

int Map::getTopElement(Player p , RenderWindow &Window)
{


    //if(screenCapture.GetPixel(p.getX(),p.getY())  ==  );

    int i =    (( p.GetBoundingBox().Left + 20 )/ BLOCKSIZE ) ;
    int j =   ( (p.GetBoundingBox().Bottom + 5 )/ BLOCKSIZE ) ;
    int    pos ;



       pos = MapVector[j-1][i] ;



    return pos  ;
}

 
« Last Edit: December 25, 2012, 07:17:30 pm by riadh »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Need help with map collsion Here is my game , your opinion ?
« Reply #1 on: December 22, 2012, 12:18:35 pm »
Yes, we're here to help! :)
But you should also provide the relevant code and at best create a minimal and complete example.

For collision, you can take a look at the official wiki, where you can find some tutorials or simply google a bit more on collision detection.
One of the most simplest way to do collision detection is AABB checking, which can be done in SFML quite easily with:
sf::FloatRect playerRect(0.f, 0.f, 20.f, 20.f);
sf::FloatRect boxRect(0.f, 40.f, 20.f, 20.f);

if(playerRect.contains(boxRect))
    // Collision occurred
 
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

riadh

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Need help with map collsion Here is my game , your opinion ?
« Reply #2 on: December 22, 2012, 12:33:47 pm »
the problem isn't in the collision between 2 boundingBox for example, but is about how to check if there are a collison with the map wich is created with a mapVector, i will put my code now :)

riadh

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Need help with map collsion Here is my game , your opinion ?
« Reply #3 on: December 23, 2012, 04:04:20 pm »
any idea ?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Need help with map collsion Here is my game , your opinion ?
« Reply #4 on: December 23, 2012, 04:13:43 pm »
In my opinion it's too much code to go through and trying to understand it... Try to minimize it further.

If you want to check against the map you could simply iterate over all map tiles and do a collision check. Or you could calculate the approximate position in the map and check with the surrounding tiles.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

krzat

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Need help with map collsion Here is my game , your opinion ?
« Reply #5 on: December 23, 2012, 04:24:23 pm »
Take all tiles that are intesecting with your player and check them all for collisions.
« Last Edit: December 23, 2012, 04:26:12 pm by krzat »
SFML.Utils - useful extensions for SFML.Net

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Need help with map collsion Here is my game , your opinion ?
« Reply #6 on: December 23, 2012, 04:37:22 pm »
1. Use a physics library, Box2D will handle all your possible needs and much more.
2. Don't do this bad position+=delta*speed; stuff, it's hard to manage(tunneling, fps floating point inaccuracy causing incosistency, ect.), fix youre timestep to some sensible value instead.
3. What above post says
Back to C++ gamedev with SFML in May 2023

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Need help with map collsion Here is my game , your opinion ?
« Reply #7 on: December 23, 2012, 05:16:05 pm »
Didn't read through code much, but bascially, you want to check for collision, and if there is one set the player next to the block according to it's velocity. If velocity is greater than the height or width of the collision box, then you would want to break the check up into a number of increments equal to velocity divided height or width plus 1, to prevent the player from moving through blocks if they are moving really fast.

So, if y velocity is +, this means you are moving down, and if there is a collision, then you should set the player's y position equal to the top of the block. Player.y = block.y - player.GetHeight()

If you are moving with a -y velocity, then you would need to set it equal to the bottom of the block. player.y = block.y + block.GetHeight().

If moving with a +x velocity, then you know the player is moving right and should be set to the left side of the block and vice versa with a -x velocity.

So, update player velocity, check collision, if there is a collision, adjust the player's poistion. If there is no collision, then update player position using the original velocities.

riadh

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Need help with map collsion Here is my game , your opinion ?
« Reply #8 on: December 23, 2012, 06:47:05 pm »
i know that all my collision code should be in the trash hhh
i don't know if it would be a good idea to think about box2d in this short time

but no one of your replies helped me to know what exactly should i do , why i have never found any article or tutorial talking about collision with map while it's a fundamental thing :( ,

the first thing is the code in the map.cpp wich must be changed ?
for the player just getboundingbox is ok ?
thanks for your help

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Need help with map collsion Here is my game , your opinion ?
« Reply #9 on: December 23, 2012, 09:57:05 pm »
but no one of your replies helped me to know what exactly should i do , why i have never found any article or tutorial talking about collision with map while it's a fundamental thing :( ,

the first thing is the code in the map.cpp wich must be changed ?
for the player just getboundingbox is ok ?
We won't program for you (unless you pay us), but we only give points in the direction you should go, if you don't have enough knowledge on how to approach things you can ask again, but if you don't know at all where to start, it seems to me that you like some major knowledge in programming, which is required for the level you want to achieve things.

So our posts didn't say do X in file Y, but we gave you the idea you want, mainly: "Do a collision detection with each tile in the map that is interesting for the player."
If you don't understand that instruction you should ask again, but don't expect us to 'fix'/write code for you. ;)

Also if you haven't found anything on collision with maps etc. with Google, then I'd say you didn't search hard enough or with smart enough keywords...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

riadh

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Need help with map collsion Here is my game , your opinion ?
« Reply #10 on: December 23, 2012, 11:18:36 pm »
but no one of your replies helped me to know what exactly should i do , why i have never found any article or tutorial talking about collision with map while it's a fundamental thing :( ,

the first thing is the code in the map.cpp wich must be changed ?
for the player just getboundingbox is ok ?
We won't program for you (unless you pay us), but we only give points in the direction you should go, if you don't have enough knowledge on how to approach things you can ask again, but if you don't know at all where to start, it seems to me that you like some major knowledge in programming, which is required for the level you want to achieve things.

So our posts didn't say do X in file Y, but we gave you the idea you want, mainly: "Do a collision detection with each tile in the map that is interesting for the player."
If you don't understand that instruction you should ask again, but don't expect us to 'fix'/write code for you. ;)

Also if you haven't found anything on collision with maps etc. with Google, then I'd say you didn't search hard enough or with smart enough keywords...


hello  ; i know i'm not asking you to write the code for me , but asking if there are a tutorial talking about this stuff on sfml why not to put here , unless im' going to try your solution and then ask u again if i found some problem

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Need help with map collsion Here is my game , your opinion ?
« Reply #11 on: December 23, 2012, 11:31:56 pm »
but asking if there are a tutorial talking about this stuff on sfml why not to put here
Well if there was a tutorial for collision detection regarding tile-maps and specifically for SFML that I'd know of, I would've linked it long time ago. ::)
I guess you'll find some sample code in the forum somewhere and if you're ever curious about tutorials/source code for SFML, you can check on your own on the official SFML wiki. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

riadh

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Need help with map collsion Here is my game , your opinion ?
« Reply #12 on: December 24, 2012, 12:21:24 am »
but asking if there are a tutorial talking about this stuff on sfml why not to put here
Well if there was a tutorial for collision detection regarding tile-maps and specifically for SFML that I'd know of, I would've linked it long time ago. ::)
I guess you'll find some sample code in the forum somewhere and if you're ever curious about tutorials/source code for SFML, you can check on your own on the official SFML wiki. :)
thanks,  i think of making a tutorial of this if i made it :D

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Need help with map collsion Here is my game , your opinion ?
« Reply #13 on: December 24, 2012, 05:05:05 am »
Looking at all the tiles in your map would not be the best way, you only care about a handful to look at, which are those that are around where the player is trying to get to. Where the player is trying to get to is based on the player's velocity.

So first, you generate velocities for your player. Then, you test and see if these velocities will produce a valid path of movement, IE it won't cause the player to collide with anything. You only need to look at tiles that are in the player's intended movement path. If you collide with a block, then you need to set the player's position based on the block. If you don't collide with blocks then you can simply add your player's velocities to its position.